Google Tag Manager

hi again

Just so you know, here is what the file now looks like, though I have not uploaded it to my site

<?php

// =============================================================================
// VIEWS/INTEGRITY/_HEADER.PHP
// -----------------------------------------------------------------------------
// Declares the DOCTYPE for the site and includes the <head>.
// =============================================================================

?>

<!DOCTYPE html>

<html class="no-js" <?php language_attributes(); ?>>

<head>
  <?php wp_head(); ?>
<!-- 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-WCDDFZM');</script>
<!-- End Google Tag Manager -->
</head>

<body <?php body_class(); ?>>

	<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-WCDDFZM"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

  <div id="x-root" class="x-root">

    <?php do_action( 'x_before_site_begin' ); ?>

    <div id="top" class="site">

    <?php do_action( 'x_after_site_begin' ); ?>

Does that seem right to you and also the location I indicated above?

cheers

1 Like

Hi There,

Thanks for writing in! Instead you can setup a child theme and add the following code into your child theme’s functions.php file.

// Begin Google Tag Manager Code
// =============================================================================
function my_custom_google_tag_manager_code(){
?>

<!-- Replace this line with your Google Tag Manager Code  -->


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

Hope that helps.

2 Likes

Hello Midarshana

Thanks so much for your reply

OK, I can add the head code snippet to this, but what about the body snippet?

Regards

Hi There @guybower1

To add the code to the body tag, please add another code below the code provided above, in the functions.php file on your child theme.

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

<!-- Replace this line with your code provided by google -->

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

Hope that’s clear.
Thanks!

2 Likes

hi Midarshana

Thanks for your reply

I will adjust that file in my child theme

Take care

Regards

Hi There,

You’re always welcome.
Do let us know if you still have questions. For more information regarding filters and hooks, please check this article: https://theme.co/apex/forum/t/customizations-actions-and-filters-in-x/208

I get zero luck with adding Google Tag Manager for Header and for Body in my child functions.php

I have worked on this for a couple hours, any advice or guidance is greatly appreciated. I had GTM working well in X, but PRO - not yet.

My site is TobyElwin.com

Many thanks,

Toby

Hi @TobyElwin,

Your site is heavily cached, minified, and scripts are optimized by WP Rocket. Please try clearing your cache and turn off minification just for testing, then try it again. I can’t see your google tags embed code either so it could be due to cache.

Thanks!

hi Lely

Just an update on the implementation of Google Tag Manager. I added the supplied code into the functions.php file in my child theme, yet no tags are visible when I run the Tag Assistant.

Do you have any suggestions as to why they would not be showing? URL is feastthailand.com

Head script is

`

` <?php } add_action( 'wp_head', 'my_custom_google_tag_manager_code' ); // End Google Tag Manager Code Body script is // Add Google Tag Manager Code in Body // ============================================================================= function my_custom_tag_manager_body(){ ?>

`

` <?php } add_action( 'x_before_site_begin', 'my_custom_tag_manager_body' ); Any suggestions, Lely? regards

Hi Lely

As an addition to what I put above, Google Analytics is also not working, yet I have your X addon installed and configured.

So to date, 2 arms of Google’s analytics are not working, neither GA nor GTM

regards

Thank you for looking.

I took suggestion to turn off clear cache, then turn off WP Rocket minify, then tried to deactivate WP Rocket - not one worked, though.

The Google Tag is, I used the same method as @guybower1 in his comment.

I will leave WP Rocket off, would you try another look at it, please.

Thank you,

Toby

@guybower1, I checked and there is no google tag code present in your site. Event this line is not there <!-- Google Tag Manager -->. Would you mind providing your admin login credentials in a secure note? I just like to check how you set it up, but if it’s host cache related then you should contact them for fixes.

@TobyElwin, Same in your case, I still can’t see the google tag manager code in your site’s source code. Please start a new thread about this and provide your admin login credentials in a secure note. I can’t promise a fix but I like to check it first.

Thanks!

hi Iam

Thanks so much for your reply

To date, I have only enabled the Google Analytics plugin and put the Tag Manager code in my functions.php file.

Login details:

cheers

Hi There,

Google analytics code and the code added via child theme is there. See this: https://screencast-o-matic.com/watch/cbjbDql7my. Check it when you are not logged in on admin because for report accuracy, the code was only active when you’re not on admin. Also try to clear browser cache in case you still can’t see it.

OK, thank you. I started a new thread Code for Google Tag Manager

Hello There,

We will be responding on that thread.

Best Regards.

Hi @mldarshana,

I’ve added both of these scripts to my X (v.5.1.1) site, in the child theme, replacing my own GTM code blocks as required. However, the GTM Testing tool isn’t picking up the code just beneath the tag. I’m wondering if that’s perhaps because of the extra

line of code after the opening tag. Is it possible to insert the code before this
opening?

Site is at https://smartdataseo.com/

Thanks,
–Eric

Hi There @smartdataseo

Could you please open up a new thread and provide us with your FTP credentials in a secure note or provide us with the full code that is in your functions.php file to check your issue.

Thanks!

Hi there, is this correct how I added? Let me know

<?php // ============================================================================= // FUNCTIONS.PHP // ----------------------------------------------------------------------------- // Overwrite or add your own custom functions to Pro in this file. // ============================================================================= // ============================================================================= // TABLE OF CONTENTS // ----------------------------------------------------------------------------- // 01. Enqueue Parent Stylesheet // 02. Additional Functions // ============================================================================= // Enqueue Parent Stylesheet // ============================================================================= add_action('wp_head', 'add_header_code'); function add_header_code(){ ?> <?php }; add_filter( 'x_enqueue_parent_stylesheet', '__return_true' ); // Additional Functions // ============================================================================= function my_scripts_method() { wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/js/custom.js', array( 'jquery' ) ); } add_action( 'wp_enqueue_scripts', 'my_scripts_method' ); // Add Script to Body // ============================================================================= function add_script_to_body(){ ?> <?php } add_action( 'x_after_body_begin', 'add_script_to_body' ); // =================================================

Hi Iban,

If you need to add the GTM code in the body area of your page, you can add the GTM code here:

// Add Script to Body
// =============================================================================
function add_script_to_body(){
?>

     <!-- Add the GTM code here: -->

<?php
}
add_action( 'x_after_body_begin', 'add_script_to_body' );
// =================================================

Hope this helps.