Skip to content

Commit 572a151

Browse files
committed
Add tests and update public type docs.
1 parent afdfced commit 572a151

File tree

5 files changed

+101
-5
lines changed

5 files changed

+101
-5
lines changed

packages/analytics/src/api.test.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,19 @@ import { getFullApp } from '../testing/get-fake-firebase-services';
2222
import {
2323
getAnalytics,
2424
initializeAnalytics,
25+
setConsent,
2526
setDefaultEventParameters
2627
} from './api';
2728
import { FirebaseApp, deleteApp } from '@firebase/app';
2829
import { AnalyticsError } from './errors';
2930
import * as init from './initialize-analytics';
3031
const fakeAppParams = { appId: 'abcdefgh12345:23405', apiKey: 'AAbbCCdd12345' };
3132
import * as factory from './factory';
32-
import { defaultEventParametersForInit } from './functions';
33+
import {
34+
defaultConsentSettingsForInit,
35+
defaultEventParametersForInit
36+
} from './functions';
37+
import { ConsentSettings } from './public-types';
3338

3439
describe('FirebaseAnalytics API tests', () => {
3540
let initStub: SinonStub = stub();
@@ -123,4 +128,30 @@ describe('FirebaseAnalytics API tests', () => {
123128
eventParametersForInit
124129
);
125130
});
131+
it('setConsent() updates defaultConsentSettingsForInit if gtag does not exist ', () => {
132+
const consentParametersForInit: ConsentSettings = {
133+
'analytics_storage': 'granted',
134+
'functionality_storage': 'denied'
135+
};
136+
stub(factory, 'wrappedGtagFunction').get(() => undefined);
137+
app = getFullApp(fakeAppParams);
138+
setConsent(consentParametersForInit);
139+
expect(defaultConsentSettingsForInit).to.deep.equal(
140+
consentParametersForInit
141+
);
142+
});
143+
it('setConsent() calls gtag consent "update" if wrappedGtagFunction exists', () => {
144+
const consentParametersForInit: ConsentSettings = {
145+
'analytics_storage': 'granted',
146+
'functionality_storage': 'denied'
147+
};
148+
stub(factory, 'wrappedGtagFunction').get(() => wrappedGtag);
149+
app = getFullApp(fakeAppParams);
150+
setConsent(consentParametersForInit);
151+
expect(wrappedGtag).to.have.been.calledWithExactly(
152+
'consent',
153+
'update',
154+
consentParametersForInit
155+
);
156+
});
126157
});

packages/analytics/src/functions.test.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ import {
2525
setUserProperties,
2626
setAnalyticsCollectionEnabled,
2727
defaultEventParametersForInit,
28-
_setDefaultEventParametersForInit
28+
_setDefaultEventParametersForInit,
29+
_setConsentDefaultForInit,
30+
defaultConsentSettingsForInit
2931
} from './functions';
3032
import { GtagCommand } from './constants';
33+
import { ConsentSettings } from './public-types';
3134

3235
const fakeMeasurementId = 'abcd-efgh-ijkl';
3336
const fakeInitializationPromise = Promise.resolve(fakeMeasurementId);
@@ -192,4 +195,26 @@ describe('FirebaseAnalytics methods', () => {
192195
...additionalParams
193196
});
194197
});
198+
it('_setConsentDefaultForInit() stores individual params correctly', async () => {
199+
const consentParametersForInit: ConsentSettings = {
200+
'analytics_storage': 'granted',
201+
'functionality_storage': 'denied'
202+
};
203+
_setConsentDefaultForInit(consentParametersForInit);
204+
expect(defaultConsentSettingsForInit).to.deep.equal(
205+
consentParametersForInit
206+
);
207+
});
208+
it('_setConsentDefaultForInit() replaces previous params with new params', async () => {
209+
const consentParametersForInit: ConsentSettings = {
210+
'analytics_storage': 'granted',
211+
'functionality_storage': 'denied'
212+
};
213+
const additionalParams = { 'wait_for_update': 500 };
214+
_setConsentDefaultForInit(consentParametersForInit);
215+
_setConsentDefaultForInit(additionalParams);
216+
expect(defaultConsentSettingsForInit).to.deep.equal({
217+
...additionalParams
218+
});
219+
});
195220
});

