Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Platform conditional user agent tracking for Plausible #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/AppScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/CategoryListScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/ChannelListScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/FollowingScreen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/screens/LoginScreen.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
19 changes: 5 additions & 14 deletions lib/track.dart → lib/track/mobile_track.dart
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
5 changes: 5 additions & 0 deletions lib/track/stub_track.dart
Original file line number Diff line number Diff line change
@@ -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");
30 changes: 30 additions & 0 deletions lib/track/track.dart
Original file line number Diff line number Diff line change
@@ -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();
}
11 changes: 11 additions & 0 deletions lib/track/web_track.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}