added firebase_analytics

This commit is contained in:
Vadim 2023-10-30 10:25:22 +01:00
parent 181c3ef7d8
commit 6d1be75070
8 changed files with 82 additions and 0 deletions

View file

@ -1,4 +1,6 @@
import 'package:flutter/material.dart'; 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/caffeine_service.dart';
import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_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 userPreferencesService = UserPreferencesService(snapshot.data![0] as SharedPreferences);
final hasLightSensor = snapshot.data![1] as bool; final hasLightSensor = snapshot.data![1] as bool;
return ServicesProvider( return ServicesProvider(
analytics: const LightmeterAnalytics(api: LightmeterAnalyticsFirebase()),
caffeineService: const CaffeineService(), caffeineService: const CaffeineService(),
environment: env.copyWith(hasLightSensor: hasLightSensor), environment: env.copyWith(hasLightSensor: hasLightSensor),
hapticsService: const HapticsService(), hapticsService: const HapticsService(),

View file

@ -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<void> logEvent(
LightmeterAnalyticsEvent event, {
Map<String, dynamic>? parameters,
}) async {
if (kDebugMode) {
log('<LightmeterAnalytics> logEvent: ${event.name} / $parameters');
return;
}
return _api.logEvent(
event: event,
parameters: parameters,
);
}
Future<void> logProFeaturesPurchaseAttempt(String purchaseButtonText) async {
return logEvent(
LightmeterAnalyticsEvent.unlockProFeatures,
parameters: {"purchaseButtonText": purchaseButtonText},
);
}
}

View file

@ -0,0 +1,8 @@
import 'package:lightmeter/data/analytics/entity/analytics_event.dart';
abstract class ILightmeterAnalyticsApi {
Future<void> logEvent({
required LightmeterAnalyticsEvent event,
Map<String, dynamic>? parameters,
});
}

View file

@ -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<void> logEvent({
required LightmeterAnalyticsEvent event,
Map<String, dynamic>? 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());
}
}
}

View file

@ -0,0 +1,3 @@
enum LightmeterAnalyticsEvent {
unlockProFeatures,
}

View file

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:lightmeter/data/analytics/analytics.dart';
import 'package:lightmeter/data/caffeine_service.dart'; import 'package:lightmeter/data/caffeine_service.dart';
import 'package:lightmeter/data/haptics_service.dart'; import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_service.dart'; import 'package:lightmeter/data/light_sensor_service.dart';
@ -10,6 +11,7 @@ import 'package:lightmeter/environment.dart';
// coverage:ignore-start // coverage:ignore-start
class ServicesProvider extends InheritedWidget { class ServicesProvider extends InheritedWidget {
final LightmeterAnalytics analytics;
final CaffeineService caffeineService; final CaffeineService caffeineService;
final Environment environment; final Environment environment;
final HapticsService hapticsService; final HapticsService hapticsService;
@ -20,6 +22,7 @@ class ServicesProvider extends InheritedWidget {
final VolumeEventsService volumeEventsService; final VolumeEventsService volumeEventsService;
const ServicesProvider({ const ServicesProvider({
required this.analytics,
required this.caffeineService, required this.caffeineService,
required this.environment, required this.environment,
required this.hapticsService, required this.hapticsService,

View file

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/feature.dart'; import 'package:lightmeter/data/models/feature.dart';
import 'package:lightmeter/generated/l10n.dart'; import 'package:lightmeter/generated/l10n.dart';
import 'package:lightmeter/providers/remote_config_provider.dart'; import 'package:lightmeter/providers/remote_config_provider.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/res/dimens.dart'; import 'package:lightmeter/res/dimens.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart'; import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
@ -29,6 +30,9 @@ Future<void> showBuyProDialog(BuildContext context) {
onPressed: () { onPressed: () {
Navigator.of(context).pop(); Navigator.of(context).pop();
IAPProductsProvider.of(context).buy(IAPProductType.paidFeatures); 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), child: Text(unlockFeaturesEnabled ? S.of(context).unlock : S.of(context).buy),
), ),

View file

@ -13,6 +13,7 @@ dependencies:
clipboard: 0.1.3 clipboard: 0.1.3
dynamic_color: 1.6.6 dynamic_color: 1.6.6
exif: 3.1.4 exif: 3.1.4
firebase_analytics: 10.6.2
firebase_core: 2.20.0 firebase_core: 2.20.0
firebase_crashlytics: 3.4.2 firebase_crashlytics: 3.4.2
firebase_remote_config: 4.3.2 firebase_remote_config: 4.3.2