How to Add Google Tag Manager Code - Child Theme

I am looking to add the GTM code to my site.

However, I can’t see the ‘header php’ in the child theme. See below.

How and where do I find the header to insert the code?

Hi @lasek723,

Thanks for reaching out.

You can simply add this code to the functions.php (Theme Functions) in your screenshot with your GTM code.

add_action('wp_head', 'my_gtm_code', 9999);

function my_gtm_code () { ?>

<!--  Add your GTM Code below this line -->

<?php }

It works the same as adding it in the header, and it’s more recommended. But if you still wish to do it through PHP file, then it’s here \x\framework\views\header\base.php. And you may add the code after the line <?php wp_head(); ?>.

<?php

// =============================================================================
// VIEWS/HEADER/BASE.PHP
// -----------------------------------------------------------------------------
// Declares the DOCTYPE for the site, includes the <head>, opens the <body>
// element as well as the .x-root <div> and .x-site <div>.
// =============================================================================

$x_root_atts = x_atts( apply_filters( 'x_root_atts', array( 'id' => 'x-root', 'class' => 'x-root' ) ) );
$x_site_atts = x_atts( apply_filters( 'x_site_atts', array( 'id' => 'x-site', 'class' => 'x-site site' ) ) );

?>
<!DOCTYPE html>

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

<head>
  <?php wp_head(); ?>
</head>

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

  <div <?php echo $x_root_atts; ?>>

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

    <div <?php echo $x_site_atts; ?>>

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

Then to make sure the change is permanent, you can copy that file to your child theme (eg. \x-child\framework\views\header\base.php)

Thanks!

The google tag manager comes in 2 parts
1
Copy the code below and paste it onto every page of your website.
Paste this code as high in the <head> of the page as possible:
2
Additionally, paste this code immediately after the opening <body> tag:

Do you past both parts one after another into the the space below the line <!-- Add your GTM Code below this line --> you mentioned above - or do you only paste the first part of the tag manager there?

Hi There,

Thanks for asking!

Only need the first part of the code to be there.

Hope this is clear!

Thanks

Ok - Where do I past the second part of the google tag manager code?

Hey @cyclonec,

Sorry for the confusion. To add the 2nd code directly below the body, please go back to @Rad’s suggestion to override the \x\framework\views\header\base.php.

You can see there the <body> tag. Insert your code below it.

Alternatively, though not directly below the body tag, you can use the x_before_site_begin like this:

add_action('x_before_site_begin', 'gtm_body', 9999);

function gtm_body() { ?>

<!--  Add your GTM Code below this line -->

<?php }

Like the first suggestion of Rad, it needs to be added in your child theme’s functions.php.

Hope that helps.

Hi,

I’m also installing Google Tag Manager but on X Pro child theme.

There’s nothing under the Pro child theme’s \framework\views folder. What’s the correct approach? Should I create the \framework\views\header folder and base.php file under the child theme to install the second code of GTM?

Thanks!

Hi Tommi,

Yes, please create the \framework\views\header directory on your child theme, and copy the file base.php on your parent theme, and do the modification suggested by Christian above.

Thanks,

Hi there, I pasted this code in my theme-x child functions.php file,
but nothing gets loaded: Can you check if I missed something?

add_action(‘wp_head’, ‘my_gtm_code’, 9999);

function my_gtm_code () { ?>

<?php }

Uhh, hjow do I paste code here so you guys can see it?

O and it is on www.xplobookings.nl

I justr tried the suggestion of : https://theme.co/apex/forum/t/x-6-3-6-add-support-enable-google-tag-manager/45466

but it doesn’t seem to work either. Help :slight_smile:

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.