Skip to content

Commit

Permalink
feat(firebase_analytics): update logEvent() & `setDefaultParameters…
Browse files Browse the repository at this point in the history
…()` to assert input types. (#9520)
  • Loading branch information
russellwheatley committed Dec 29, 2022
1 parent c44f4ba commit bac87e9
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 82 deletions.
Expand Up @@ -53,7 +53,8 @@ private static Bundle createBundleFromMap(Map<String, Object> map) {
if (value instanceof String) {
bundle.putString(key, (String) value);
} else if (value instanceof Integer) {
bundle.putInt(key, (Integer) value);
// FirebaseAnalytics default event parameters only support long and double types, so we convert the int to a long.
bundle.putLong(key, (Integer) value);
} else if (value instanceof Long) {
bundle.putLong(key, (Long) value);
} else if (value instanceof Double) {
Expand Down
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';

import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'firebase_options.dart';
Expand Down Expand Up @@ -68,7 +69,28 @@ class _MyHomePageState extends State<MyHomePage> {
});
}

Future<void> _setDefaultEventParameters() async {
if (kIsWeb) {
setMessage(
'"setDefaultEventParameters()" is not supported on web platform',
);
} else {
// Only strings, numbers & null (longs & doubles for android, ints and doubles for iOS) are supported for default event parameters:
await widget.analytics.setDefaultEventParameters(<String, dynamic>{
'string': 'string',
'int': 42,
'long': 12345678910,
'double': 42.0,
'bool': true.toString(),
});
setMessage('setDefaultEventParameters succeeded');
}
}

Future<void> _sendAnalyticsEvent() async {
// Only strings and numbers (longs & doubles for android, ints and doubles for iOS) are supported for GA custom event parameters:
// https://firebase.google.com/docs/reference/ios/firebaseanalytics/api/reference/Classes/FIRAnalytics#+logeventwithname:parameters:
// https://firebase.google.com/docs/reference/android/com/google/firebase/analytics/FirebaseAnalytics#public-void-logevent-string-name,-bundle-params
await widget.analytics.logEvent(
name: 'test_event',
parameters: <String, dynamic>{
Expand All @@ -79,9 +101,9 @@ class _MyHomePageState extends State<MyHomePage> {
// 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()]
},
);

setMessage('logEvent succeeded');
}

Expand Down Expand Up @@ -327,6 +349,10 @@ class _MyHomePageState extends State<MyHomePage> {
onPressed: _testResetAnalyticsData,
child: const Text('Test resetAnalyticsData'),
),
MaterialButton(
onPressed: _setDefaultEventParameters,
child: const Text('Test setDefaultEventParameters'),
),
Text(
_message,
style: const TextStyle(color: Color.fromARGB(255, 0, 155, 0)),
Expand Down

0 comments on commit bac87e9

Please sign in to comment.