Skip to content

Commit

Permalink
fix(analytics): add missing quantity parameter to the Item structure (#…
Browse files Browse the repository at this point in the history
…4536)

* Add the missing quantity parameter to the Item structure
* Convert the quantity to integer if present in the elements of the items array
* Add quantity to an end-to-end test just to make sure that the new code doesn't crash the app
  • Loading branch information
Danchoys committed Nov 23, 2020
1 parent 22c615c commit f9935e7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@
*/


import android.os.Bundle;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.google.firebase.analytics.FirebaseAnalytics;

import java.util.ArrayList;

import javax.annotation.Nullable;

Expand All @@ -39,7 +44,7 @@ public class ReactNativeFirebaseAnalyticsModule extends ReactNativeFirebaseModul

@ReactMethod
public void logEvent(String name, @Nullable ReadableMap params, Promise promise) {
module.logEvent(name, Arguments.toBundle(params)).addOnCompleteListener(task -> {
module.logEvent(name, toBundle(params)).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
promise.resolve(task.getResult());
} else {
Expand Down Expand Up @@ -114,4 +119,19 @@ public void resetAnalyticsData(Promise promise) {
}
});
}

private Bundle toBundle(ReadableMap readableMap) {
Bundle bundle = Arguments.toBundle(readableMap);
if (bundle == null) {
return null;
}
ArrayList itemsArray = (ArrayList) bundle.getSerializable(FirebaseAnalytics.Param.ITEMS);
for (Object item : itemsArray != null ? itemsArray : new ArrayList()) {
if (item instanceof Bundle && ((Bundle) item).containsKey(FirebaseAnalytics.Param.QUANTITY)) {
double number = ((Bundle) item).getDouble(FirebaseAnalytics.Param.QUANTITY);
((Bundle) item).putInt(FirebaseAnalytics.Param.QUANTITY, (int) number);
}
}
return bundle;
}
}
1 change: 1 addition & 0 deletions packages/analytics/e2e/analytics.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe('analytics()', () => {
item_name: 'foo',
item_category: 'foo',
item_location_id: 'foo',
quantity: 5,
},
],
value: 123,
Expand Down
21 changes: 20 additions & 1 deletion packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ - (dispatch_queue_t)methodQueue {
rejecter:
(RCTPromiseRejectBlock) reject) {
@try {
[FIRAnalytics logEventWithName:name parameters:params];
[FIRAnalytics logEventWithName:name parameters:[self cleanJavascriptParams:params]];
} @catch (NSException *exception) {
return [RNFBSharedUtils rejectPromiseWithExceptionDict:reject exception:exception];
}
Expand Down Expand Up @@ -135,4 +135,23 @@ - (dispatch_queue_t)methodQueue {
return resolve([NSNull null]);
}

#pragma mark -
#pragma mark Private methods

- (NSDictionary *)cleanJavascriptParams:(NSDictionary *)params {
NSMutableDictionary *newParams = [params mutableCopy];
if (newParams[kFIRParameterItems]) {
NSMutableArray *newItems = [NSMutableArray array];
[(NSArray *)newParams[kFIRParameterItems] enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSMutableDictionary *item = [obj mutableCopy];
if (item[kFIRParameterQuantity]) {
item[kFIRParameterQuantity] = @([item[kFIRParameterQuantity] integerValue]);
}
[newItems addObject:[item copy]];
}];
newParams[kFIRParameterItems] = [newItems copy];
}
return [newParams copy];
}

@end
5 changes: 5 additions & 0 deletions packages/analytics/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,12 @@ export namespace FirebaseAnalyticsTypes {
* The Item variant.
*/
item_variant?: string;
/**
* The Item quantity.
*/
quantity?: number;
}

export interface AddPaymentInfoEventParameters {
items?: Item[];
/**
Expand Down
1 change: 1 addition & 0 deletions packages/analytics/lib/structs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const Item = struct({
item_list_name: 'string?',
item_location_id: 'string?',
item_variant: 'string?',
quantity: 'number?',
});

export const ScreenView = struct({
Expand Down

0 comments on commit f9935e7

Please sign in to comment.