Skip to content

Commit c4b9792

Browse files
committed
Move API functionality to internal function
1 parent 4c93e53 commit c4b9792

File tree

4 files changed

+77
-41
lines changed

4 files changed

+77
-41
lines changed

packages/analytics/src/api.test.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ import {
3636
defaultEventParametersForInit
3737
} from './functions';
3838
import { ConsentSettings } from './public-types';
39-
import { GtagCommand } from './constants';
40-
import * as util from '@firebase/util';
4139

4240
describe('FirebaseAnalytics API tests', () => {
4341
let initStub: SinonStub = stub();
@@ -157,19 +155,4 @@ describe('FirebaseAnalytics API tests', () => {
157155
consentParametersForInit
158156
);
159157
});
160-
it('getGoogleAnalyticsClientId() calls gtag "get" through wrappedGtagFunction', () => {
161-
stub(factory, 'wrappedGtagFunction').get(() => wrappedGtag);
162-
stub(util, 'getModularInstance').returns({
163-
app: { options: { measurementId: 'measurement-id-1' } }
164-
});
165-
app = getFullApp({ ...fakeAppParams, measurementId: 'measurement-id-1' });
166-
const analyticsInstance = initializeAnalytics(app);
167-
const id = getGoogleAnalyticsClientId(analyticsInstance);
168-
console.log({ didWeGetTheId: id });
169-
expect(wrappedGtag).to.have.been.calledWith(
170-
GtagCommand.GET,
171-
'measurement-id-1',
172-
'client_id'
173-
);
174-
});
175158
});

packages/analytics/src/api.ts

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ import {
5050
setUserProperties as internalSetUserProperties,
5151
setAnalyticsCollectionEnabled as internalSetAnalyticsCollectionEnabled,
5252
_setConsentDefaultForInit,
53-
_setDefaultEventParametersForInit
53+
_setDefaultEventParametersForInit,
54+
internalGetGoogleAnalyticsClientId
5455
} from './functions';
5556
import { ERROR_FACTORY, AnalyticsError } from './errors';
5657

@@ -175,31 +176,14 @@ export function setCurrentScreen(
175176
*
176177
* @param app - The {@link @firebase/app#FirebaseApp} to use.
177178
*/
178-
export async function getGoogleAnalyticsClientId(
179+
export function getGoogleAnalyticsClientId(
179180
analyticsInstance: Analytics
180181
): Promise<string> {
181182
analyticsInstance = getModularInstance(analyticsInstance);
182-
const measurementId = analyticsInstance.app.options.measurementId;
183-
let clientId = '';
184-
if (!measurementId) {
185-
logger.error('The app has no recognizable measurement ID.');
186-
} else {
187-
clientId = await new Promise((resolve, reject) => {
188-
wrappedGtagFunction(
189-
GtagCommand.GET,
190-
measurementId,
191-
'client_id',
192-
(fieldName: string) => {
193-
if (!fieldName) {
194-
reject('There was an issue retrieving the `client_id`');
195-
}
196-
resolve(fieldName);
197-
}
198-
);
199-
});
200-
}
201-
202-
return clientId;
183+
return internalGetGoogleAnalyticsClientId(
184+
wrappedGtagFunction,
185+
initializationPromisesMap[analyticsInstance.app.options.measurementId!]
186+
);
203187
}
204188

205189
/**

packages/analytics/src/functions.test.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,12 @@ import {
2727
defaultEventParametersForInit,
2828
_setDefaultEventParametersForInit,
2929
_setConsentDefaultForInit,
30-
defaultConsentSettingsForInit
30+
defaultConsentSettingsForInit,
31+
internalGetGoogleAnalyticsClientId
3132
} from './functions';
3233
import { GtagCommand } from './constants';
3334
import { ConsentSettings } from './public-types';
35+
import { Gtag } from './types';
3436

3537
const fakeMeasurementId = 'abcd-efgh-ijkl';
3638
const fakeInitializationPromise = Promise.resolve(fakeMeasurementId);
@@ -238,4 +240,34 @@ describe('FirebaseAnalytics methods', () => {
238240
...additionalParams
239241
});
240242
});
243+
it('internalGetGoogleAnalyticsClientId() rejects when no client_id is available', async () => {
244+
await expect(
245+
internalGetGoogleAnalyticsClientId(
246+
function fakeWrappedGtag(
247+
unused1: unknown,
248+
unused2: unknown,
249+
unused3: unknown,
250+
callBackStub: (fieldName: string) => {}
251+
): void {
252+
callBackStub('');
253+
} as Gtag,
254+
fakeInitializationPromise
255+
)
256+
).to.be.rejectedWith('There was an issue retrieving the `client_id`');
257+
});
258+
it('internalGetGoogleAnalyticsClientId() returns client_id when available', async () => {
259+
const CLIENT_ID = 'clientId1234';
260+
const id = await internalGetGoogleAnalyticsClientId(
261+
function fakeWrappedGtag(
262+
unused1: unknown,
263+
unused2: unknown,
264+
unused3: unknown,
265+
callBackStub: (fieldName: string) => {}
266+
): void {
267+
callBackStub(CLIENT_ID);
268+
} as Gtag,
269+
fakeInitializationPromise
270+
);
271+
expect(id).to.equal(CLIENT_ID);
272+
});
241273
});

packages/analytics/src/functions.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
} from './public-types';
2525
import { Gtag } from './types';
2626
import { GtagCommand } from './constants';
27+
import { logger } from './logger';
2728

2829
/**
2930
* Event parameters to set on 'gtag' during initialization.
@@ -137,6 +138,42 @@ export async function setUserProperties(
137138
}
138139
}
139140

141+
/**
142+
* Retrieves a unique Google Analytics identifier for the web client.
143+
* See {@link https://developers.google.com/analytics/devguides/collection/ga4/reference/config#client_id | client_id}.
144+
*
145+
* @public
146+
*
147+
* @param gtagFunction Wrapped gtag function that waits for fid to be set before sending an event
148+
* @param properties Map of user properties to set
149+
*/
150+
export async function internalGetGoogleAnalyticsClientId(
151+
gtagFunction: Gtag,
152+
initializationPromise: Promise<string>
153+
): Promise<string> {
154+
const measurementId = await initializationPromise;
155+
let clientId = '';
156+
if (!measurementId) {
157+
logger.error('The app has no recognizable measurement ID.');
158+
} else {
159+
clientId = await new Promise((resolve, reject) => {
160+
gtagFunction(
161+
GtagCommand.GET,
162+
measurementId,
163+
'client_id',
164+
(fieldName: string) => {
165+
console.log('inside the callback');
166+
if (!fieldName) {
167+
reject('There was an issue retrieving the `client_id`');
168+
}
169+
resolve(fieldName);
170+
}
171+
);
172+
});
173+
}
174+
return clientId;
175+
}
176+
140177
/**
141178
* Set whether collection is enabled for this ID.
142179
*

0 commit comments

Comments
 (0)