11library analytics_plugin_firebase;
22
3+ import 'package:firebase_analytics/firebase_analytics.dart' ;
4+ import 'package:firebase_core/firebase_core.dart' show FirebaseOptions, Firebase;
35import 'package:segment_analytics/event.dart' ;
46import 'package:segment_analytics/logger.dart' ;
5- import 'package:segment_analytics/plugin.dart' ;
67import 'package:segment_analytics/map_transform.dart' ;
7-
8- import 'package:firebase_analytics/firebase_analytics.dart' ;
9- import 'package:firebase_core/firebase_core.dart'
10- show FirebaseOptions, Firebase;
11-
12- export 'package:firebase_core/firebase_core.dart'
13- show FirebaseOptions, Firebase;
14-
8+ import 'package:segment_analytics/plugin.dart' ;
159import 'package:segment_analytics_plugin_firebase/properties.dart' ;
1610
11+ export 'package:firebase_core/firebase_core.dart' show FirebaseOptions, Firebase;
12+
1713class FirebaseDestination extends DestinationPlugin {
1814 final Future <void > firebaseFuture;
1915
@@ -27,15 +23,26 @@ class FirebaseDestination extends DestinationPlugin {
2723
2824 @override
2925 Future <RawEvent ?> identify (IdentifyEvent event) async {
26+ // Set user ID if provided
3027 if (event.userId != null ) {
31- await FirebaseAnalytics .instance.setUserId (id: event.userId! );
28+ await FirebaseAnalytics .instance.setUserId (id: event.userId);
3229 }
30+
31+ // Set user properties from traits if provided
3332 if (event.traits != null ) {
34- await Future .wait (event.traits! .toJson ().entries.map ((entry) async {
35- await FirebaseAnalytics .instance
36- .setUserProperty (name: entry.key, value: entry.value.toString ());
37- }));
33+ // Transform and cast traits to the required format
34+ final transformedTraits = recurseMapper (event.traits? .toJson (), mappings);
35+ final userProperties = castParameterType (transformedTraits as Map <String , Object ?>);
36+
37+ // Set each user property individually
38+ for (final entry in userProperties.entries) {
39+ await FirebaseAnalytics .instance.setUserProperty (
40+ name: entry.key,
41+ value: entry.value.toString (),
42+ );
43+ }
3844 }
45+
3946 return event;
4047 }
4148
@@ -48,44 +55,37 @@ class FirebaseDestination extends DestinationPlugin {
4855 switch (event.event) {
4956 case 'Product Clicked' :
5057 if (! (properties.containsKey ('list_id' ) ||
51- properties.containsKey ('list_name' ) ||
58+ properties.containsKey ('list_name' ) ||
5259 properties.containsKey ('name' ) ||
53- properties.containsKey ('itemId' )) ) {
60+ properties.containsKey ('itemId' ))) {
5461 throw Exception ("Missing properties: list_name, list_id, name and itemID" );
5562 }
5663
57- AnalyticsEventItem itemClicked = AnalyticsEventItem (
58- itemName: properties['name' ].toString (),
59- itemId: properties['itemId' ].toString ());
64+ AnalyticsEventItem itemClicked =
65+ AnalyticsEventItem (itemName: properties['name' ].toString (), itemId: properties['itemId' ].toString ());
6066
6167 await FirebaseAnalytics .instance.logSelectItem (
62- itemListName: properties['list_name' ].toString (),
63- itemListId: properties['list_id' ].toString (),
64- items: [itemClicked],
68+ itemListName: properties['list_name' ].toString (),
69+ itemListId: properties['list_id' ].toString (),
70+ items: [itemClicked],
6571 );
6672 break ;
6773 case 'Product Viewed' :
6874 await FirebaseAnalytics .instance.logViewItem (
6975 currency: properties["currency" ]? .toString (),
70- items: event.properties == null
71- ? null
72- : [AnalyticsEventItemJson (event.properties! )],
76+ items: event.properties == null ? null : [AnalyticsEventItemJson (event.properties! )],
7377 value: double .tryParse (properties["value" ].toString ()));
7478 break ;
7579 case 'Product Added' :
7680 await FirebaseAnalytics .instance.logAddToCart (
7781 currency: properties["currency" ]? .toString (),
78- items: event.properties == null
79- ? null
80- : [AnalyticsEventItemJson (event.properties! )],
82+ items: event.properties == null ? null : [AnalyticsEventItemJson (event.properties! )],
8183 value: double .tryParse (properties["value" ].toString ()));
8284 break ;
8385 case 'Product Removed' :
8486 await FirebaseAnalytics .instance.logRemoveFromCart (
8587 currency: properties["currency" ]? .toString (),
86- items: event.properties == null
87- ? null
88- : [AnalyticsEventItemJson (event.properties! )],
88+ items: event.properties == null ? null : [AnalyticsEventItemJson (event.properties! )],
8989 value: double .tryParse (properties["value" ].toString ()));
9090 break ;
9191 case 'Checkout Started' :
@@ -147,12 +147,10 @@ class FirebaseDestination extends DestinationPlugin {
147147 value: double .tryParse (properties["value" ].toString ()));
148148 break ;
149149 case 'Cart Shared' :
150- if (event.properties == null ||
151- event.properties! ['products' ] == null ) {
150+ if (event.properties == null || event.properties! ['products' ] == null ) {
152151 log ("Error tracking event '${event .event }' for Firebase: products property must be a list of products" );
153152 } else if (event.properties! ['products' ] is List ) {
154- await Future .wait (
155- (event.properties! ['products' ] as List ).map ((product) async {
153+ await Future .wait ((event.properties! ['products' ] as List ).map ((product) async {
156154 final productProperties = mapProperties (product, mappings);
157155 if (productProperties.containsKey ("contentType" ) &&
158156 productProperties.containsKey ("itemId" ) &&
@@ -189,20 +187,18 @@ class FirebaseDestination extends DestinationPlugin {
189187 searchTerm: properties["searchTerm" ].toString (),
190188 destination: properties["destination" ]? .toString (),
191189 endDate: properties["endDate" ]? .toString (),
192- numberOfNights:
193- int .tryParse (properties["numberOfNights" ].toString ()),
194- numberOfPassengers:
195- int .tryParse (properties["numberOfPassengers" ].toString ()),
196- numberOfRooms:
197- int .tryParse (properties["numberOfRooms" ].toString ()),
190+ numberOfNights: int .tryParse (properties["numberOfNights" ].toString ()),
191+ numberOfPassengers: int .tryParse (properties["numberOfPassengers" ].toString ()),
192+ numberOfRooms: int .tryParse (properties["numberOfRooms" ].toString ()),
198193 origin: properties["origin" ]? .toString (),
199194 startDate: properties["startDate" ]? .toString (),
200195 travelClass: properties["travelClass" ]? .toString ());
201196 break ;
202197 default :
203198 await FirebaseAnalytics .instance.logEvent (
204- name: sanitizeEventName (event.event),
205- parameters: castParameterType (properties));
199+ name: sanitizeEventName (event.event),
200+ parameters: castParameterType (properties),
201+ );
206202 break ;
207203 }
208204 } catch (error) {
@@ -213,8 +209,15 @@ class FirebaseDestination extends DestinationPlugin {
213209
214210 @override
215211 Future <RawEvent ?> screen (ScreenEvent event) async {
216- FirebaseAnalytics .instance
217- .logScreenView (screenClass: event.name, screenName: event.name);
212+ // Transform and cast properties to the required format
213+ final transformedProperties = recurseMapper (event.properties, mappings);
214+ final parameters = castParameterType (transformedProperties as Map <String , Object ?>);
215+
216+ FirebaseAnalytics .instance.logScreenView (
217+ screenClass: event.name,
218+ screenName: event.name,
219+ parameters: parameters,
220+ );
218221 return event;
219222 }
220223
0 commit comments