-
Notifications
You must be signed in to change notification settings - Fork 54
Enhance Firebase Analytics Integration: screen() & identify() Support Parameters and Traits Mapping #174
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
Open
mohamedzakaria974
wants to merge
10
commits into
segmentio:main
Choose a base branch
from
mohamedzakaria974:fix/screen_view_params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Enhance Firebase Analytics Integration: screen() & identify() Support Parameters and Traits Mapping #174
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a73915e
fix: screen view event parameters
mohamedzakaria974 b132670
Merge remote-tracking branch 'upstream/main' into fix/screen_view_params
mohamedzakaria974 d530dfe
upgrade firebase dependencies
mohamedzakaria974 6403652
fix: screen view event parameters
mohamedzakaria974 079590c
upgrade firebase dependencies
mohamedzakaria974 541f203
Merge branch 'fix/screen_view_params' of https://github.com/mohamedza…
mohamedzakaria974 60e8dc7
Merge remote-tracking branch 'upstream/main' into fix/screen_view_params
mohamedzakaria974 4e14954
fix: AnalyticsEventItemJson fromJson
mohamedzakaria974 ae67e6c
Merge branch 'main' into fix/screen_view_params
MichaelGHSeg de06bfa
Merge branch 'main' into fix/screen_view_params
MichaelGHSeg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,15 @@ | ||
| library analytics_plugin_firebase; | ||
|
|
||
| import 'package:firebase_analytics/firebase_analytics.dart'; | ||
| import 'package:firebase_core/firebase_core.dart' show FirebaseOptions, Firebase; | ||
| import 'package:segment_analytics/event.dart'; | ||
| import 'package:segment_analytics/logger.dart'; | ||
| import 'package:segment_analytics/plugin.dart'; | ||
| import 'package:segment_analytics/map_transform.dart'; | ||
|
|
||
| import 'package:firebase_analytics/firebase_analytics.dart'; | ||
| import 'package:firebase_core/firebase_core.dart' | ||
| show FirebaseOptions, Firebase; | ||
|
|
||
| export 'package:firebase_core/firebase_core.dart' | ||
| show FirebaseOptions, Firebase; | ||
|
|
||
| import 'package:segment_analytics/plugin.dart'; | ||
| import 'package:segment_analytics_plugin_firebase/properties.dart'; | ||
|
|
||
| export 'package:firebase_core/firebase_core.dart' show FirebaseOptions, Firebase; | ||
|
|
||
| class FirebaseDestination extends DestinationPlugin { | ||
| final Future<void> firebaseFuture; | ||
|
|
||
|
|
@@ -27,15 +23,26 @@ class FirebaseDestination extends DestinationPlugin { | |
|
|
||
| @override | ||
| Future<RawEvent?> identify(IdentifyEvent event) async { | ||
| // Set user ID if provided | ||
| if (event.userId != null) { | ||
| await FirebaseAnalytics.instance.setUserId(id: event.userId!); | ||
| await FirebaseAnalytics.instance.setUserId(id: event.userId); | ||
| } | ||
|
|
||
| // Set user properties from traits if provided | ||
| if (event.traits != null) { | ||
| await Future.wait(event.traits!.toJson().entries.map((entry) async { | ||
| await FirebaseAnalytics.instance | ||
| .setUserProperty(name: entry.key, value: entry.value.toString()); | ||
| })); | ||
| // Transform and cast traits to the required format | ||
| final transformedTraits = recurseMapper(event.traits?.toJson(), mappings); | ||
| final userProperties = castParameterType(transformedTraits as Map<String, Object?>); | ||
|
|
||
| // Set each user property individually | ||
| for (final entry in userProperties.entries) { | ||
| await FirebaseAnalytics.instance.setUserProperty( | ||
| name: entry.key, | ||
| value: entry.value.toString(), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return event; | ||
| } | ||
|
|
||
|
|
@@ -48,44 +55,37 @@ class FirebaseDestination extends DestinationPlugin { | |
| switch (event.event) { | ||
| case 'Product Clicked': | ||
| if (!(properties.containsKey('list_id') || | ||
| properties.containsKey('list_name') || | ||
| properties.containsKey('list_name') || | ||
| properties.containsKey('name') || | ||
| properties.containsKey('itemId')) ) { | ||
| properties.containsKey('itemId'))) { | ||
| throw Exception("Missing properties: list_name, list_id, name and itemID"); | ||
| } | ||
|
|
||
| AnalyticsEventItem itemClicked = AnalyticsEventItem( | ||
| itemName: properties['name'].toString(), | ||
| itemId: properties['itemId'].toString()); | ||
| AnalyticsEventItem itemClicked = | ||
| AnalyticsEventItem(itemName: properties['name'].toString(), itemId: properties['itemId'].toString()); | ||
|
|
||
| await FirebaseAnalytics.instance.logSelectItem( | ||
| itemListName: properties['list_name'].toString(), | ||
| itemListId: properties['list_id'].toString(), | ||
| items:[itemClicked], | ||
| itemListName: properties['list_name'].toString(), | ||
| itemListId: properties['list_id'].toString(), | ||
| items: [itemClicked], | ||
| ); | ||
| break; | ||
| case 'Product Viewed': | ||
| await FirebaseAnalytics.instance.logViewItem( | ||
| currency: properties["currency"]?.toString(), | ||
| items: event.properties == null | ||
| ? null | ||
| : [AnalyticsEventItemJson(event.properties!)], | ||
| items: event.properties == null ? null : [AnalyticsEventItemJson(event.properties!)], | ||
| value: double.tryParse(properties["value"].toString())); | ||
| break; | ||
| case 'Product Added': | ||
| await FirebaseAnalytics.instance.logAddToCart( | ||
| currency: properties["currency"]?.toString(), | ||
| items: event.properties == null | ||
| ? null | ||
| : [AnalyticsEventItemJson(event.properties!)], | ||
| items: event.properties == null ? null : [AnalyticsEventItemJson(event.properties!)], | ||
| value: double.tryParse(properties["value"].toString())); | ||
| break; | ||
| case 'Product Removed': | ||
| await FirebaseAnalytics.instance.logRemoveFromCart( | ||
| currency: properties["currency"]?.toString(), | ||
| items: event.properties == null | ||
| ? null | ||
| : [AnalyticsEventItemJson(event.properties!)], | ||
| items: event.properties == null ? null : [AnalyticsEventItemJson(event.properties!)], | ||
| value: double.tryParse(properties["value"].toString())); | ||
| break; | ||
| case 'Checkout Started': | ||
|
|
@@ -100,7 +100,7 @@ class FirebaseDestination extends DestinationPlugin { | |
| creativeName: properties["creativeName"]?.toString(), | ||
| creativeSlot: properties["creativeSlot"]?.toString(), | ||
| items: properties["items"] as List<AnalyticsEventItemJson>, | ||
| locationId: properties["locationdId"]?.toString(), | ||
| locationId: properties["locationId"]?.toString(), | ||
| promotionId: properties["promotionId"]?.toString(), | ||
| promotionName: properties["promotionName"]?.toString()); | ||
| break; | ||
|
|
@@ -147,12 +147,10 @@ class FirebaseDestination extends DestinationPlugin { | |
| value: double.tryParse(properties["value"].toString())); | ||
| break; | ||
| case 'Cart Shared': | ||
| if (event.properties == null || | ||
| event.properties!['products'] == null) { | ||
| if (event.properties == null || event.properties!['products'] == null) { | ||
| log("Error tracking event '${event.event}' for Firebase: products property must be a list of products"); | ||
| } else if (event.properties!['products'] is List) { | ||
| await Future.wait( | ||
| (event.properties!['products'] as List).map((product) async { | ||
| await Future.wait((event.properties!['products'] as List).map((product) async { | ||
| final productProperties = mapProperties(product, mappings); | ||
| if (productProperties.containsKey("contentType") && | ||
| productProperties.containsKey("itemId") && | ||
|
|
@@ -189,20 +187,18 @@ class FirebaseDestination extends DestinationPlugin { | |
| searchTerm: properties["searchTerm"].toString(), | ||
| destination: properties["destination"]?.toString(), | ||
| endDate: properties["endDate"]?.toString(), | ||
| numberOfNights: | ||
| int.tryParse(properties["numberOfNights"].toString()), | ||
| numberOfPassengers: | ||
| int.tryParse(properties["numberOfPassengers"].toString()), | ||
| numberOfRooms: | ||
| int.tryParse(properties["numberOfRooms"].toString()), | ||
| numberOfNights: int.tryParse(properties["numberOfNights"].toString()), | ||
| numberOfPassengers: int.tryParse(properties["numberOfPassengers"].toString()), | ||
| numberOfRooms: int.tryParse(properties["numberOfRooms"].toString()), | ||
| origin: properties["origin"]?.toString(), | ||
| startDate: properties["startDate"]?.toString(), | ||
| travelClass: properties["travelClass"]?.toString()); | ||
| break; | ||
| default: | ||
| await FirebaseAnalytics.instance.logEvent( | ||
| name: sanitizeEventName(event.event), | ||
| parameters: castParameterType(properties)); | ||
| name: sanitizeEventName(event.event), | ||
| parameters: castParameterType(properties), | ||
| ); | ||
| break; | ||
| } | ||
| } catch (error) { | ||
|
|
@@ -213,8 +209,15 @@ class FirebaseDestination extends DestinationPlugin { | |
|
|
||
| @override | ||
| Future<RawEvent?> screen(ScreenEvent event) async { | ||
| FirebaseAnalytics.instance | ||
| .logScreenView(screenClass: event.name, screenName: event.name); | ||
| // Transform and cast properties to the required format | ||
| final transformedProperties = recurseMapper(event.properties, mappings); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| final parameters = castParameterType(transformedProperties as Map<String, Object?>); | ||
|
|
||
| FirebaseAnalytics.instance.logScreenView( | ||
| screenClass: event.name, | ||
| screenName: event.name, | ||
| parameters: parameters, | ||
| ); | ||
| return event; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mappingswas intended for product properties and contains thing likename -> itemNamethat might not be appropriate everywhere. Was there a mapping in particular you were looking to have here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You’re right that the original mappings were introduced mainly for product properties (e.g. name -> itemName).
However, in this case the purpose of the mapping isn’t just renaming fields — it’s to align our event payload with Firebase Analytics’ expected schema and functionality.
Firebase defines specific parameter names and structures for things like items, item_name, item_id, etc. If we deviate from that or simplify it too much, we risk:
So the mapping here should exist to match Firebase’s required/expected format, not to abstract or reduce it.
If we remove or oversimplify the mapping layer, we’re effectively weakening what Firebase already provides out of the box.
If there’s a specific field you feel shouldn’t be mapped this way, let me know and we can review it — but the intent here is to preserve Firebase functionality, not override it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our approach in our other firebase plugins is usually not to map screen and identify properties, and this change seems likely to break other users existing implementations. The other changes look good and I'd like to merge them.
I think the best solution here is for you branch the project and customize it for your case (which I think you're already doing) and we'll take the more general fixes.
If you want to modify this PR we can merge it, or I can branch it and make the changes myself. Whatever works for you.
Thanks!
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your reply, but it shouldn't break anything as it's missing the event parameters as it's already passed from the caller method.
It just adds the missing parts, and if the users don't pass it, it will remain works as you see in line 211 the ScreenEvent did not be touched