Skip Navigation

[Resolved] Adding Google Tag Manager js code to the head of every page that uses a layout

This support ticket is created 7 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 8 replies, has 4 voices.

Last updated by Miranda 7 years, 11 months ago.

Assisted by: Serhii Salii.

Author
Posts
#442376

Hello.
I would like to intall the google tag manager on all of my sites that use the toolset layout plugin.

I have a Divi Child Theme activated. If I paste the code of the GTM into the head of the child theme, it shows that the tags I am using are active on the sites where I used the divi builder.

How can I add the code to all of my sites where I used the Toolset Layouts Plugin though?
Especially how do I add code right under the head or body tags?

Thanks for your help.

#442634

Hello Nicholas!

You can add custom JavaScript code to any page of your website on "Layouts CSS and JS Editor" section. BUT! GTM code will not work from external JS file.

So, my suggestion is: use child theme, regardless of whether you are using Divi or any other theme. Open your child theme header.php file, locate the opening HEAD tag or opening BODY tag and put GTM code there.

#442724

Hello Serhii!
That's exactly what I did.
For some reason the GTM does not appear on the sites where I used a toolset layout.
Only on the sites where I used the divi builder.

Do you know why that is?

Have a nice day.

Nicholas

#442987

Hello, Nicholas

I found the problem, our integration plugin overrides Divi theme header.php, so we can do this in another way.

Please add following code to your child theme functions.php file:

// define the wp_print_scripts callback
function action_wp_print_scripts() {
    echo 'your GTM snippet code here'; // put your code here
};

// add the action
add_action( 'wp_print_scripts', 'action_wp_print_scripts', 10);
#443022

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please follow the guidelines given by Serhii and try to resolve your issue.

#443084
Google_Tag_Manager_Installation_Guide.png

Unfotunately this didn't work.
Google TM suggests you to add this code right under the <header> tag:
<!-- 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=
'hidden link);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->

And this code right under the <body> tag:
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="hidden link"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->

Go ahead and checkout the picutre in the attachment file...

Thank your for your help so far.

#443737

Hi, Nicholas

Thank you for screenshot, I see.
Seems like you have to follow Google rules to make it works.

So, let's try to put both snippets into right place.
First part I already posted here. We have to add script to HEAD section:

// define the wp_print_scripts callback
function action_wp_print_scripts() {
    echo '<!-- 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=
'<em><u>hidden link</u></em>);
})(window,document,'script','dataLayer','GTM-XXXXXX');</script>
<!-- End Google Tag Manager -->';
};
 
// add the action
add_action( 'wp_print_scripts', 'action_wp_print_scripts', 10);

Second part — add hidden iframe right after opening BODY tag. Let's do it with Layouts built-in filter:

function filter_wp_add_after_body($content){
	return '<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="<em><u>hidden link</u></em>"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->' . $content;
}

add_filter('get_layout_content_for_render', 'filter_wp_add_after_body');

Please try and let me know.

#443849

Thank you Serhii!
Great job.
I got it to work.
Have a nice day.

#1132919

Hi, I have the same issue, could you help me? Should I create a new ticket or can you help me here?
Your answer is unclear, not sure where to put the code on you last messages.

THanks you

This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.