diff --git a/app/lib/main/sharezone_bloc_providers.dart b/app/lib/main/sharezone_bloc_providers.dart index 30cd6a7f6..97862bdb5 100644 --- a/app/lib/main/sharezone_bloc_providers.dart +++ b/app/lib/main/sharezone_bloc_providers.dart @@ -322,6 +322,8 @@ class _SharezoneBlocProvidersState extends State { user: api.user.userStream, functions: widget.blocDependencies.functions, ); + trySetSharezonePlusAnalyticsUserProperties( + analytics, crashAnalytics, subscriptionService); final feedbackApi = FirebaseFeedbackApi(firestore); diff --git a/app/lib/sharezone_plus/subscription_service/subscription_service.dart b/app/lib/sharezone_plus/subscription_service/subscription_service.dart index 210b068f2..ace57c454 100644 --- a/app/lib/sharezone_plus/subscription_service/subscription_service.dart +++ b/app/lib/sharezone_plus/subscription_service/subscription_service.dart @@ -8,7 +8,9 @@ import 'dart:async'; +import 'package:analytics/analytics.dart'; import 'package:cloud_functions/cloud_functions.dart'; +import 'package:crash_analytics/crash_analytics.dart'; import 'package:user/user.dart'; class SubscriptionService { @@ -79,3 +81,34 @@ enum SharezonePlusFeature { viewPastEvents, homeworkDueDateChips, } + +void trySetSharezonePlusAnalyticsUserProperties(Analytics analytics, + CrashAnalytics crashAnalytics, SubscriptionService subscriptionService) { + try { + subscriptionService.sharezonePlusStatusStream.listen((final status) { + if (status != null) { + analytics.setUserProperty( + name: 'has_plus', + value: status.hasPlus.toString(), + ); + if (status.hasPlus) { + if (status.period != null) { + analytics.setUserProperty( + name: 'plus_period', + value: status.period!, + ); + } + var source = status.source ?? SubscriptionSource.unknown; + analytics.setUserProperty( + name: 'plus_source', + value: source.stableAnalyticsKey, + ); + } + } + }); + } catch (e) { + crashAnalytics + .log('Error setting user properties regarding Sharezone Plus: $e'); + crashAnalytics.recordError(e, StackTrace.current); + } +} diff --git a/lib/user/lib/src/models/sharezone_plus_status.dart b/lib/user/lib/src/models/sharezone_plus_status.dart index a7075984a..f8d6bfa6a 100644 --- a/lib/user/lib/src/models/sharezone_plus_status.dart +++ b/lib/user/lib/src/models/sharezone_plus_status.dart @@ -21,13 +21,15 @@ class SharezonePlusStatus { final SubscriptionSource? source; /// Whether the user has a lifetime subscription. - final bool hasLifetime; + bool get hasLifetime => period == 'lifetime'; + + final String? period; const SharezonePlusStatus({ required this.hasPlus, required this.isCancelled, required this.source, - required this.hasLifetime, + required this.period, }); static SharezonePlusStatus? fromData(Map? map) { @@ -40,7 +42,7 @@ class SharezonePlusStatus { hasPlus: map['hasPlus'] ?? false, isCancelled: map['isCancelled'] ?? false, source: parseSource(map['subscriptionDetails']), - hasLifetime: map['period'] == 'lifetime', + period: map['period'], ); } @@ -61,6 +63,7 @@ class SharezonePlusStatus { other.hasPlus == hasPlus && other.isCancelled == isCancelled && other.source == source && + other.period == period && other.hasLifetime == hasLifetime; } @@ -69,17 +72,22 @@ class SharezonePlusStatus { return hasPlus.hashCode ^ isCancelled.hashCode ^ source.hashCode ^ + period.hashCode ^ hasLifetime.hashCode; } } enum SubscriptionSource { - playStore('Play Store'), - appStore('App Store'), - stripe('Stripe'), - unknown('Unknown'); + playStore(uiString: 'Play Store', stableAnalyticsKey: 'playstore'), + appStore(uiString: 'App Store', stableAnalyticsKey: 'appstore'), + stripe(uiString: 'Stripe', stableAnalyticsKey: 'stripe'), + unknown(uiString: 'Unknown', stableAnalyticsKey: 'unknown'); - const SubscriptionSource(this.uiString); + const SubscriptionSource({ + required this.uiString, + required this.stableAnalyticsKey, + }); final String uiString; + final String stableAnalyticsKey; }