Google Tag Manager Container
Injects the GTM JavaScript snippet into <head> and the required <noscript> iframe into <body>, using a configurable container ID constant.
Module2 parts · by SnipCraft
PHPHead Tag
php
<?php
// PLACEHOLDER: Define your GTM container ID.
// Add this to wp-config.php: define( 'SCSEED_GTM_ID', 'GTM-XXXXXXX' );
if ( ! defined( 'SCSEED_GTM_ID' ) || ! SCSEED_GTM_ID ) { return; }
add_action( 'wp_head', 'scseed_gtm_head_snippet', 1 );
function scseed_gtm_head_snippet() {
$gtm_id = esc_js( SCSEED_GTM_ID );
?>
<!-- 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','<?php echo $gtm_id; ?>');</script>
<!-- End Google Tag Manager -->
<?php
}PHPBody Tag
php
<?php
// PLACEHOLDER: Define your GTM container ID.
// Add this to wp-config.php: define( 'SCSEED_GTM_ID', 'GTM-XXXXXXX' );
if ( ! defined( 'SCSEED_GTM_ID' ) || ! SCSEED_GTM_ID ) { return; }
add_action( 'wp_body_open', 'scseed_gtm_body_noscript', 1 );
function scseed_gtm_body_noscript() {
$gtm_id = esc_attr( SCSEED_GTM_ID );
echo '<!-- Google Tag Manager (noscript) -->' . "
";
echo '<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=' . $gtm_id . '" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>' . "
";
echo '<!-- End Google Tag Manager (noscript) -->' . "
";
}#frontend#tracking#analytics#marketing#gtm