DuracellTomi's Google Tag Manager in PRO Theme

Hi,

I tried original Google’s approach for adding Google Tag Manager by using different threads here in the forum but in the end it breaks headers in PRO Theme.

I see that many people recommend to use this third party plugin instead: https://wordpress.org/plugins/duracelltomi-google-tag-manager/

But it still requires to embed a string of code into the theme’s header template to follow recommendations of Google. Could you please help with the script for doing this correctly?

  1. Install, activate the plugin and enter Google Tag Manager ID in it’s settings. Then we select Custom in Container Code Placement for embedding the code provided into the theme (that is all easy).

  2. We copy header.php from original PRO Theme folder into the Child’s Theme. Open it for edit but it’s almost empty. There is no <head> </head> and <body> </body> parts. In accordance with instruction of the developer the code <?php if ( function_exists( 'gtm4wp_the_gtm_tag' ) ) { gtm4wp_the_gtm_tag(); } ?> shall go right after the <body>. Do I need to pre-populate anything inside <head> and <body> of the header.php in the Child Theme to make it work properly or there is another way for embedding that code?

Hi @imdyakov,

Thanks for reaching out.

The plugin should automatically implement it, that’s the main purpose of recommending a ready to use the plugin, to prevent further coding.

The theme has many template parts located in multiple folder locations, and it has <head></head>, but maybe they are only looking to one single location which is default to the standard theme (header.php). You can do this in two ways, first is by editing the X template for the header, the second is by using wp_head hook. I recommend using the hook since it’s already built-in to Wordpress, it’s there for that purpose. Please add this code to your child theme’s functions.php

add_action('wp_head', 'google_tag_custom',  999999);

function google_tag_custom () {

if ( function_exists( 'gtm4wp_the_gtm_tag' ) ) { gtm4wp_the_gtm_tag(); }

}

Your code will then be added to <head></head> no matter where the header template is located :slight_smile:

Thanks!

Hi!

Thank you for the suggestion. When I do it this way the tag in the Chrome extension is red and returnes two errors:
An error occured while the tag was fired: net::ERR_BLOCKED_BY_CLIENT
Error while sending request: net::ERR_BLOCKED_BY_CLIENT

However, I see there Google Tag Manager ID which was set up in the setting of the plugin.

In terms of the recommendation of plugin developer it says:
Code placement decides where to put the second, so called noscript part of the GTM container code.
This code is usually only executed if your visitor has for some reason disabled JavaScript.
The main GTM container code will be placed into the section of your webpages anyway (where it belongs to).
If you select ‘Custom’ you need to edit your template file and add the following line just after the opening <body> tag:
<?php if ( function_exists( 'gtm4wp_the_gtm_tag' ) ) { gtm4wp_the_gtm_tag(); } ?>

And it is recommended to use this custom code embed method because automatic implementation can only put it inside the footer. That is not recommended by Google.

Hi There,

To print the code inside the body tag, please update the previous code to this:

add_action('x_before_site_begin', 'google_tag_custom',  999999);

function google_tag_custom () {

if ( function_exists( 'gtm4wp_the_gtm_tag' ) ) { gtm4wp_the_gtm_tag(); }

}

The x_before_site_begin hook is located under pro/framework/views/header/base.php line 26, it’s inside the body tag.

Hope it helps :slight_smile:

Unfortunately,

It didn’t change anything. :frowning:
I do not use cashing as the site is under development so it is not the case.

Hi there,

You have several licenses on your account so I am not sure which site is in question.

Would you mind providing the admin and FTP details of the site you are working on so that we can check why the code previously provided did not work.

Please post the details as a Secure Note.

Hey @imdyakov,

I see the tag manager code added below the body tag (see secure note). That means, the code @thai gave worked.

Please contact the third party tag manager plugin you’re using why you’re having this issue especially that we do not have resources to troubleshoot tag manager issues.

We also already did our part by showing how to output the code in the theme.

Thanks.

OK, thanks for your help! Did you make any changes? I am asking because now the tag manger is green so it was set up properly. Maybe I still had to clean browser cash although I do not have any cashing plugins.

Nope. It might have not taken effect immediately and not related to caching.

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