Consequences of including an external script file in a GTM tag

joakimdahlstrom picture joakimdahlstrom · Sep 20, 2013 · Viewed 10k times · Source

In GTM, lets say that I have a "custom HTML" tag, and in it include an external script file like

<script type="text/javascript" src="http://externalsite.com/file.js"></script>

How is this file loaded? Does it affect the loading time of the page?

I know that the GTM script is loaded asynchronously along with the tags, but I can't really imagine what happens in this case.

Answer

Mike Causer picture Mike Causer · Nov 8, 2013

In you GTM custom HTML tag, you can even use the async or defer attributes:

<script async type="text/javascript" src="/path/to/file.js"></script>

Further reading: http://davidwalsh.name/html5-async

And if you are using HTML5, type defaults to text/javascript, so you can leave it off.

Alternatively you could load asynchronously using an anonymous self invoking function:

<script>
(function(d,s){
var e = d.createElement(s),
m = d.getElementsByTagName(s)[0];
e.async = 1;
e.src = '//externalsite.com/file.js';
m.parentNode.insertBefore(e,m);
})(document,'script');
</script>