Skip to content

Commit dc7b7f1

Browse files
committed
Created function to create policy
1 parent 516a70d commit dc7b7f1

File tree

2 files changed

+88
-32
lines changed

2 files changed

+88
-32
lines changed

packages/analytics/src/helpers.test.ts

Lines changed: 55 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import {
2525
wrapOrCreateGtag,
2626
findGtagScriptOnPage,
2727
promiseAllSettled,
28-
createGtagTrustedTypesScriptURL
28+
createGtagTrustedTypesScriptURL,
29+
createTrustedTypesPolicy
2930
} from './helpers';
3031
import { GtagCommand, GTAG_URL } from './constants';
3132
import { Deferred } from '@firebase/util';
@@ -48,30 +49,63 @@ const fakeDynamicConfig: DynamicConfig = {
4849
const fakeDynamicConfigPromises = [Promise.resolve(fakeDynamicConfig)];
4950

5051
describe('Trusted Types policies and functions', () => {
51-
const ttStub = stub(
52-
window.trustedTypes as TrustedTypePolicyFactory,
53-
'createPolicy'
54-
).returns({
55-
createScriptURL: (s: string) => s
56-
} as any);
57-
58-
it('Verify trustedTypes is called if the API is available', () => {
59-
expect(ttStub).to.be.called;
60-
});
52+
describe('Trusted types exists', () => {
53+
let ttStub: SinonStub;
54+
55+
beforeEach(() => {
56+
ttStub = stub(
57+
window.trustedTypes as TrustedTypePolicyFactory,
58+
'createPolicy'
59+
).returns({
60+
createScriptURL: (s: string) => s
61+
} as any);
62+
});
63+
64+
afterEach(() => {
65+
removeGtagScripts();
66+
ttStub.restore();
67+
});
68+
69+
it('Verify trustedTypes is called if the API is available', () => {
70+
const trustedTypesPolicy = createTrustedTypesPolicy(
71+
'firebase-js-sdk-policy',
72+
{
73+
createScriptURL: createGtagTrustedTypesScriptURL
74+
}
75+
);
6176

62-
it('createGtagTrustedTypesScriptURL verifies gtag URL base exists when a URL is provided', () => {
63-
expect(createGtagTrustedTypesScriptURL(GTAG_URL)).to.equal(GTAG_URL);
77+
expect(ttStub).to.be.called;
78+
expect(trustedTypesPolicy).not.to.be.undefined;
79+
});
80+
81+
it('createGtagTrustedTypesScriptURL verifies gtag URL base exists when a URL is provided', () => {
82+
expect(createGtagTrustedTypesScriptURL(GTAG_URL)).to.equal(GTAG_URL);
83+
});
84+
85+
it('createGtagTrustedTypesScriptURL rejects URLs with non-gtag base', () => {
86+
const NON_GTAG_URL = 'http://iamnotgtag.com';
87+
const consoleErrorStub = stub(console, 'error');
88+
89+
expect(createGtagTrustedTypesScriptURL(NON_GTAG_URL)).to.equal('');
90+
expect(consoleErrorStub).to.be.calledWith(
91+
'Unknown gtag resource!',
92+
NON_GTAG_URL
93+
);
94+
});
6495
});
6596

66-
it('createGtagTrustedTypesScriptURL rejects URLs with non-gtag base', () => {
67-
const NON_GTAG_URL = 'http://iamnotgtag.com';
68-
const consoleErrorStub = stub(console, 'error');
97+
describe('Trusted types does not exist', () => {
98+
it('Verify trustedTypes functions are not called if the API is not available', () => {
99+
delete window.trustedTypes;
100+
const trustedTypesPolicy = createTrustedTypesPolicy(
101+
'firebase-js-sdk-policy',
102+
{
103+
createScriptURL: createGtagTrustedTypesScriptURL
104+
}
105+
);
69106

70-
expect(createGtagTrustedTypesScriptURL(NON_GTAG_URL)).to.equal('');
71-
expect(consoleErrorStub).to.be.calledWith(
72-
'Unknown gtag resource!',
73-
NON_GTAG_URL
74-
);
107+
expect(trustedTypesPolicy).to.be.undefined;
108+
});
75109
});
76110
});
77111

packages/analytics/src/helpers.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,6 @@ import { AnalyticsError, ERROR_FACTORY } from './errors';
2929
// Possible parameter types for gtag 'event' and 'config' commands
3030
type GtagConfigOrEventParams = ControlParams & EventParams & CustomParams;
3131

32-
// Create a TrustedTypes policy that we can use for updating src
33-
// properties
34-
let _ttPolicy: Partial<TrustedTypePolicy>;
35-
if (window.trustedTypes) {
36-
_ttPolicy = window.trustedTypes.createPolicy('firebase-js-sdk-policy', {
37-
createScriptURL: createGtagTrustedTypesScriptURL
38-
});
39-
}
40-
4132
/**
4233
* Verifies and creates a TrustedScriptURL.
4334
*/
@@ -65,6 +56,30 @@ export function promiseAllSettled<T>(
6556
return Promise.all(promises.map(promise => promise.catch(e => e)));
6657
}
6758

59+
/**
60+
* Creates a TrustedTypePolicy object that implements the rules passed as policyOptions.
61+
*
62+
* @param policyName A string containing the name of the policy
63+
* @param policyOptions Object containing implementations of instance methods for TrustedTypesPolicy, see {@link https://developer.mozilla.org/en-US/docs/Web/API/TrustedTypePolicy#instance_methods
64+
* | the TrustedTypePolicy reference documentation}.
65+
* @returns
66+
*/
67+
export function createTrustedTypesPolicy(
68+
policyName: string,
69+
policyOptions: Partial<TrustedTypePolicyOptions>
70+
): Partial<TrustedTypePolicy> | undefined {
71+
// Create a TrustedTypes policy that we can use for updating src
72+
// properties
73+
let trustedTypesPolicy: Partial<TrustedTypePolicy> | undefined;
74+
if (window.trustedTypes) {
75+
trustedTypesPolicy = window.trustedTypes.createPolicy(
76+
policyName,
77+
policyOptions
78+
);
79+
}
80+
return trustedTypesPolicy;
81+
}
82+
6883
/**
6984
* Inserts gtag script tag into the page to asynchronously download gtag.
7085
* @param dataLayerName Name of datalayer (most often the default, "_dataLayer").
@@ -73,13 +88,20 @@ export function insertScriptTag(
7388
dataLayerName: string,
7489
measurementId: string
7590
): void {
91+
const trustedTypesPolicy = createTrustedTypesPolicy(
92+
'firebase-js-sdk-policy',
93+
{
94+
createScriptURL: createGtagTrustedTypesScriptURL
95+
}
96+
);
97+
7698
const script = document.createElement('script');
7799
// We are not providing an analyticsId in the URL because it would trigger a `page_view`
78100
// without fid. We will initialize ga-id using gtag (config) command together with fid.
79101

80102
const gtagScriptURL = `${GTAG_URL}?l=${dataLayerName}&id=${measurementId}`;
81-
(script.src as string | TrustedScriptURL) = _ttPolicy
82-
? (_ttPolicy as TrustedTypePolicy)?.createScriptURL(gtagScriptURL)
103+
(script.src as string | TrustedScriptURL) = trustedTypesPolicy
104+
? (trustedTypesPolicy as TrustedTypePolicy)?.createScriptURL(gtagScriptURL)
83105
: gtagScriptURL;
84106

85107
script.async = true;

0 commit comments

Comments
 (0)