6177214e-ce7c-49e3-99de-ff9721b26f63 — Commit 204c72b9

AuthorMikkel Thygesen<mth@dwarf.dk>
Date2025-01-20 22:31:38 +0100
478: Abstracted tracking interface, added item model

Changed files

.../lib/.generated/database/comwell_db.g.dart      |   4 +-
 .../tracking/models/analytics_event_item.g.dart    |  33 ++++++
 .../pregistration/bloc/preregistration_cubit.dart  |  68 ++++++------
 comwell_key_app/lib/tracking/FirebaseTracking.dart | 116 ---------------------
 comwell_key_app/lib/tracking/comwell_tracking.dart |  22 ++++
 .../lib/tracking/models/analytics_event_item.dart  |  31 ++++++
 .../lib/tracking/trackers/firebase_tracker.dart    |  49 +++++++++
 comwell_key_app/lib/utils/locator.dart             |   4 +-
 8 files changed, 177 insertions(+), 150 deletions(-)

Diff

diff --git a/comwell_key_app/lib/.generated/database/comwell_db.g.dart b/comwell_key_app/lib/.generated/database/comwell_db.g.dart
index 99db1942..dd1959d6 100644
--- a/comwell_key_app/lib/.generated/database/comwell_db.g.dart
+++ b/comwell_key_app/lib/.generated/database/comwell_db.g.dart
@@ -59,7 +59,7 @@ class $BookingEntityTable extends BookingEntity
}
@override
- Set<GeneratedColumn> get $primaryKey => const {};
+ Set<GeneratedColumn> get $primaryKey => {id};
@override
BookingDb map(Map<String, dynamic> data, {String? tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
@@ -275,7 +275,7 @@ class $UserEntityTable extends UserEntity
}
@override
- Set<GeneratedColumn> get $primaryKey => const {};
+ Set<GeneratedColumn> get $primaryKey => {id};
@override
UserDb map(Map<String, dynamic> data, {String? tablePrefix}) {
final effectivePrefix = tablePrefix != null ? '$tablePrefix.' : '';
diff --git a/comwell_key_app/lib/.generated/tracking/models/analytics_event_item.g.dart b/comwell_key_app/lib/.generated/tracking/models/analytics_event_item.g.dart
new file mode 100644
index 00000000..5496c052
--- /dev/null
+++ b/comwell_key_app/lib/.generated/tracking/models/analytics_event_item.g.dart
@@ -0,0 +1,33 @@
+// GENERATED CODE - DO NOT MODIFY BY HAND
+
+part of '../../../tracking/models/analytics_event_item.dart';
+
+// **************************************************************************
+// JsonSerializableGenerator
+// **************************************************************************
+
+AnalyticsEventItem _$AnalyticsEventItemFromJson(Map<String, dynamic> json) =>
+ AnalyticsEventItem(
+ hotelName: json['hotelName'] as String,
+ currency: json['currency'] as String,
+ value: json['value'] as String,
+ placement: json['placement'] as String,
+ items: (json['items'] as List<dynamic>).map((e) => e as String),
+ itemId: json['itemId'] as String,
+ itemName: json['itemName'] as String,
+ price: json['price'] as num,
+ quantity: json['quantity'] as num,
+ );
+
+Map<String, dynamic> _$AnalyticsEventItemToJson(AnalyticsEventItem instance) =>
+ <String, dynamic>{
+ 'hotelName': instance.hotelName,
+ 'currency': instance.currency,
+ 'value': instance.value,
+ 'placement': instance.placement,
+ 'items': instance.items.toList(),
+ 'itemId': instance.itemId,
+ 'itemName': instance.itemName,
+ 'price': instance.price,
+ 'quantity': instance.quantity,
+ };
diff --git a/comwell_key_app/lib/pregistration/bloc/preregistration_cubit.dart b/comwell_key_app/lib/pregistration/bloc/preregistration_cubit.dart
index a2f2ba45..b38e313f 100644
--- a/comwell_key_app/lib/pregistration/bloc/preregistration_cubit.dart
+++ b/comwell_key_app/lib/pregistration/bloc/preregistration_cubit.dart
@@ -5,7 +5,9 @@ import 'package:comwell_key_app/pregistration/pregistration_repository.dart';
import 'package:comwell_key_app/pregistration/preregistration_flow.dart';
import 'package:comwell_key_app/services/adyen/adyen_amount.dart';
import 'package:comwell_key_app/services/adyen/stored_payment_method.dart';
-import 'package:comwell_key_app/tracking/FirebaseTracking.dart';
+import 'package:comwell_key_app/tracking/comwell_tracking.dart';
+import 'package:comwell_key_app/tracking/models/analytics_event_item.dart';
+import 'package:comwell_key_app/tracking/trackers/firebase_tracker.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
@@ -15,7 +17,7 @@ import '../../utils/locator.dart';
class PreregistrationCubit extends Cubit<PreregistrationState> {
final _profileSettingsRepository = locator<ProfileSettingsRepository>();
final _preregistrationRepository = locator<PreregistrationRepository>();
- final _tracking = locator<FirebaseTracking>();
+ final _tracking = locator<ComwellTracking>();
final pageController = PageController();
final addressTextController = TextEditingController();
@@ -28,7 +30,10 @@ class PreregistrationCubit extends Cubit<PreregistrationState> {
bool _isAnimating = false;
PreregistrationCubit() : super(const PreregistrationState(loading: false)) {
- _tracking.trackScreenViewPrereg();
+ _tracking.trackScreenView(
+ "Pre-registration - Betalingskort",
+ "/pre-registration/betalingskort",
+ );
}
void init() async {
@@ -79,29 +84,31 @@ class PreregistrationCubit extends Cubit<PreregistrationState> {
}
void addToCart() {
- _tracking.trackAddToCart(
+ final analyticsEventItem = AnalyticsEventItem(
+ hotelName: "Comwell",
currency: "DKK",
- hotelName: "COMWELL",
- itemId: "ITEM_ID",
- itemName: "ITEM_NAME",
- items: ["ITEM_NAME"],
- placement: "pre-registration",
- price: 500,
- quantity: 5,
- value: 5000);
+ value: 500,
+ placement: "placement",
+ items: ["items"],
+ itemId: "itemId",
+ itemName: "itemName",
+ price: 200,
+ quantity: 200);
+ _tracking.trackAddToCart(analyticsEventItem);
}
void removeToCard() {
- _tracking.trackRemoveFromCard(
+ final analyticsEventItem = AnalyticsEventItem(
+ hotelName: "Comwell",
currency: "DKK",
- hotelName: "COMWELL",
- itemId: "ITEM_ID",
- itemName: "ITEM_NAME",
- items: ["ITEM_NAME"],
- placement: "pre-registration",
- price: 500,
- quantity: 5,
- value: 5000);
+ value: 500,
+ placement: "placement",
+ items: ["items"],
+ itemId: "itemId",
+ itemName: "itemName",
+ price: 200,
+ quantity: 200);
+ _tracking.trackRemoveFromCart(analyticsEventItem);
}
void onPaymentMethodsContinueClicked() {
@@ -109,16 +116,17 @@ class PreregistrationCubit extends Cubit<PreregistrationState> {
}
void _onConfirmPressed() async {
- _tracking.trackBeginCheckout(
+ final analyticsEventItem = AnalyticsEventItem(
+ hotelName: "Comwell",
currency: "DKK",
- hotelName: "COMWELL",
- itemId: "ITEM_ID",
- itemName: "ITEM_NAME",
- items: ["ITEM_NAME"],
- placement: "pre-registration",
- price: 500,
- quantity: 5,
- value: 5000);
+ value: 500,
+ placement: "placement",
+ items: ["items"],
+ itemId: "itemId",
+ itemName: "itemName",
+ price: 200,
+ quantity: 200);
+ _tracking.trackBeginCheckout(analyticsEventItem);
emit(state.copyWith(loading: true));
Future.delayed(const Duration(seconds: 3), () {
emit(state.copyWith(loading: false));
diff --git a/comwell_key_app/lib/tracking/FirebaseTracking.dart b/comwell_key_app/lib/tracking/FirebaseTracking.dart
deleted file mode 100644
index 38df1350..00000000
--- a/comwell_key_app/lib/tracking/FirebaseTracking.dart
+++ /dev/null
@@ -1,116 +0,0 @@
-import 'dart:ffi';
-
-import 'package:firebase_analytics/firebase_analytics.dart';
-import 'package:flutter/foundation.dart';
-
-class FirebaseTracking {
- final _tracking = FirebaseAnalytics.instance;
-
- void trackScreenViewPrereg() {
- _track(_eventScreenView, {
- _paramScreenClass: "prereg",
- _paramScreenName: "prereg",
- });
- }
-
- void trackAddToCart({
- required String currency,
- required num value,
- required String placement,
- required Iterable<String> items,
- required String itemId,
- required String itemName,
- required num price,
- required num quantity,
- required String hotelName,
- }) {
- _track(_eventAddToCart, {
- _paramCurrency: currency, // Currency of the items associated with the event, in 3-letter ISO 4217 format.
- _paramValue: value, // The monetary value of the event. Set value to the sum of (price * quantity) for all items in items. Don't include shipping or tax.
- _paramPlacement: placement, // The items for the event.
- _paramItems: items, // Where in the app flow this event happened
- _paramItemId: itemId, // The ID of the item. One of item_id or item_name is required.
- _paramItemName: itemName, // The name of the item. One of item_id or item_name is required.
- _paramPrice: price, // The monetary unit price of the item, in units of the specified currency parameter.
- _paramQuantity: quantity, // Item quantity. If not set, quantity is set to 1.
- _paramHotelName: hotelName // The hotel name associated with the event.
- });
- }
-
- void trackRemoveFromCard({
- required String currency,
- required num value,
- required String placement,
- required Iterable<String> items,
- required String itemId,
- required String itemName,
- required num price,
- required num quantity,
- required String hotelName,
- }) {
- _track(_eventRemoveFromCart, {
- _paramCurrency: currency, // Currency of the items associated with the event, in 3-letter ISO 4217 format.
- _paramValue: value, // The monetary value of the event. Set value to the sum of (price * quantity) for all items in items. Don't include shipping or tax.
- _paramPlacement: placement, // The items for the event.
- _paramItems: items, // Where in the app flow this event happened
- _paramItemId: itemId, // The ID of the item. One of item_id or item_name is required.
- _paramItemName: itemName, // The name of the item. One of item_id or item_name is required.
- _paramPrice: price, // The monetary unit price of the item, in units of the specified currency parameter.
- _paramQuantity: quantity, // Item quantity. If not set, quantity is set to 1.
- _paramHotelName: hotelName // The hotel name associated with the event.
- });
- }
-
- void trackBeginCheckout({
- required String currency,
- required num value,
- required String placement,
- required Iterable<String> items,
- required String itemId,
- required String itemName,
- required num price,
- required num quantity,
- required String hotelName,
- }) {
- _track(_eventBeginCheckout, {
- _paramCurrency: currency, // Currency of the items associated with the event, in 3-letter ISO 4217 format.
- _paramValue: value, // The monetary value of the event. Set value to the sum of (price * quantity) for all items in items. Don't include shipping or tax.
- _paramPlacement: placement, // The items for the event.
- _paramItems: items, // Where in the app flow this event happened
- _paramItemId: itemId, // The ID of the item. One of item_id or item_name is required.
- _paramItemName: itemName, // The name of the item. One of item_id or item_name is required.
- _paramPrice: price, // The monetary unit price of the item, in units of the specified currency parameter.
- _paramQuantity: quantity, // Item quantity. If not set, quantity is set to 1.
- _paramHotelName: hotelName // The hotel name associated with the event.
- });
- }
-
- void _track(String name, Map<String, Object>? parameters) async {
- try {
- await _tracking.logEvent(name: name, parameters: parameters);
- } catch (e) {
- if (kDebugMode) {
- debugPrint("qqq tracking error error=$e");
- }
- }
- }
-
- // event names
- static const _eventScreenView = "screen_view";
- static const _eventAddToCart = "add_to_cart";
- static const _eventRemoveFromCart = "remove_from_cart";
- static const _eventBeginCheckout = "begin_checkout";
-
- // props
- static const _paramScreenClass = "screen_class";
- static const _paramScreenName = "screen_name";
- static const _paramHotelName = "hotel_name";
- static const _paramCurrency = "currency";
- static const _paramValue = "value";
- static const _paramPlacement = "placement";
- static const _paramItems = "items";
- static const _paramItemId = "itemId";
- static const _paramItemName = "itemName";
- static const _paramPrice = "price";
- static const _paramQuantity = "quantity";
-}
diff --git a/comwell_key_app/lib/tracking/comwell_tracking.dart b/comwell_key_app/lib/tracking/comwell_tracking.dart
new file mode 100644
index 00000000..e2fa5ad8
--- /dev/null
+++ b/comwell_key_app/lib/tracking/comwell_tracking.dart
@@ -0,0 +1,22 @@
+import 'package:comwell_key_app/tracking/models/analytics_event_item.dart';
+import 'package:comwell_key_app/tracking/trackers/firebase_tracker.dart';
+
+class ComwellTracking {
+ final _firebaseTracking = FirebaseTracker();
+
+ void trackScreenView(String name, String path) {
+ _firebaseTracking.trackScreen(name: name, path: path);
+ }
+
+ void trackBeginCheckout(AnalyticsEventItem analyticsEventItem) {
+ _firebaseTracking.trackBeginCheckout(analyticsEventItem);
+ }
+
+ void trackAddToCart(AnalyticsEventItem analyticsEventItem) {
+ _firebaseTracking.trackAddToCart(analyticsEventItem);
+ }
+
+ void trackRemoveFromCart(AnalyticsEventItem analyticsEventItem) {
+ _firebaseTracking.trackRemoveFromCard(analyticsEventItem);
+ }
+}
diff --git a/comwell_key_app/lib/tracking/models/analytics_event_item.dart b/comwell_key_app/lib/tracking/models/analytics_event_item.dart
new file mode 100644
index 00000000..9c9b4b5b
--- /dev/null
+++ b/comwell_key_app/lib/tracking/models/analytics_event_item.dart
@@ -0,0 +1,31 @@
+import 'package:comwell_key_app/utils/json.dart';
+import 'package:json_annotation/json_annotation.dart';
+
+part '../../.generated/tracking/models/analytics_event_item.g.dart';
+
+@JsonSerializable()
+class AnalyticsEventItem {
+ final String hotelName; // Currency of the items associated with the event, in 3-letter ISO 4217 format.
+ final String currency; // The monetary value of the event. Set value to the sum of (price * quantity) for all items in items. Don't include shipping or tax.
+ final num value; // The items for the event.
+ final String placement; // Where in the app flow this event happened
+ final Iterable<String> items; // The ID of the item. One of item_id or item_name is required.
+ final String itemId; // The name of the item. One of item_id or item_name is required.
+ final String itemName; // The monetary unit price of the item, in units of the specified currency parameter.
+ final num price; // Item quantity. If not set, quantity is set to 1.
+ final num quantity; // The hotel name associated with the event.
+
+ AnalyticsEventItem({
+ required this.hotelName,
+ required this.currency,
+ required this.value,
+ required this.placement,
+ required this.items,
+ required this.itemId,
+ required this.itemName,
+ required this.price,
+ required this.quantity,
+ });
+
+ Json toJson() => _$AnalyticsEventItemToJson(this);
+}
diff --git a/comwell_key_app/lib/tracking/trackers/firebase_tracker.dart b/comwell_key_app/lib/tracking/trackers/firebase_tracker.dart
new file mode 100644
index 00000000..b302230d
--- /dev/null
+++ b/comwell_key_app/lib/tracking/trackers/firebase_tracker.dart
@@ -0,0 +1,49 @@
+import 'package:firebase_analytics/firebase_analytics.dart';
+import 'package:flutter/foundation.dart';
+import '../models/analytics_event_item.dart' as comwell;
+
+class FirebaseTracker {
+ final _tracking = FirebaseAnalytics.instance;
+
+ void trackScreen({required String name, required String path}) {
+ _track(_eventScreenView, {
+ _paramScreenClass: name,
+ _paramScreenName: path,
+ });
+ }
+
+ void trackAddToCart(comwell.AnalyticsEventItem analyticsEventItem) {
+ _track(_eventAddToCart, analyticsEventItem.toJson());
+ }
+
+ void trackRemoveFromCard(comwell.AnalyticsEventItem analyticsEventItem) {
+ _track(_eventRemoveFromCart, analyticsEventItem.toJson());
+ }
+
+ void trackBeginCheckout(comwell.AnalyticsEventItem analyticsEventItem) {
+ _track(_eventBeginCheckout, analyticsEventItem.toJson());
+ }
+
+ void _track(String name, Map<String, dynamic> parameters) async {
+ try {
+ await _tracking.logEvent(
+ name: name,
+ parameters: (parameters as Map<String, Object>?) ?? <String, Object>{},
+ );
+ } catch (e) {
+ if (kDebugMode) {
+ debugPrint("qqq tracking error=$e");
+ }
+ }
+ }
+
+ // event names
+ static const _eventScreenView = "screen_view";
+ static const _eventAddToCart = "add_to_cart";
+ static const _eventRemoveFromCart = "remove_from_cart";
+ static const _eventBeginCheckout = "begin_checkout";
+
+ // params
+ static const _paramScreenClass = "screen_class";
+ static const _paramScreenName = "screen_name";
+}
diff --git a/comwell_key_app/lib/utils/locator.dart b/comwell_key_app/lib/utils/locator.dart
index eb4df319..1f1a10de 100644
--- a/comwell_key_app/lib/utils/locator.dart
+++ b/comwell_key_app/lib/utils/locator.dart
@@ -6,7 +6,7 @@ import 'package:comwell_key_app/overview/repository/overview_repository.dart';
import 'package:comwell_key_app/pregistration/pregistration_repository.dart';
import 'package:comwell_key_app/profile/profile_repository.dart';
import 'package:comwell_key_app/profile_settings/repostiory/profile_settings_repository.dart';
-import 'package:comwell_key_app/tracking/FirebaseTracking.dart';
+import 'package:comwell_key_app/tracking/trackers/firebase_tracker.dart';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/foundation.dart';
import 'package:get_it/get_it.dart';
@@ -30,7 +30,7 @@ void setupLocator() {
locator.registerFactory<ProfileRepository>(() => ProfileRepository());
locator.registerFactory<PreregistrationRepository>(
() => PreregistrationRepository());
- locator.registerSingleton<FirebaseTracking>(FirebaseTracking());
+ locator.registerSingleton<FirebaseTracker>(FirebaseTracker());
locator.registerSingleton<ComwellDatabase>(ComwellDatabase());
}
}