Adding Google Tag Manager to X Theme

I’ve spent hours trying to integrate the Google Tag Manager into my clien’t site, but with no luck.

I added the Google Tag script to the header and opening body tag via the functions.php, but with the no luck. My code used in my functions.php is below:

// Begin Google Tag Manager Code in Head Tag
// =============================================================================
function my_custom_google_tag_manager_code(){
?>
<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-P3NBNW4');</script>



<?php 
}
add_action( 'wp_head', 'my_custom_google_tag_manager_code' );
// End Google Tag Manager Code




// Add Google Tag Manager Code in Body
// =============================================================================
function my_custom_tag_manager_body(){
?>

<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-P3NBNW4"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>

<?php 
}
add_action( 'x_before_site_begin', 'my_custom_tag_manager_body' );

What’s the proper way to add Google Tag Manager to X theme? My website is http://www.corridorcompany.com

Hi There,

What you did is correct. I can see the code was added when I check the source code. See this: https://screencast-o-matic.com/watch/cbXeeK21OG

But then I got the following error on the console:
Failed to load resource: the server responded with a status of 404 ()
Please check this: https://stackoverflow.com/questions/29243170/404-error-for-google-tag-manager

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.