Visitar URL original
feat(analytics): add support for items in logEvent (#18097) · firebase/flutterfire@2b8517c · GitHub
Skip to content

Commit 2b8517c

Browse files
authored
feat(analytics): add support for items in logEvent (#18097)
1 parent 8a2ed9f commit 2b8517c

4 files changed

Lines changed: 54 additions & 5 deletions

File tree

‎packages/firebase_analytics/firebase_analytics/android/src/main/kotlin/io/flutter/plugins/firebase/analytics/FlutterFirebaseAnalyticsPlugin.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -310,11 +310,11 @@ class FlutterFirebaseAnalyticsPlugin : FlutterFirebasePlugin,
310310
} else if (value == null) {
311311
bundle.putString(key, null)
312312
} else if (value is Iterable<*>) {
313-
val list = ArrayList<Parcelable?>()
313+
val list = mutableListOf<Parcelable>()
314314

315315
for (item in value) {
316316
if (item is Map<*, *>) {
317-
list.add(createBundleFromMap(item as Map<String, Any>))
317+
createBundleFromMap(item as Map<String, Any>)?.let { list.add(it) }
318318
} else {
319319
if (item != null) {
320320
throw IllegalArgumentException(
@@ -327,7 +327,7 @@ class FlutterFirebaseAnalyticsPlugin : FlutterFirebasePlugin,
327327
}
328328
}
329329

330-
bundle.putParcelableArrayList(key, list)
330+
bundle.putParcelableArray(key, list.toTypedArray())
331331
} else if (value is Map<*, *>) {
332332
bundle.putParcelable(key, createBundleFromMap(value as Map<String, Any>))
333333
} else {

‎packages/firebase_analytics/firebase_analytics/lib/src/firebase_analytics.dart‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,27 @@ class FirebaseAnalytics extends FirebasePluginPlatform {
100100
Future<void> logEvent({
101101
required String name,
102102
Map<String, Object>? parameters,
103+
List<AnalyticsEventItem>? items,
103104
AnalyticsCallOptions? callOptions,
104105
}) async {
105106
_logEventNameValidation(name);
106107

107108
_assertParameterTypesAreCorrect(parameters);
109+
_assertItemsParameterTypesAreCorrect(items);
110+
111+
final Map<String, Object>? effectiveParameters;
112+
if (items != null) {
113+
effectiveParameters = filterOutNulls(<String, Object?>{
114+
_ITEMS: _marshalItems(items),
115+
if (parameters != null) ...parameters,
116+
});
117+
} else {
118+
effectiveParameters = parameters;
119+
}
108120

109121
await _delegate.logEvent(
110122
name: name,
111-
parameters: parameters,
123+
parameters: effectiveParameters,
112124
callOptions: callOptions,
113125
);
114126
}

‎packages/firebase_analytics/firebase_analytics/test/firebase_analytics_test.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,21 @@ void main() {
189189
value: 123.90,
190190
);
191191
});
192+
193+
test('logEvent with items rejects invalid item parameter types', () {
194+
expect(
195+
() => analytics!.logEvent(
196+
name: 'custom_event',
197+
items: [
198+
AnalyticsEventItem(
199+
itemId: 'id',
200+
parameters: {'invalid': true},
201+
),
202+
],
203+
),
204+
throwsA(isA<AssertionError>()),
205+
);
206+
});
192207
});
193208

194209
group('filter out nulls', () {

‎tests/integration_test/firebase_analytics/firebase_analytics_e2e_test.dart‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,35 @@ void main() {
102102
parameters: {
103103
'foo': 'bar',
104104
'baz': 500,
105-
// Lists are not supported
105+
// Lists are not supported in parameters
106106
'items': [analyticsEventItem],
107107
},
108108
),
109109
throwsA(isA<AssertionError>()),
110110
);
111111

112+
// test logEvent with typed items parameter
113+
await expectLater(
114+
FirebaseAnalytics.instance.logEvent(
115+
name: 'testing-items',
116+
items: [analyticsEventItem],
117+
),
118+
completes,
119+
);
120+
121+
// test logEvent with both items and parameters
122+
await expectLater(
123+
FirebaseAnalytics.instance.logEvent(
124+
name: 'testing-items-and-parameters',
125+
items: [analyticsEventItem],
126+
parameters: {
127+
'foo': 'bar',
128+
'baz': 500,
129+
},
130+
),
131+
completes,
132+
);
133+
112134
// test 3 reserved events
113135
await expectLater(
114136
FirebaseAnalytics.instance.logAdImpression(

0 commit comments

Comments
 (0)