SC Wesley
SC Wesley

Reputation: 13

How delayed are JavaScript tags if they are inserted onto page via Google Tag Manager's custom HTML?

I'm trying to get some custom JS into the company website which is severely restricted because of the platform (for example, the website editor our vendor provided automatically removes any "script" tags). For this reason I tried inserting my custom js scripts via Tag Manager with custom HTML.

It works, albeit unreliably. Sometimes my script loads but most times it doesn't. One point worth noting is that our vendor only allowed us to insert our tag container to the top of "body" and have a "script asyn" in "head". Also our GTM container was set up incorrectly on a separate dataLayer without proper tuning. So my questions are:

1) Is there a way for me to check how long it takes for my own script to load, in a waterfall type analysis (or whatever alternative really)?

2) Is the script loaded by Tag Manager fired only after absolutely everything on page is loaded?

3) Is pasting the code in external js or inline THE surefire way to have my custom tag loaded? Would fixing the dataLayer issue also increase firing speed?

Thanks in advance!

Upvotes: 1

Views: 952

Answers (1)

Eike Pierstorff
Eike Pierstorff

Reputation: 32770

There is actually not much difference between using GTM, using an external file or even embedding your code inline. Unlike some other tag managers GTM does not fetch tags from a remote server; instead it packages all tags in a bit javascript function and links that to your page (you can test this by calling http://www.googletagmanager.com/gtm.js?id=GTM-XXXXXX where xxxxx is the id of your container, make a local copy and redirect the GTM request to that local copy - the tags will fire just like before).

The js file also contain all configured rules, and it is the rules that determine when a tag is fired.

The earliest possible point at which tags are fired is page load - that is the moment your browser renders the GTM code. Next (default) point is DOM ready, when all structural elements are created but not necessarily all assets are loaded, followed by window.loaded when everything is ready. After page load you can have custom events or click/submit etc. events.

As to your questions:

1) You can debug GTM tags just like any other JS tags (e.g. use the waterfall diagram in your browsers dev console)

2) No. When scripts are fired is determined by the configured rules. The earliest moment at which tags are fired is when the GTM code is loaded. However the tags do not (by default) fire in any specific sequence.

3) Any script that has problems from within GTM will probably have problems when pasted inline. Using GTM is not very different from using an external js file, except that GTM contains a lot of code to evaluate rules and triggers.

Upvotes: 1

Related Questions