From 6d1be75070c33e689cb33855edc577f555f1f303 Mon Sep 17 00:00:00 2001 From: Vadim <44135514+vodemn@users.noreply.github.com> Date: Mon, 30 Oct 2023 10:25:22 +0100 Subject: [PATCH] added `firebase_analytics` --- lib/application_wrapper.dart | 3 ++ lib/data/analytics/analytics.dart | 34 +++++++++++++++++++ .../api/analytics_api_interface.dart | 8 +++++ .../analytics/api/analytics_firebase.dart | 26 ++++++++++++++ .../analytics/entity/analytics_event.dart | 3 ++ lib/providers/services_provider.dart | 3 ++ .../components/utils/show_buy_pro_dialog.dart | 4 +++ pubspec.yaml | 1 + 8 files changed, 82 insertions(+) create mode 100644 lib/data/analytics/analytics.dart create mode 100644 lib/data/analytics/api/analytics_api_interface.dart create mode 100644 lib/data/analytics/api/analytics_firebase.dart create mode 100644 lib/data/analytics/entity/analytics_event.dart diff --git a/lib/application_wrapper.dart b/lib/application_wrapper.dart index 447d892..d0772ee 100644 --- a/lib/application_wrapper.dart +++ b/lib/application_wrapper.dart @@ -1,4 +1,6 @@ import 'package:flutter/material.dart'; +import 'package:lightmeter/data/analytics/analytics.dart'; +import 'package:lightmeter/data/analytics/api/analytics_firebase.dart'; import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/light_sensor_service.dart'; @@ -36,6 +38,7 @@ class ApplicationWrapper extends StatelessWidget { final userPreferencesService = UserPreferencesService(snapshot.data![0] as SharedPreferences); final hasLightSensor = snapshot.data![1] as bool; return ServicesProvider( + analytics: const LightmeterAnalytics(api: LightmeterAnalyticsFirebase()), caffeineService: const CaffeineService(), environment: env.copyWith(hasLightSensor: hasLightSensor), hapticsService: const HapticsService(), diff --git a/lib/data/analytics/analytics.dart b/lib/data/analytics/analytics.dart new file mode 100644 index 0000000..94b7550 --- /dev/null +++ b/lib/data/analytics/analytics.dart @@ -0,0 +1,34 @@ +import 'dart:async'; +import 'dart:developer'; + +import 'package:flutter/foundation.dart'; +import 'package:lightmeter/data/analytics/api/analytics_api_interface.dart'; +import 'package:lightmeter/data/analytics/entity/analytics_event.dart'; + +class LightmeterAnalytics { + final ILightmeterAnalyticsApi _api; + + const LightmeterAnalytics({required ILightmeterAnalyticsApi api}) : _api = api; + + Future logEvent( + LightmeterAnalyticsEvent event, { + Map? parameters, + }) async { + if (kDebugMode) { + log(' logEvent: ${event.name} / $parameters'); + return; + } + + return _api.logEvent( + event: event, + parameters: parameters, + ); + } + + Future logProFeaturesPurchaseAttempt(String purchaseButtonText) async { + return logEvent( + LightmeterAnalyticsEvent.unlockProFeatures, + parameters: {"purchaseButtonText": purchaseButtonText}, + ); + } +} diff --git a/lib/data/analytics/api/analytics_api_interface.dart b/lib/data/analytics/api/analytics_api_interface.dart new file mode 100644 index 0000000..1aa007f --- /dev/null +++ b/lib/data/analytics/api/analytics_api_interface.dart @@ -0,0 +1,8 @@ +import 'package:lightmeter/data/analytics/entity/analytics_event.dart'; + +abstract class ILightmeterAnalyticsApi { + Future logEvent({ + required LightmeterAnalyticsEvent event, + Map? parameters, + }); +} diff --git a/lib/data/analytics/api/analytics_firebase.dart b/lib/data/analytics/api/analytics_firebase.dart new file mode 100644 index 0000000..fb11d02 --- /dev/null +++ b/lib/data/analytics/api/analytics_firebase.dart @@ -0,0 +1,26 @@ +import 'package:firebase_analytics/firebase_analytics.dart'; +import 'package:firebase_core/firebase_core.dart'; +import 'package:flutter/foundation.dart'; +import 'package:lightmeter/data/analytics/api/analytics_api_interface.dart'; +import 'package:lightmeter/data/analytics/entity/analytics_event.dart'; + +class LightmeterAnalyticsFirebase implements ILightmeterAnalyticsApi { + const LightmeterAnalyticsFirebase(); + + @override + Future logEvent({ + required LightmeterAnalyticsEvent event, + Map? parameters, + }) async { + try { + await FirebaseAnalytics.instance.logEvent( + name: event.name, + parameters: parameters, + ); + } on FirebaseException catch (e) { + debugPrint('Firebase Analytics Exception: $e'); + } catch (e) { + debugPrint(e.toString()); + } + } +} diff --git a/lib/data/analytics/entity/analytics_event.dart b/lib/data/analytics/entity/analytics_event.dart new file mode 100644 index 0000000..8275869 --- /dev/null +++ b/lib/data/analytics/entity/analytics_event.dart @@ -0,0 +1,3 @@ +enum LightmeterAnalyticsEvent { + unlockProFeatures, +} diff --git a/lib/providers/services_provider.dart b/lib/providers/services_provider.dart index 080b42d..a439132 100644 --- a/lib/providers/services_provider.dart +++ b/lib/providers/services_provider.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:lightmeter/data/analytics/analytics.dart'; import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/light_sensor_service.dart'; @@ -10,6 +11,7 @@ import 'package:lightmeter/environment.dart'; // coverage:ignore-start class ServicesProvider extends InheritedWidget { + final LightmeterAnalytics analytics; final CaffeineService caffeineService; final Environment environment; final HapticsService hapticsService; @@ -20,6 +22,7 @@ class ServicesProvider extends InheritedWidget { final VolumeEventsService volumeEventsService; const ServicesProvider({ + required this.analytics, required this.caffeineService, required this.environment, required this.hapticsService, diff --git a/lib/screens/settings/components/utils/show_buy_pro_dialog.dart b/lib/screens/settings/components/utils/show_buy_pro_dialog.dart index 50edbd1..8460397 100644 --- a/lib/screens/settings/components/utils/show_buy_pro_dialog.dart +++ b/lib/screens/settings/components/utils/show_buy_pro_dialog.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:lightmeter/data/models/feature.dart'; import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/providers/remote_config_provider.dart'; +import 'package:lightmeter/providers/services_provider.dart'; import 'package:lightmeter/res/dimens.dart'; import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; @@ -29,6 +30,9 @@ Future showBuyProDialog(BuildContext context) { onPressed: () { Navigator.of(context).pop(); IAPProductsProvider.of(context).buy(IAPProductType.paidFeatures); + ServicesProvider.of(context) + .analytics + .logProFeaturesPurchaseAttempt(unlockFeaturesEnabled ? "Unlock" : "Buy"); }, child: Text(unlockFeaturesEnabled ? S.of(context).unlock : S.of(context).buy), ), diff --git a/pubspec.yaml b/pubspec.yaml index 67cb101..a7eb3a2 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -13,6 +13,7 @@ dependencies: clipboard: 0.1.3 dynamic_color: 1.6.6 exif: 3.1.4 + firebase_analytics: 10.6.2 firebase_core: 2.20.0 firebase_crashlytics: 3.4.2 firebase_remote_config: 4.3.2