Skip to content

Commit

Permalink
Improving user agent generation
Browse files Browse the repository at this point in the history
  • Loading branch information
clone1018 committed Mar 18, 2022
1 parent 36eb27d commit c49f142
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion lib/track.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,54 @@
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";

final track = Plausible(serverUrl, domain);
final track = Plausible(
serverUrl,
domain,
userAgent: UserAgentBuilder.build(),
);

class UserAgentBuilder {
// Build to help Plausible figure out what our device usage is. Not perfect yet though!
static String build() {
String os = Platform.operatingSystem;
String osVersion = Platform.operatingSystemVersion;
String osString = "${os} ${osVersion}";

if (Platform.isIOS) {
if (UserAgentBuilder.isTablet()) {
os = "iPad";
} else {
os = "iPhone";
}
osVersion = Platform.operatingSystemVersion
.replaceAll('"', '')
.replaceAll(".", "_");

osString = "${os}; CPU ${os} ${osVersion} like Mac OS X";
} else if (Platform.isAndroid) {
os = "Android";
osVersion = Platform.operatingSystemVersion
.replaceAll('"', '')
.replaceAll(".", "_");

osString = "Linux; ${os} ${osVersion}";
}

return "Mozilla/5.0 (${osString}) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E217";
}

static bool isTablet() {
if (WidgetsBinding.instance != null) {
final data = MediaQueryData.fromWindow(WidgetsBinding.instance!.window);
return data.size.shortestSide > 600;
}

return false;
}
}

0 comments on commit c49f142

Please sign in to comment.