Skip to content

Commit 3e401cb

Browse files
committed
Update gtagWrapper to take variable number of args for potential fallthrough case
1 parent 8f6ac07 commit 3e401cb

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

packages/analytics/src/api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ export async function getGoogleAnalyticsClientId(
189189
GtagCommand.GET,
190190
measurementId,
191191
'client_id',
192-
fieldName => {
192+
(fieldName: string) => {
193193
if (!fieldName) {
194194
reject('There was an issue retrieving the `client_id`');
195195
}

packages/analytics/src/helpers.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ describe('Gtag wrapping functions', () => {
281281
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
282282
});
283283

284+
it('new window.gtag function does not wait when sending an unknown command', async () => {
285+
wrapOrCreateGtag(
286+
{ [fakeAppId]: Promise.resolve(fakeMeasurementId) },
287+
fakeDynamicConfigPromises,
288+
{},
289+
'dataLayer',
290+
'gtag'
291+
);
292+
window['dataLayer'] = [];
293+
(window['gtag'] as Gtag)('new-command-from-gtag-team', fakeMeasurementId);
294+
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
295+
});
296+
284297
it('new window.gtag function waits for initialization promise when sending "config" calls', async () => {
285298
const initPromise1 = new Deferred<string>();
286299
wrapOrCreateGtag(

packages/analytics/src/helpers.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -227,45 +227,50 @@ function wrapGtag(
227227
* @param gtagParams Params if event is EVENT/CONFIG.
228228
*/
229229
async function gtagWrapper(
230-
command: 'config' | 'set' | 'event' | 'consent' | 'get',
231-
idOrNameOrParams: string | ControlParams,
232-
gtagParams?: GtagConfigOrEventParams | ConsentSettings | string,
233-
callback?: (s: string) => void
230+
command: 'config' | 'set' | 'event' | 'consent' | 'get' | string,
231+
...args: unknown[]
234232
): Promise<void> {
235233
try {
236234
// If event, check that relevant initialization promises have completed.
237235
if (command === GtagCommand.EVENT) {
236+
const [measurementId, gtagParams] = args;
238237
// If EVENT, second arg must be measurementId.
239238
await gtagOnEvent(
240239
gtagCore,
241240
initializationPromisesMap,
242241
dynamicConfigPromisesList,
243-
idOrNameOrParams as string,
242+
measurementId as string,
244243
gtagParams as GtagConfigOrEventParams
245244
);
246245
} else if (command === GtagCommand.CONFIG) {
246+
const [measurementId, gtagParams] = args;
247247
// If CONFIG, second arg must be measurementId.
248248
await gtagOnConfig(
249249
gtagCore,
250250
initializationPromisesMap,
251251
dynamicConfigPromisesList,
252252
measurementIdToAppId,
253-
idOrNameOrParams as string,
253+
measurementId as string,
254254
gtagParams as GtagConfigOrEventParams
255255
);
256256
} else if (command === GtagCommand.CONSENT) {
257+
const [gtagParams] = args;
257258
// If CONFIG, second arg must be measurementId.
258259
gtagCore(GtagCommand.CONSENT, 'update', gtagParams as ConsentSettings);
259260
} else if (command === GtagCommand.GET) {
261+
const [targetId, fieldName, callback] = args;
260262
gtagCore(
261263
GtagCommand.GET,
262-
idOrNameOrParams as string,
263-
gtagParams as string,
264-
callback as (fieldName: string) => void
264+
targetId as string,
265+
fieldName as string,
266+
callback as (...args: unknown[]) => void
265267
);
266-
} else {
268+
} else if (command === GtagCommand.SET) {
269+
const [customParams] = args;
267270
// If SET, second arg must be params.
268-
gtagCore(GtagCommand.SET, idOrNameOrParams as CustomParams);
271+
gtagCore(GtagCommand.SET, customParams as CustomParams);
272+
} else {
273+
gtagCore(command, ...args);
269274
}
270275
} catch (e) {
271276
logger.error(e);

packages/analytics/src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface MinimalDynamicConfig {
5353
measurementId: string;
5454
}
5555

56+
// We can kill this interface as it'll all be unknown
5657
/**
5758
* Standard `gtag` function provided by gtag.js.
5859
*/
@@ -77,8 +78,9 @@ export interface Gtag {
7778
command: 'get',
7879
targetId: string,
7980
fieldName: string,
80-
callback: (s: string) => void
81+
callback: (...args: unknown[]) => void
8182
): void;
83+
(command: string, ...args: unknown[]): void;
8284
}
8385

8486
export type DataLayer = IArguments[];

0 commit comments

Comments
 (0)