Skip to content

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

Merged
merged 5 commits into from
Nov 8, 2019
Merged

Directly check for existence of gtag script tag #2339

merged 5 commits into from
Nov 8, 2019

Conversation

hsubox76
Copy link
Contributor

@hsubox76 hsubox76 commented Nov 6, 2019

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 a window.dataLayer but does not insert a script tag to download gtag.js.

This change directly checks the DOM for existence of such a script tag, and we will insert it if not found.

@hsubox76 hsubox76 requested a review from Feiyang1 as a code owner November 6, 2019 21:38
expect(getOrCreateDataLayer('dataLayer')).to.deep.equal([]);
});

it('getOrCreateDataLayer is able to correctly identify an existing data layer', () => {
Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yes, changed.

@@ -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] = [];
Copy link
Member

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

Copy link
Contributor Author

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)) {
Copy link
Member

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)

Copy link
Contributor Author

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.

@hsubox76 hsubox76 merged commit ed4b147 into master Nov 8, 2019
@hsubox76 hsubox76 added this to the next milestone Nov 13, 2019
@firebase firebase locked and limited conversation to collaborators Dec 9, 2019
@hsubox76 hsubox76 deleted the ch-gtag branch January 8, 2020 21:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants