Skip to content

Commit

Permalink
Added disable feature
Browse files Browse the repository at this point in the history
  • Loading branch information
bostrot committed Nov 6, 2021
1 parent 6bc8d5f commit 5722048
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.1

* Added disable feature.

## 0.1.0

* Initial working release.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,11 @@ final event = plausible.event(
referrer: 'referrerPage');
```

Disable analytics (might be useful if a user opts out):

```dart
plausible.enabled = false;
```

You can also use a custom user agent but that is not recommended as
the default one already puts in the current Operation System & Version.
8 changes: 6 additions & 2 deletions lib/plausible_analytics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ class Plausible {
String userAgent;
String domain;
String screenWidth;
String appName;
bool enabled = true;

/// Constructor
Plausible(this.serverUrl, this.domain,
{this.userAgent = "", this.screenWidth = "", this.appName = "app"});
{this.userAgent = "", this.screenWidth = ""});

/// Post event to plausible
Future<int> event(
{String name = "pageview",
String referrer = "",
String page = "main"}) async {
if (!enabled) {
return 0;
}

// Post-edit parameters
int lastCharIndex = serverUrl.length - 1;
if (serverUrl.toString()[lastCharIndex] == '/') {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: plausible_analytics
description: A Flutter plugin for Plausible Analytics.
version: 0.1.0
version: 0.1.1
homepage: https://github.com/bostrot/flutter_plausible_analytics.git
issue_tracker: https://github.com/bostrot/flutter_plausible_analytics/issues

Expand Down
11 changes: 11 additions & 0 deletions test/plausible_analytics_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,15 @@ void main() {
name: 'conversion', page: 'homescreen', referrer: 'referrerPage');
expect(await event, 202);
});
test('check disabled call', () async {
final plausible = Plausible(serverUrl, domain, screenWidth: screenWidth);
plausible.enabled = false;
expect(plausible.serverUrl, serverUrl);
expect(plausible.domain, domain);
expect(plausible.screenWidth, screenWidth);

final event = plausible.event(
name: 'conversion', page: 'homescreen', referrer: 'referrerPage');
expect(await event, 0);
});
}

0 comments on commit 5722048

Please sign in to comment.