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/)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With