Skip to content

Instantly share code, notes, and snippets.

@broskees
Created May 8, 2024 21:20
Show Gist options
  • Save broskees/b335f261025f80bdc73c86ba246d12d8 to your computer and use it in GitHub Desktop.
Save broskees/b335f261025f80bdc73c86ba246d12d8 to your computer and use it in GitHub Desktop.
The most straightforward way to install a Google Tag Manager Tag onto a WordPress install.
<?php
add_action('wp_head', fn () => print <<<HTML
<!-- 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','YOUR-GOOGLE-TAG-ID-HERE');</script>
<!-- End Google Tag Manager -->
HTML, PHP_INT_MIN);
add_action('wp_body_open', fn () => print <<<HTML
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=YOUR-GOOGLE-TAG-ID-HERE"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
HTML, PHP_INT_MIN);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment