-
Notifications
You must be signed in to change notification settings - Fork 948
Directly check for existence of gtag script tag #2339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
expect(getOrCreateDataLayer('dataLayer')).to.deep.equal([]); | ||
}); | ||
|
||
it('getOrCreateDataLayer is able to correctly identify an existing data layer', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is exactly the same as the one above. Do you mean to test something else?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, yes, changed.
packages/analytics/src/helpers.ts
Outdated
@@ -80,7 +77,7 @@ export function insertScriptTag(dataLayerName: string): void { | |||
export function getOrCreateDataLayer(dataLayerName: string): DataLayer { | |||
// Check for existing dataLayer and create if needed. | |||
let dataLayer: DataLayer = []; | |||
if (hasDataLayer(dataLayerName)) { | |||
if (Array.isArray(window[dataLayerName])) { | |||
dataLayer = window[dataLayerName] as DataLayer; | |||
} else { | |||
dataLayer = window[dataLayerName] = []; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since dataLayer is already initialized with []
, you can avoid one memory allocation by window[dataLayerName] = dataLayer
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
*/ | ||
export function findGtagScriptOnPage(): HTMLScriptElement | null { | ||
const scriptTags = window.document.getElementsByTagName('script'); | ||
for (const tag of Object.values(scriptTags)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You an iterate directly on scriptTags
- for (const tag of scriptTags)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed we cannot do this without downlevelIteration
and adding dom.iterable
TS lib, which we agreed adds unnecessary bloat to the analytics package.
We previously checked for pre-existence of a gtag script by looking for
window.dataLayer
. This is not an accurate check in some cases, such as if the user is using Google Tag Manager on the page, which creates awindow.dataLayer
but does not insert a script tag to downloadgtag.js
.This change directly checks the DOM for existence of such a script tag, and we will insert it if not found.