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

feat(firebase_analytics): update logEvent() & setDefaultParameters() to assert input types. #9520

Merged
merged 21 commits into from Dec 29, 2022

Conversation

russellwheatley
Copy link
Member

@russellwheatley russellwheatley commented Sep 12, 2022

Description

This PR is designed to avoid confusion about what values are accepted when logging a custom event via logEvent(). I've implemented an assert on the values passed to logEvent which throws when the parameter values are not String or num. The same for setDefaultEventParameters() except it also allows parameter values to be null which resets that value as a default parameter.

The Firebase documentation states that the only values accepted are strings and numbers:

From android documentation (see bold):

The map of event parameters. Passing null indicates that the event has no parameters. Parameter names can be up to 40 characters long and must start with an alphabetic character and contain only alphanumeric characters and underscores. String, long and double param types are supported. String parameter values can be up to 100 characters long. The "firebase_", "google_" and "ga_" prefixes are reserved and should not be used for parameter names.

I've also tested on web, iOS and android:

android:
Successful custom event without items list:
android successful custom event  No item

Unsuccessful custom event including items list (Note error code 21):
android unsuccessful event  with item

iOS:
Successful custom event without items list:
iOS successful custom event  no item

Unsuccessful custom event including items list (Note error code 21):
iOS unsuccessful custom event  with item

Web:
Web is slightly different as it uses a Chrome extension to debug. It does pass the items list in the request for a custom event, but it appears to stringify the value. See:

logEvent(
  analytics, 
  'test_event',
  { some_values: [{some:'thing'}, 3, 'hello'] }
);

The result in the debugger console for some_values looks like this:

ep.some_values=%5Bobject%20Object%5D%2C3%2Chello&_et=3

Once decoded, it shows the value as one string (including the object within the array, hence [object Object])

[object Object],3,hello&_et=3

setDefaultEventParameters

I've also changed Int to Long for the event parameters bundle in android. The documentation for android says Long & Double only. If you try to add Int when running setDefaultEventParameters() you will get the following warning in the console logs:

W/FA: Invalid default event parameter type. Name, value: someInt, 400
W/FA: Invalid default event parameter type. Name, value: longNumber, 230485
W/FA: Invalid default event parameter type. Name, value: inter, 9

It also doesn't show up in the debugger console.
Screenshot 2022-12-09 at 14 05 17

When I changed putInt() to putLong(), the warnings disappear and the default parameters appear in the console:
Screenshot 2022-12-09 at 14 07 46

TODO

Related Issues

closes #8861, #9698

Other related issues which highlight confusion:
#4353
#2835

Replace this paragraph with a list of issues related to this PR from the issue database. Indicate, which of these issues are resolved or fixed by this PR. Note that you'll have to prefix the issue numbers with flutter/flutter#.

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

// Only strings and numbers (ints & doubles) are supported for GA custom event parameters:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets#overview
'bool': true.toString(),
'items': [itemCreator()]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have items anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not for custom events (logEvent()). See the description for why 😄

Map<String, Object> _parameters = {};

// Can only pass either a String or a num value
EventParameters addParameter(String key, {String? string, num? number}) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, this is not much better. We are allowing the consumer to pass both of them, and he's going to have a runtime exception if he does it. We could make a safe API that wouldn't allow the user to pass any kind of invalid data, so that, if it compiles, it works.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair point. I think I will change addParameter to addString & addNumber to improve it.

I will be keeping the fromMap() function, as its main purpose is to help transition to the new EventParameters API, and it will still throw assert on incorrect property values before it ever makes it to the native side.

'double': 42.0,
// Only strings and numbers (ints & doubles) are supported for GA custom event parameters:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/custom-dims-mets#overview
'bool': true.toString(),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT about to introduce an addBool method, which behind the scenes saves it as value.toString()?

This way we might support most of these base types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer not as it isn't stored as a boolean and hence misleading, true.toString() is painless enough.

@russellwheatley russellwheatley changed the title feat(firebase_analytics)!: update logEvent() so it can only accept strings and numbers feat(firebase_analytics)!: update logEvent() & setDefaultParameters() accepted input types. Dec 8, 2022
@russellwheatley russellwheatley marked this pull request as ready for review December 9, 2022 14:51
@russellwheatley russellwheatley changed the title feat(firebase_analytics)!: update logEvent() & setDefaultParameters() accepted input types. feat(firebase_analytics): update logEvent() & setDefaultParameters() to assert input types. Dec 23, 2022
@russellwheatley russellwheatley merged commit bac87e9 into master Dec 29, 2022
@russellwheatley russellwheatley deleted the @russell/analytics-8861/2 branch December 29, 2022 10:26
@mockturtl
Copy link
Contributor

From android documentation (see bold):

The map of event parameters. Passing null indicates that the event has no parameters. Parameter names can be up to 40 characters long and must start with an alphabetic character and contain only alphanumeric characters and underscores. String, long and double param types are supported. String parameter values can be up to 100 characters long. The "firebase_", "google_" and "ga_" prefixes are reserved and should not be used for parameter names.

Source: https://developers.google.com/android/reference/com/google/firebase/analytics/FirebaseAnalytics#logEvent(java.lang.String,%20android.os.Bundle)

@firebase firebase locked and limited conversation to collaborators Jan 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 [firebase_analytics] Error when trying to send a list when logging event
6 participants