Google Tag Manager

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.

Hi there,
thank you, but I don’t see the place to put the code here.

I have this content on the Child functions.php page. Where do I put those codes?

<?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_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' );

Hello @Iban,

You child theme’s functions.php file should look like this:

<?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_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(){
?>

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

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

Hope this helps.

1 Like

Hi RueNel,

Where do I put the HEAD part? I only see the BODY part.

Thank you

Hey @Iban,

If you need to add a script for the head part, you will have to update the codes and use this:

<?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_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 HEAD
// =============================================================================
function add_script_to_head(){
?>

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

<?php
}
add_action( 'wp_head', 'add_script_to_head' );
// =================================================


// 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.

2 Likes

That worked,
Thank you so much

You’re welcome, Iban.

Hi there. I’ve been through the many support pages about how to add Google Tag Manager in X.

The support docs have all been good but it was a challenge for me to find the exact answer in one place.

So, if anyone is interested, I created this post that may help: https://theseosystem.com/wordpress-x-theme-google-tag-manager/

Cheers,
Richard

Hi Richard,

Thanks for the information.