How to prevent google tag manager overwriting document.write()?


by terrence , in category: SEO Tools , 7 months ago

How to prevent google tag manager overwriting document.write()?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by ervin.williamson , 7 months ago


Google Tag Manager has a built-in mechanism that automatically replaces the document.write() function with its own functionality. This behavior cannot be directly prevented or disabled in Google Tag Manager settings.

However, there are a few workarounds you can implement to prevent the overwriting of document.write().

  1. Use custom HTML tags: Instead of using the built-in tags provided by Google Tag Manager, you can create custom HTML tags where you have control over the code. This way, you can avoid the automatic overwriting of document.write().
  2. Implement code in a custom HTML tag: If you need to include a script that uses document.write(), you can put this script directly into a custom HTML tag in Google Tag Manager. By doing this, the script will not be affected by the automatic replacement.
  3. Use a separate JavaScript file: Instead of embedding scripts directly in Google Tag Manager, you can create a separate JavaScript file and include it in your website outside of Google Tag Manager. This will ensure that the document.write() function is not overwritten.
  4. Modify the script to avoid using document.write(): If possible, consider modifying the script to use alternative methods instead of document.write(). This will allow you to retain compatibility with Google Tag Manager while avoiding the overwriting issue.

It's important to note that Google Tag Manager's automatic replacement of document.write() is designed to maintain secure and reliable tag management. By using the recommended workarounds, you can still achieve your desired functionality without compromising the benefits of Google Tag Manager.