Digital experiences for all disciplines
New Landing › How can we help? › Atelier › Google tag manager
New Landing › How can we help? › Atelier › Google tag manager
- This topic has 16 replies, 3 voices, and was last updated 8 years by Mohammad – SUPPORT.
-
Posted in: Atelier
-
March 22, 2016 at 5:28 pm #257189
Hi, I am trying to implement Google Tag Manager for my site.
It requires a line of code be added to my theme file inside the <body> tag, I was wondering how I can implement this without that code being over written each time the theme is updated?
I did think of using a child theme, but my site ran really slow when using it so I turned it off.
Any thoughts would be greatly appreciated.
thanks
SamMarch 22, 2016 at 5:48 pm #257192Hi,
You can add that to Theme Options => Custom JS.
Thanks.
March 22, 2016 at 7:19 pm #257215Hi thanks that makes sense, so would that insert the GTM script in the right place?
thanks
March 22, 2016 at 9:10 pm #257234Yes, that is correct.
Thanks.
March 23, 2016 at 5:42 pm #257449Hi, I am using a plugin (https://wordpress.org/support/plugin/duracelltomi-google-tag-manager) to integrate with GTM. it asks to put a piece of PHP code in rather than the GTM script. Can you advise if there is a way to insert that without editing the theme template files?
Many thanks.
March 23, 2016 at 5:44 pm #257450Hi,
No that is not possible.
– Let me know what it asks you to do and I’ll advise.
Thanks.
March 23, 2016 at 5:59 pm #257457HI, thanks, I have attached a screen grab. Please let me know how you think best to manage this. I think your suggestion of puting the GTM code in the custom JS could be an option, however I think it would mean doing the whole integration manual and not using the durall tomi plugin as I tried it with the plugin and it did not work.
thank again.
Attachments:
You must be logged in to view attached files.March 23, 2016 at 6:11 pm #257465could there be a way to insert this PHP using the Snippets plugin that I use for other things?
March 24, 2016 at 7:51 am #257530Hi,
You can insert through Snippets plugin.
Thanks
MohammadMarch 24, 2016 at 10:05 am #257556Hi Mohammad, I tried using the below PHP using the snippets plugin and when I do my analytics tags vanish…
<?php if ( function_exists( ‘gtm4wp_the_gtm_tag’ ) ) { gtm4wp_the_gtm_tag(); } ?>
Any thoughts why that might be?
Is there any way you could include it in an update of the theme? I imagine lots of users are struggling with this issue (they will be if they what to use GTM)… also it is a generic piece of code so the same for any installation of Duracell Tomi’s GTM plugin. If not any other ideas I can add the GTM code without editing the theme? I tried pasting manually the code direct from GTM into the Custom JS as suggested above but that did not seem to work, maybe it does not place it in the correct place on the page? It needs to be immediately inside the <body> tag.
I notice using Google tag assistant you have analytics tags active on the Atelier site, how did you guys do it?
Many thanks for the continued support.
Sam
March 24, 2016 at 10:17 am #257559Hi,
Please paste this code at functions.php of child theme:-add_action('wp_head', 'sf_gtm'); function sf_gtm(){ if ( function_exists( 'gtm4wp_the_gtm_tag' ) ) { gtm4wp_the_gtm_tag(); } return; }
Thanks
MohammadMarch 24, 2016 at 10:23 am #257565Hi Mohammad, thanks I will try this. Please clarify, I do not use a child theme… can it be added using the Snippets plugin?
I use this for other code additions.
Thanks
SamMarch 24, 2016 at 10:31 am #257568Hi,
You can add it through snippet plugin.
Thanks
MohammadMarch 24, 2016 at 11:25 am #257592Hi Mohammad, I tried this but the GTM assistant then says the code is outside the <body> tag… does that sound right to you?
I was looking at this: http://www.affectivia.com/blog/placing-the-google-tag-manager-in-wordpress-after-the-body-tag/
Bud could not get it to work because your theme file header.php has a different value than <?php body_class(); ?>. Based on this link is there a way to adit the snippet you gave me to be compatible with this technique?
Sorry I am no a programmer, but do have understanding of code.
thanks
SamMarch 24, 2016 at 11:42 am #257601Hi So I tried this and it seemed ok, I placed this code in the snippet plugin, would you mind looking at it to check it will not cause any theme issues and would in fact place in the body tag?
thank you.
add_filter( ‘body_class’, ‘gtm_add’, 10000 );
function gtm_add( $classes ) {
$block = <<<‘BLOCK’
<!– Google Tag Manager –>
<noscript><iframe src=”//www.googletagmanager.com/ns.html?id=GTM-WX9PHM”
height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
<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=
‘//www.googletagmanager.com/gtm.js?id=’+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,’script’,’dataLayer’,’GTM-WX9PHM’);</script>
<!– End Google Tag Manager –>BLOCK;
$classes[] = ‘”>’ . $block . ‘<br style=”display:none’;
return $classes;
} -
Posted in: Atelier
You must be logged in and have valid license to reply to this topic.