From 572204860b3756ed14d1e074dda6480a939d2c4b Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 6 Nov 2021 21:30:39 +0100 Subject: [PATCH] Added disable feature --- CHANGELOG.md | 4 ++++ README.md | 6 ++++++ lib/plausible_analytics.dart | 8 ++++++-- pubspec.yaml | 2 +- test/plausible_analytics_test.dart | 11 +++++++++++ 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4c1f42a..f16f1c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.1.1 + +* Added disable feature. + ## 0.1.0 * Initial working release. diff --git a/README.md b/README.md index ee37fb6..0a057c5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lib/plausible_analytics.dart b/lib/plausible_analytics.dart index d95a905..4a8b716 100644 --- a/lib/plausible_analytics.dart +++ b/lib/plausible_analytics.dart @@ -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 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] == '/') { diff --git a/pubspec.yaml b/pubspec.yaml index e001be3..1990d8d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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 diff --git a/test/plausible_analytics_test.dart b/test/plausible_analytics_test.dart index 1e1bb1f..49752b0 100644 --- a/test/plausible_analytics_test.dart +++ b/test/plausible_analytics_test.dart @@ -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); + }); }