Skip to content

Instantly share code, notes, and snippets.

@runezero
Created May 25, 2022 14:04
Show Gist options
  • Select an option

  • Save runezero/8747ce9444105bd0064a722d99b7d082 to your computer and use it in GitHub Desktop.

Select an option

Save runezero/8747ce9444105bd0064a722d99b7d082 to your computer and use it in GitHub Desktop.
[Add GTAG] Add Google Tag Manager to the website trough the script in the head and noscript in the body #enfold #wordpress
add_action('wp_head', 'wpdev_hook_gtag');
function wpdev_hook_gtag() {
?>
<!-- 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=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-1234567');</script>
<!-- End Google Tag Manager -->
<?php
}
add_action( 'ava_after_body_opening_tag', 'wpdev_hook_gtag_body', 10);
function wpdev_hook_gtag_body() {
?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-1234567"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment