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

fix(analytics): added missing quantity parameter to the Item structure #4536

Merged
merged 5 commits into from
Nov 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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];
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternative solution would be to transform the incoming dictionary into a JSON string, search and replace all numbers with zeroes after the dot with whole numbers and transform it back into an object, but obviously this may have some unexpected impact on other things...


@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