Skip to content

Commit

Permalink
Add analytics properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Feb 21, 2024
1 parent 17f289e commit 571066f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 58 deletions.
23 changes: 23 additions & 0 deletions lib/Backend/NavigationObserver/CustomNavObserver.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flutter/material.dart';
import 'package:plausible_analytics/plausible_analytics.dart';
import 'package:sentry_hive/sentry_hive.dart';

import '../Definitions/Device/BaseDeviceDefinition.dart';

class CustomNavObserver extends NavigatorObserver {
/// The [Plausible] instance to report page views to.
final Plausible plausible;

CustomNavObserver(this.plausible);

@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
String? name = route.settings.name;
String refferalName = previousRoute?.settings.name ?? "";
if (name != null) {
plausible.screenWidth = MediaQuery.of(route.navigator!.context).size.width.toString();
plausible.event(page: route.settings.name.toString(), props: {"Number Of Devices": SentryHive.box<BaseStoredDevice>('devices').length.toString()}, referrer: refferalName);
}
}
}
15 changes: 9 additions & 6 deletions lib/Frontend/GoRouterConfig.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:plausible_analytics/navigator_observer.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:tail_app/Frontend/pages/DirectGearControl.dart';
import 'package:tail_app/Frontend/pages/Shell.dart';
Expand All @@ -10,23 +9,21 @@ import 'package:tail_app/Frontend/pages/move_list.dart';
import 'package:tail_app/Frontend/pages/settings.dart';
import 'package:tail_app/Frontend/pages/triggers.dart';

import '../Backend/NavigationObserver/CustomNavObserver.dart';
import '../main.dart';
import 'pages/Actions.dart';

final GlobalKey<NavigatorState> _rootNavigatorKey = GlobalKey<NavigatorState>();
final GlobalKey<NavigatorState> _shellNavigatorKey = GlobalKey<NavigatorState>();

