Skip to content

Commit

Permalink
feat(analytics): Adding default event parameters (#5246)
Browse files Browse the repository at this point in the history
* Adding firebase default parameter function call to android
* iOS implementation
* e2e tests

Co-authored-by: Mike Hardy <github@mikehardy.net>
  • Loading branch information
MFrat and mikehardy authored May 2, 2021
1 parent 3f95171 commit 684bb50
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/analytics/__tests__/analytics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ describe('Analytics', function () {
});
});

describe('setDefaultEventParameters()', function () {
it('errors if params is not a object', function () {
// @ts-ignore test
expect(() => firebase.analytics().setDefaultEventParameters('123')).toThrowError(
"firebase.analytics().setDefaultEventParameters(*) 'params' expected an object value when it is defined.",
);
});
});

describe('logAddToCart()', function () {
it('errors if param is not an object', function () {
// @ts-ignore test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,11 @@ Task<Void> resetAnalyticsData() {
return null;
});
}

Task<Void> setDefaultEventParameters(Bundle parameters) {
return Tasks.call(() -> {
FirebaseAnalytics.getInstance(getContext()).setDefaultEventParameters(parameters);
return null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,17 @@ public void resetAnalyticsData(Promise promise) {
});
}

@ReactMethod
public void setDefaultEventParameters(@Nullable ReadableMap params, Promise promise) {
module.setDefaultEventParameters(toBundle(params)).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
promise.resolve(task.getResult());
} else {
rejectPromiseWithExceptionMap(promise, task.getException());
}
});
}

private Bundle toBundle(ReadableMap readableMap) {
Bundle bundle = Arguments.toBundle(readableMap);
if (bundle == null) {
Expand Down
14 changes: 14 additions & 0 deletions packages/analytics/e2e/analytics.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,18 @@ describe('analytics()', function () {
});
});
});

describe('setDefaultEventParameters()', function () {
it('set null default parameter', async function () {
await firebase.analytics().setDefaultEventParameters(null);
});

it('set undefined default parameter', async function () {
await firebase.analytics().setDefaultEventParameters(undefined);
});

it('set default parameters', async function () {
await firebase.analytics().setDefaultEventParameters({ number: 1, stringn: '123' });
});
});
});
15 changes: 15 additions & 0 deletions packages/analytics/ios/RNFBAnalytics/RNFBAnalyticsModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,21 @@ - (dispatch_queue_t)methodQueue {
return resolve([FIRAnalytics appInstanceID]);
}

RCT_EXPORT_METHOD(setDefaultEventParameters:
(NSDictionary *) params
resolver:
(RCTPromiseResolveBlock) resolve
rejecter:
(RCTPromiseRejectBlock) reject) {
@try {
[FIRAnalytics setDefaultEventParameters:[self cleanJavascriptParams:params]];
} @catch (NSException *exception) {
return [RNFBSharedUtils rejectPromiseWithExceptionDict:reject exception:exception];
}

return resolve([NSNull null]);
}

#pragma mark -
#pragma mark Private methods

Expand Down
20 changes: 20 additions & 0 deletions packages/analytics/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,26 @@ export namespace FirebaseAnalyticsTypes {
* @param params See {@link analytics.ViewSearchResultsParameters}.
*/
logViewSearchResults(params: ViewSearchResultsParameters): Promise<void>;

/**
* Adds parameters that will be set on every event logged from the SDK, including automatic ones.
*
* #### Example
*
* ```js
* await firebase.analytics().setDefaultEventParameters({
* userId: '1234',
* });
* ```
*
*
* @param params Parameters to be added to the map of parameters added to every event.
* They will be added to the map of default event parameters, replacing any existing
* parameter with the same name. Valid parameter values are String, long, and double.
* Setting a key's value to null will clear that parameter. Passing in a null bundle
* will clear all parameters.
*/
setDefaultEventParameters(params?: { [key: string]: any }): Promise<void>;
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/analytics/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,16 @@ class FirebaseAnalyticsModule extends FirebaseModule {
),
);
}

setDefaultEventParameters(params) {
if (!isObject(params) && !isNull(params) && !isUndefined(params)) {
throw new Error(
"firebase.analytics().setDefaultEventParameters(*) 'params' expected an object value when it is defined.",
);
}

return this.native.setDefaultEventParameters(params);
}
}

// import { SDK_VERSION } from '@react-native-firebase/analytics';
Expand Down

1 comment on commit 684bb50

@vercel
Copy link

@vercel vercel bot commented on 684bb50 May 2, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.