Skip to content

Commit d6a3815

Browse files
committed
Update to look for datalayer of gtag script
1 parent 34ad43c commit d6a3815

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

packages/analytics/src/helpers.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ describe('Gtag wrapping functions', () => {
5757
});
5858

5959
it('insertScriptIfNeeded inserts script tag', () => {
60-
expect(findGtagScriptOnPage()).to.be.null;
61-
insertScriptTag('customDataLayerName', fakeMeasurementId);
62-
const scriptTag = findGtagScriptOnPage();
60+
const customDataLayerName = 'customDataLayerName';
61+
expect(findGtagScriptOnPage(customDataLayerName)).to.be.null;
62+
insertScriptTag(customDataLayerName, fakeMeasurementId);
63+
const scriptTag = findGtagScriptOnPage(customDataLayerName);
6364
expect(scriptTag).to.not.be.null;
6465
expect(scriptTag!.src).to.contain(`l=customDataLayerName`);
6566
expect(scriptTag!.src).to.contain(`id=${fakeMeasurementId}`);

packages/analytics/src/helpers.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,20 @@ export function wrapOrCreateGtag(
320320
/**
321321
* Returns first script tag in DOM matching our gtag url pattern.
322322
*/
323-
export function findGtagScriptOnPage(): HTMLScriptElement | null {
323+
// TODO: We'll have this take a datalayer name
324+
// We'll check if a script exists and if it has the same data layer name
325+
export function findGtagScriptOnPage(
326+
dataLayerName: string
327+
): HTMLScriptElement | null {
328+
console.log(findGtagScriptOnPage);
329+
console.log(dataLayerName);
324330
const scriptTags = window.document.getElementsByTagName('script');
325331
for (const tag of Object.values(scriptTags)) {
326-
if (tag.src && tag.src.includes(GTAG_URL)) {
332+
if (
333+
tag.src &&
334+
tag.src.includes(GTAG_URL) &&
335+
tag.src.includes(dataLayerName)
336+
) {
327337
return tag;
328338
}
329339
}

packages/analytics/src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ describe('FirebaseAnalytics instance tests', () => {
349349
// Successfully resolves fake IDB open request.
350350
fakeRequest.onsuccess();
351351
await initializationPromisesMap[fakeAppParams.appId];
352-
expect(findGtagScriptOnPage()).to.not.be.null;
352+
expect(findGtagScriptOnPage('dataLayer')).to.not.be.null;
353353
expect(typeof window['gtag']).to.equal('function');
354354
expect(Array.isArray(window['dataLayer'])).to.be.true;
355355

packages/analytics/src/initialize-analytics.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ export async function _initializeAnalytics(
119119
fidPromise
120120
]);
121121

122-
// Detect if user has already put the gtag <script> tag on this page.
123-
if (!findGtagScriptOnPage()) {
122+
// Detect if user has already put the gtag <script> tag on this page with the passed in
123+
// data layer name.
124+
if (!findGtagScriptOnPage(dataLayerName)) {
124125
insertScriptTag(dataLayerName, dynamicConfig.measurementId);
125126
}
126127

0 commit comments

Comments
 (0)