Skip to content

Commit 3a6995b

Browse files
authored
Make event params optional in logEvent (#2226)
1 parent 9be8285 commit 3a6995b

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

packages/analytics-types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface FirebaseAnalytics {
4646
*/
4747
logEvent(
4848
eventName: EventNameString,
49-
eventParams: EventParams,
49+
eventParams?: EventParams,
5050
options?: AnalyticsCallOptions
5151
): void;
5252

packages/analytics/src/functions.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ describe('FirebaseAnalytics methods', () => {
5151
);
5252
});
5353

54+
it('logEvent() with no event params calls gtag function correctly', () => {
55+
logEvent(gtagStub, analyticsId, EventName.VIEW_ITEM);
56+
57+
expect(gtagStub).to.have.been.calledWith(
58+
GtagCommand.EVENT,
59+
EventName.VIEW_ITEM,
60+
{
61+
'send_to': analyticsId
62+
}
63+
);
64+
});
65+
5466
it('logEvent() globally calls gtag function correctly', () => {
5567
logEvent(
5668
gtagStub,
@@ -71,6 +83,18 @@ describe('FirebaseAnalytics methods', () => {
7183
);
7284
});
7385

86+
it('logEvent() with no event params globally calls gtag function correctly', () => {
87+
logEvent(gtagStub, analyticsId, EventName.ADD_TO_CART, undefined, {
88+
global: true
89+
});
90+
91+
expect(gtagStub).to.have.been.calledWith(
92+
GtagCommand.EVENT,
93+
EventName.ADD_TO_CART,
94+
{}
95+
);
96+
});
97+
7498
it('setCurrentScreen() calls gtag correctly (instance)', async () => {
7599
setCurrentScreen(gtagStub, analyticsId, 'home');
76100
expect(gtagStub).to.have.been.calledWith(GtagCommand.CONFIG, analyticsId, {

packages/analytics/src/functions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ export function logEvent(
3434
gtagFunction: Gtag,
3535
analyticsId: string,
3636
eventName: string,
37-
eventParams: EventParams,
37+
eventParams?: EventParams,
3838
options?: AnalyticsCallOptions
3939
): void {
40-
let params: EventParams | ControlParams = eventParams;
40+
let params: EventParams | ControlParams = eventParams || {};
4141
if (!options || !options.global) {
4242
params = { ...eventParams, 'send_to': analyticsId };
4343
}

packages/firebase/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4298,7 +4298,7 @@ declare namespace firebase.analytics {
42984298
*/
42994299
logEvent<T extends string>(
43004300
eventName: CustomEventName<T>,
4301-
eventParams: { [key: string]: any },
4301+
eventParams?: { [key: string]: any },
43024302
options?: firebase.analytics.AnalyticsCallOptions
43034303
): void;
43044304

0 commit comments

Comments
 (0)