packages/analytics/src/helpers.test.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
} from './helpers';
2929
import { GtagCommand } from './constants';
3030
import { Deferred } from '@firebase/util';
31+
import { ConsentSettings } from './public-types';
3132

3233
const fakeMeasurementId = 'abcd-efgh-ijkl';
3334
const fakeAppId = 'my-test-app-1234';
@@ -213,7 +214,11 @@ describe('Gtag wrapping functions', () => {
213214
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
214215
});
215216

216-
it('new window.gtag function does not wait when sending "set" calls', async () => {
217+
it('new window.gtag function does not wait when sending "consent" calls', async () => {
218+
const consentParameters: ConsentSettings = {
219+
'analytics_storage': 'granted',
220+
'functionality_storage': 'denied'
221+
};
217222
wrapOrCreateGtag(
218223
{ [fakeAppId]: Promise.resolve(fakeMeasurementId) },
219224
fakeDynamicConfigPromises,
@@ -222,7 +227,11 @@ describe('Gtag wrapping functions', () => {
222227
'gtag'
223228
);
224229
window['dataLayer'] = [];
225-
(window['gtag'] as Gtag)(GtagCommand.SET, { 'language': 'en' });
230+
(window['gtag'] as Gtag)(
231+
GtagCommand.CONSENT,
232+
'update',
233+
consentParameters
234+
);
226235
expect((window['dataLayer'] as DataLayer).length).to.equal(1);
227236
});
228237

packages/analytics/src/initialize-analytics.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ import { Deferred } from '@firebase/util';
3030
import { _FirebaseInstallationsInternal } from '@firebase/installations';
3131
import { removeGtagScript } from '../testing/gtag-script-util';
3232
import { setDefaultEventParameters } from './api';
33-
import { defaultEventParametersForInit } from './functions';
33+
import {
34+
defaultConsentSettingsForInit,
35+
defaultEventParametersForInit,
36+
_setConsentDefaultForInit
37+
} from './functions';
38+
import { ConsentSettings } from './public-types';
3439

3540
const fakeMeasurementId = 'abcd-efgh-ijkl';
3641
const fakeFid = 'fid-1234-zyxw';
@@ -118,6 +123,29 @@ describe('initializeAnalytics()', () => {
118123
// defaultEventParametersForInit is reset after initialization.
119124
expect(defaultEventParametersForInit).to.equal(undefined);
120125
});
126+
it('calls gtag consent if there are default consent parameters', async () => {
127+
stubFetch();
128+
const consentParametersForInit: ConsentSettings = {
129+
'analytics_storage': 'granted',
130+
'functionality_storage': 'denied'
131+
};
132+
_setConsentDefaultForInit(consentParametersForInit);
133+
await _initializeAnalytics(
134+
app,
135+
dynamicPromisesList,
136+
measurementIdToAppId,
137+
fakeInstallations,
138+
gtagStub,
139+
'dataLayer'
140+
);
141+
expect(gtagStub).to.be.calledWith(
142+
GtagCommand.CONSENT,
143+
'default',
144+
consentParametersForInit
145+
);
146+
// defaultEventParametersForInit is reset after initialization.
147+
expect(defaultConsentSettingsForInit).to.equal(undefined);
148+
});
121149
it('puts dynamic fetch promise into dynamic promises list', async () => {
122150
stubFetch();
123151
await _initializeAnalytics(

packages/analytics/src/public-types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ export interface EventParams {
291291

292292
/**
293293
* Consent status settings for each consent type.
294+
* For more information, see
295+
* {@link https://developers.google.com/tag-platform/tag-manager/templates/consent-apis
296+
* | the GA4 reference documentation for consent state and consent types}.
294297
* @public
295298
*/
296299
export interface ConsentSettings {

0 commit comments

Comments
 (0)