X (6.3.6) - Add Support + Enable Google Tag Manager

Hello!

I’ve been browsing the forums looking at guides / instructions for implementing Google Tag Manager to the latest version of X. I’ve tried following a bunch of different tutorials and they either don’t work, are badly written, or seem to be outdated.

I’m currently running the latest version of X w/ a Child Theme setup. I’ve been trying to add code to my functions.php and it’s partially working.

For Google Tag Manager, you need both a snippet of code in the Body and one in the Header. All the posts on these forums don’t reference/address both of these. Here are the tags:

  Paste this code as high in the <head> of the page as possible:
  <!-- 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-TAGIDHERE');</script>
  <!-- End Google Tag Manager -->
  
  Additionally, paste this code immediately after the opening <body> tag:
  <!-- Google Tag Manager (noscript) -->
  <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TAGIDHERE"
  height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
  <!-- End Google Tag Manager (noscript) -->

I tried following a couple guides and using what I could find in the forums but it doesn’t appear to be working.

Content of Framework.php for Child Theme: https://hastebin.com/ometisiyig.xml
Content of _HEADER.php for the Child Theme: https://hastebin.com/izakawaxoy.cs

Any help would be appreciated!

Thank you.

Hi,

To make it easier, you can install this third party plugin.

Then add the code snippet for body in Scripts in Footer and add snippet for header in Scripts in Header

Please remove any code you have added in functions.php file that pertains to this,

Thanks

I would rather not rely on Plugin for this as it entails extra issues I would rather avoid in the future even if its easier in the short term.

Any advice on what I’m currently doing that I could be doing wrong would be much appreciated!

Hi There,

Please add the following code under functions.php file locates in your child theme:

add_action( 'wp_head', 'print_google_code1', 1 );
function print_google_code1(){
	?>
<!-- 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-TAGIDHERE');</script>
<!-- End Google Tag Manager -->
	<?php
}

add_action( 'x_before_site_begin', 'print_google_code2', 1 );
function print_google_code2(){
	?>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-TAGIDHERE"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
	<?php
}

Hope that helps and thank you for understanding.

1 Like

I tried this, couldnt get it to work, tried https://theme.co/apex/forum/t/how-to-add-google-tag-manager-code-child-theme/44357 and that did not work either. What am I doing wrong? Child theme on X.

Hi MacMartino,

First of, please install the Google Analytics extension and try to utilize, please follow the documentation thoroughly.

Extension - Google Analytics

Make sure when you test it you’re not login on your site, because the Google Tag Manager code does not output when you’re login, this is to avoid unnecessary hits.

Remember to clear all your caching features (plugin, server-side, CDN, and browser’s cache) so that the changes will take effect immediately.

If that does not work still, please create your own thread and provide us login credentials (wp-admin and FTP) in a secure note so we can take a closer look.

Thanks,

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