Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Google Tag Manager execute document.write on custom HTML tags?

Google's documentation clearly states that synchronously loading tags are not supported: https://support.google.com/tagmanager/answer/2787990?hl=en

However there is now a setting 'Support document.write' when creating a custom HTML tag. This implies that synchronously loading tags are now supported but I can't find any documentation to explain how the new setting works.

My question is: Does document.write actually get executed or does GTM do something clever like replace it with document.createElement in the background (as described here: http://www.stevesouders.com/blog/2012/04/10/dont-docwrite-scripts/)?

like image 828
plainflavour Avatar asked Aug 17 '26 06:08

plainflavour


1 Answers

GTM actually overrides document.write for the scope of the executed tag (a custom HTML tag with support for document.write like you mentioned), as you can see on Chrome's devtools console:

document.write                                                     
function (){return m(g(arguments).join(""))}   gtm.js?id=GTM-someid:formatted:1455

Once GTM has finished the document.write call, it discards the override:

document.write
function write() { [native code] }

The implementation it provides uses "plain" insertion (e.g. document.createElement followed by appendChild or insertBefore) and a listener that runs when the script loads (e.g. onreadystatechange or onload) and executes the code that is placed after the document.write call in the tag definition.

like image 142
Omri Sivan Avatar answered Aug 18 '26 20:08

Omri Sivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!