From f257ae3e11f0a25e85f8aec55a82d63d1f2af4ef Mon Sep 17 00:00:00 2001 From: Luke Strickland Date: Thu, 23 Feb 2023 09:33:07 -0500 Subject: [PATCH] Platform conditional user agent tracking for Plausible --- lib/main.dart | 2 +- lib/screens/AppScreen.dart | 2 +- lib/screens/CategoryListScreen.dart | 2 +- lib/screens/ChannelListScreen.dart | 2 +- lib/screens/FollowingScreen.dart | 2 +- lib/screens/LoginScreen.dart | 2 +- lib/{track.dart => track/mobile_track.dart} | 19 ++++--------- lib/track/stub_track.dart | 5 ++++ lib/track/track.dart | 30 +++++++++++++++++++++ lib/track/web_track.dart | 11 ++++++++ 10 files changed, 57 insertions(+), 20 deletions(-) rename lib/{track.dart => track/mobile_track.dart} (75%) create mode 100644 lib/track/stub_track.dart create mode 100644 lib/track/track.dart create mode 100644 lib/track/web_track.dart diff --git a/lib/main.dart b/lib/main.dart index 6ad9b72..815da5e 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -8,7 +8,7 @@ import 'package:glimesh_app/blocs/repos/chat_messages_bloc.dart'; import 'package:glimesh_app/blocs/repos/follow_bloc.dart'; import 'package:glimesh_app/blocs/repos/settings_bloc.dart'; import 'package:glimesh_app/screens/AppScreen.dart'; -import 'package:glimesh_app/track.dart'; +import 'package:glimesh_app/track/track.dart'; import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:gettext_i18n/gettext_i18n.dart'; diff --git a/lib/screens/AppScreen.dart b/lib/screens/AppScreen.dart index 3522273..93cfa64 100644 --- a/lib/screens/AppScreen.dart +++ b/lib/screens/AppScreen.dart @@ -8,7 +8,7 @@ import 'package:glimesh_app/screens/ProfileScreen.dart'; import 'package:glimesh_app/screens/CategoryListScreen.dart'; import 'package:glimesh_app/screens/FollowingScreen.dart'; -import 'package:glimesh_app/track.dart'; +import 'package:glimesh_app/track/track.dart'; class AppScreen extends StatefulWidget { AppScreen({Key? key, required this.title}) : super(key: key); diff --git a/lib/screens/CategoryListScreen.dart b/lib/screens/CategoryListScreen.dart index 3e8d6c8..63ca207 100644 --- a/lib/screens/CategoryListScreen.dart +++ b/lib/screens/CategoryListScreen.dart @@ -8,7 +8,7 @@ import 'package:glimesh_app/blocs/repos/channel_list_bloc.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:glimesh_app/repository.dart'; import 'package:gettext_i18n/gettext_i18n.dart'; -import 'package:glimesh_app/track.dart'; +import 'package:glimesh_app/track/track.dart'; class CategoryListScreen extends StatelessWidget { @override diff --git a/lib/screens/ChannelListScreen.dart b/lib/screens/ChannelListScreen.dart index ef0beef..07ca5e5 100644 --- a/lib/screens/ChannelListScreen.dart +++ b/lib/screens/ChannelListScreen.dart @@ -6,7 +6,7 @@ import 'package:glimesh_app/repository.dart'; import 'package:glimesh_app/models.dart'; import 'package:glimesh_app/blocs/repos/auth_bloc.dart'; import 'package:gettext_i18n/gettext_i18n.dart'; -import 'package:glimesh_app/track.dart'; +import 'package:glimesh_app/track/track.dart'; class ChannelListScreen extends StatelessWidget { @override diff --git a/lib/screens/FollowingScreen.dart b/lib/screens/FollowingScreen.dart index 7dfa56a..d710777 100644 --- a/lib/screens/FollowingScreen.dart +++ b/lib/screens/FollowingScreen.dart @@ -7,7 +7,7 @@ import 'package:glimesh_app/components/Loading.dart'; import 'package:glimesh_app/repository.dart'; import 'package:glimesh_app/models.dart'; import 'package:gettext_i18n/gettext_i18n.dart'; -import 'package:glimesh_app/track.dart'; +import 'package:glimesh_app/track/track.dart'; class FollowingScreen extends StatelessWidget { @override diff --git a/lib/screens/LoginScreen.dart b/lib/screens/LoginScreen.dart index 8b525c6..87ec5dc 100644 --- a/lib/screens/LoginScreen.dart +++ b/lib/screens/LoginScreen.dart @@ -1,7 +1,7 @@ import 'package:flutter/material.dart'; import 'package:glimesh_app/blocs/repos/auth_bloc.dart'; import 'package:glimesh_app/glimesh.dart'; -import 'package:glimesh_app/track.dart'; +import 'package:glimesh_app/track/track.dart'; import 'package:graphql_flutter/graphql_flutter.dart'; import 'package:auto_size_text/auto_size_text.dart'; import 'package:package_info_plus/package_info_plus.dart'; diff --git a/lib/track.dart b/lib/track/mobile_track.dart similarity index 75% rename from lib/track.dart rename to lib/track/mobile_track.dart index 5460683..9a5bcd3 100644 --- a/lib/track.dart +++ b/lib/track/mobile_track.dart @@ -1,27 +1,18 @@ -library glimesh_app.track; - import 'dart:io'; import 'package:flutter/material.dart'; -import 'package:plausible_analytics/plausible_analytics.dart'; - -const String serverUrl = "https://plausible.io"; -const String domain = "app.glimesh.tv"; +import 'package:glimesh_app/track/track.dart'; -final track = Plausible( - serverUrl, - domain, - userAgent: UserAgentBuilder.build(), -); +UserAgentBuilder getUserAgentBuilder() => MobileAgentBuilder(); -class UserAgentBuilder { +class MobileAgentBuilder extends UserAgentBuilder { // Build to help Plausible figure out what our device usage is. Not perfect yet though! - static String build() { + String build() { String os = Platform.operatingSystem; String osVersion = Platform.operatingSystemVersion; String osString = "${os} ${osVersion}"; if (Platform.isIOS) { - if (UserAgentBuilder.isTablet()) { + if (isTablet()) { os = "iPad"; } else { os = "iPhone"; diff --git a/lib/track/stub_track.dart b/lib/track/stub_track.dart new file mode 100644 index 0000000..dbfb324 --- /dev/null +++ b/lib/track/stub_track.dart @@ -0,0 +1,5 @@ +import 'package:glimesh_app/auth/handshake.dart'; +import 'package:glimesh_app/track/track.dart'; + +UserAgentBuilder getUserAgentBuilder() => + throw UnsupportedError("Cannot create an auth handshake"); diff --git a/lib/track/track.dart b/lib/track/track.dart new file mode 100644 index 0000000..10ee850 --- /dev/null +++ b/lib/track/track.dart @@ -0,0 +1,30 @@ +library glimesh_app.track; + +import 'package:plausible_analytics/plausible_analytics.dart'; + +// Conditionally loads mobile / web libraries based on compiled build +import 'package:glimesh_app/track/stub_track.dart' + if (dart.library.io) 'package:glimesh_app/track/mobile_track.dart' + if (dart.library.js) 'package:glimesh_app/track/web_track.dart'; + +const String serverUrl = "https://plausible.io"; +const String domain = "app.glimesh.tv"; + +final userAgentBuilder = UserAgentBuilder.instance; + +final track = Plausible( + serverUrl, + domain, + userAgent: userAgentBuilder.build(), +); + +abstract class UserAgentBuilder { + static UserAgentBuilder? _instance; + + static UserAgentBuilder get instance { + _instance ??= getUserAgentBuilder(); + return _instance!; + } + + String build(); +} diff --git a/lib/track/web_track.dart b/lib/track/web_track.dart new file mode 100644 index 0000000..a2f89ad --- /dev/null +++ b/lib/track/web_track.dart @@ -0,0 +1,11 @@ +import 'dart:html' show window; +import 'package:glimesh_app/track/track.dart'; + +UserAgentBuilder getUserAgentBuilder() => WebAgentBuilder(); + +class WebAgentBuilder extends UserAgentBuilder { + // Pull the user agent directly from the window + String build() { + return window.navigator.userAgent; + } +}