Created
May 7, 2019 08:42
-
-
Save itsKnight847/686dbea419f2e21a8dc3a37a4ddaedcd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Google tagmanager | |
*/ | |
add_action( 'wp_head', 'head_scripts' ); | |
function head_scripts() { ?> | |
<!-- Google Tag Manager --> | |
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': | |
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], | |
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= | |
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); | |
})(window,document,'script','dataLayer','GTM-XXXXX');</script> | |
<!-- End Google Tag Manager --> | |
<?php | |
} | |
add_action( 'wp_footer', 'body_scripts' ); | |
function body_scripts() { ?> | |
<!-- Google Tag Manager --> | |
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-XXXXX" | |
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> | |
<!-- End Google Tag Manager --> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The body script should be place as early as possible too. The best implementation I have seen so far just uses a custom hook placed immediately after opening the body tag. Example https://stackoverflow.com/a/31226533/1214176