Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add analytics user properties for Sharezone Plus. #1502

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/lib/main/sharezone_bloc_providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ class _SharezoneBlocProvidersState extends State<SharezoneBlocProviders> {
user: api.user.userStream,
functions: widget.blocDependencies.functions,
);
trySetSharezonePlusAnalyticsUserProperties(
analytics, crashAnalytics, subscriptionService);

final feedbackApi = FirebaseFeedbackApi(firestore);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}
24 changes: 16 additions & 8 deletions lib/user/lib/src/models/sharezone_plus_status.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic>? map) {
Expand All @@ -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'],
);
}

Expand All @@ -61,6 +63,7 @@ class SharezonePlusStatus {
other.hasPlus == hasPlus &&
other.isCancelled == isCancelled &&
other.source == source &&
other.period == period &&
other.hasLifetime == hasLifetime;
}

Expand All @@ -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;
}
Loading