// GoRouter configuration
final GoRouter router = GoRouter(
debugLogDiagnostics: kDebugMode,
navigatorKey: _rootNavigatorKey,
observers: [
SentryNavigatorObserver(),
PlausibleNavigatorObserver(plausible),
],
observers: [SentryNavigatorObserver(), CustomNavObserver(plausible)],
routes: [
ShellRoute(
navigatorKey: _shellNavigatorKey,
observers: [SentryNavigatorObserver(), CustomNavObserver(plausible)],
routes: [
GoRoute(
name: 'Actions',
Expand All @@ -35,6 +32,7 @@ final GoRouter router = GoRouter(
pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage(
child: const ActionPage(),
key: state.pageKey,
name: 'Actions',
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return FadeTransition(
opacity: animation,
Expand All @@ -50,6 +48,7 @@ final GoRouter router = GoRouter(
pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage(
child: const Triggers(),
key: state.pageKey,
name: 'Triggers',
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return FadeTransition(
opacity: animation,
Expand All @@ -64,6 +63,7 @@ final GoRouter router = GoRouter(
parentNavigatorKey: _shellNavigatorKey,
pageBuilder: (BuildContext context, GoRouterState state) => CustomTransitionPage(
key: state.pageKey,
name: 'Sequences',
child: const MoveListView(),
transitionsBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
return FadeTransition(
Expand All @@ -80,6 +80,7 @@ final GoRouter router = GoRouter(
pageBuilder: (context, state) {
return CustomTransitionPage(
key: state.pageKey,
name: 'Edit Sequence',
child: const EditMoveList(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
// Change the opacity of the screen using a Curve based on the the animation's
Expand All @@ -101,6 +102,7 @@ final GoRouter router = GoRouter(
pageBuilder: (context, state) {
return CustomTransitionPage(
key: state.pageKey,
name: 'Direct Gear Control',
child: const DirectGearControl(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
// Change the opacity of the screen using a Curve based on the the animation's
Expand All @@ -120,6 +122,7 @@ final GoRouter router = GoRouter(
pageBuilder: (context, state) {
return CustomTransitionPage(
key: state.pageKey,
name: 'Settings',
child: const Settings(),
transitionsBuilder: (context, animation, secondaryAnimation, child) {
// Change the opacity of the screen using a Curve based on the the animation's
Expand Down
14 changes: 0 additions & 14 deletions lib/Frontend/pages/Shell.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:tail_app/Backend/Definitions/Device/BaseDeviceDefinition.dart';
import 'package:tail_app/Frontend/Widgets/scan_for_new_device.dart';
import 'package:tail_app/Frontend/Widgets/snack_bar_overlay.dart';
import 'package:upgrader/upgrader.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../../Backend/AutoMove.dart';
import '../intnDefs.dart';
Expand Down Expand Up @@ -47,19 +46,6 @@ class NavigationDrawerExample extends ConsumerStatefulWidget {

class _NavigationDrawerExampleState extends ConsumerState<NavigationDrawerExample> {
int screenIndex = 0;
var controller = WebViewController()
..setJavaScriptMode(JavaScriptMode.unrestricted)
..setBackgroundColor(const Color(0x00000000))
..setNavigationDelegate(
NavigationDelegate(
onProgress: (int progress) {
// Update loading bar.
},
onPageStarted: (String url) {},
onPageFinished: (String url) {},
),
)
..loadRequest(Uri.parse('https://thetailcompany.com/'));

Widget getSignal(int rssi) {
if (rssi < -2) {
Expand Down
16 changes: 12 additions & 4 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import 'dart:async';
import 'dart:developer';

import 'package:feedback_sentry/feedback_sentry.dart';
import 'package:fk_user_agent/fk_user_agent.dart';
import 'package:flex_color_scheme/flex_color_scheme.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -37,13 +39,20 @@ FutureOr<SentryEvent?> beforeSend(SentryEvent event, {Hint? hint}) async {
}
}

const String serverUrl = "https://plausible.codel1417.xyz";
const String domain = "tail_app";
const String serverUrl = "https://plausable.codel1417.xyz";
const String domain = "tail-app";

final plausible = Plausible(serverUrl, domain);
final Plausible plausible = Plausible(serverUrl, domain);

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await FkUserAgent.init();
// Platform messages may fail, so we use a try/catch PlatformException.
try {
String platformVersion = FkUserAgent.userAgent!;
plausible.userAgent = platformVersion;
} on PlatformException {}
plausible.enabled = true;
Flogger.init(config: const FloggerConfig(showDebugLogs: true, printClassName: true, printMethodName: true, showDateTime: false));
PlatformDispatcher.instance.onError = (error, stack) {
Flogger.e(error.toString(), stackTrace: stack);
Expand Down Expand Up @@ -79,7 +88,6 @@ Future<void> main() async {
SentryHive.openBox<Trigger>('triggers');
SentryHive.openBox<MoveList>('sequences');
SentryHive.openBox<BaseStoredDevice>('devices');
plausible.enabled = false;
await SentryFlutter.init(
(options) {
options.dsn = 'https://[email protected]/2';
Expand Down
42 changes: 9 additions & 33 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.1.0"
fk_user_agent:
dependency: "direct main"
description:
name: fk_user_agent
sha256: fd6c94e120786985a292d12f61422a581f4e851148d5940af38b819357b8ad0d
url: "https://pub.dev"
source: hosted
version: "2.1.0"
flex_color_scheme:
dependency: "direct main"
description:
Expand Down Expand Up @@ -1466,38 +1474,6 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.2.1"
webview_flutter:
dependency: "direct main"
description:
name: webview_flutter
sha256: "25e1b6e839e8cbfbd708abc6f85ed09d1727e24e08e08c6b8590d7c65c9a8932"
url: "https://pub.dev"
source: hosted
version: "4.7.0"
webview_flutter_android:
dependency: transitive
description:
name: webview_flutter_android
sha256: "3e5f4e9d818086b0d01a66fb1ff9cc72ab0cc58c71980e3d3661c5685ea0efb0"
url: "https://pub.dev"
source: hosted
version: "3.15.0"
webview_flutter_platform_interface:
dependency: transitive
description:
name: webview_flutter_platform_interface
sha256: d937581d6e558908d7ae3dc1989c4f87b786891ab47bb9df7de548a151779d8d
url: "https://pub.dev"
source: hosted
version: "2.10.0"
webview_flutter_wkwebview:
dependency: transitive
description:
name: webview_flutter_wkwebview
sha256: "9bf168bccdf179ce90450b5f37e36fe263f591c9338828d6bf09b6f8d0f57f86"
url: "https://pub.dev"
source: hosted
version: "3.12.0"
win32:
dependency: transitive
description:
Expand Down Expand Up @@ -1540,4 +1516,4 @@ packages:
version: "3.1.2"
sdks:
dart: ">=3.2.3 <4.0.0"
flutter: ">=3.16.6"
flutter: ">=3.16.0"
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ dependencies:
open_settings: ^2.0.2
flutter_colorpicker: ^1.0.3
flutter_adaptive_scaffold: ^0.1.7+2
webview_flutter: ^4.7.0
hive: ^2.2.3
hive_flutter: ^1.1.0
path_provider: ^2.1.2
uuid: ^4.3.3
plausible_analytics: ^0.3.0
fk_user_agent: ^2.1.0
dev_dependencies:
build_runner:
flutter_test:
Expand Down

0 comments on commit 571066f

Please sign in to comment.