Tagged: GTM
-
AuthorPosts
-
January 25, 2018 at 2:18 pm #902852
Hi, please can you give me the best/latest advice for installing google tag manager. I need to add one piece to the header as high in the <head> of the page as possible and a 2nd piece of code immediately after the opening <body> tag.
I do have a child theme installed.
I did this on another site (not enfold) by editing header/footer, but Enfold is different. I have checked previous threads and the advice is quite old with various methods recommended, not sure what the best advice is for the new Enfold.
Thank you.
January 29, 2018 at 12:08 pm #904213Hey andrea,
Thank you for using Enfold.
Please add the code inside the Enfold > Google Services > Google Analytics Tracking Code field along with the ga analytics script. If you need to insert something inside the body tag, you have to modify the header.php file.
Best regards,
IsmaelJanuary 29, 2018 at 12:21 pm #904222Thank you, the bit of code for <head> is fine – but for the <body> bit, how/where is best to modify the header.php file when using a child theme?
January 30, 2018 at 4:58 am #904614Hi,
You can copy the header.php file from the parent theme and put it in the child theme folder. Do the modification in the duplicated file.
Best regards,
IsmaelDecember 19, 2018 at 5:37 pm #1047266Can I remove all code in the header.php in the child theme? And add new necessary code.
December 20, 2018 at 10:04 am #1047612Hi,
You can’t remove the existing codes because it will break the theme. You have to insert you own modification somewhere in that file.
Best regards,
IsmaelJanuary 13, 2019 at 4:48 pm #1053382Hi,
one question about this method of adding GTM.
Is copying the header.php file into the child theme and placing gtm code in there a robust method?
What if in future the structure of the header changes due to theme developments? Could it produce conflicts?
Isn´t it better to install GTM via a function in functions.php in child theme?
For another theme I worked with I had to insert the following code into functions.php to install GTMfunction google_tag_head_script() { ?> <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= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-XXXXXXX'); </script><?php } add_action( 'wp_head', 'google_tag_head_script' ); function google_tag_after_body_script() { ?> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --><?php } add_action('after_body', 'google_tag_after_body_script');
Can we do something similar with enfold?
Thanks in advance.
January 14, 2019 at 3:18 am #1053492Hi,
You can actually insert the GA code in the Enfold > Google Services panel. It’s the same as using the “wp_head” hook.
Best regards,
IsmaelJanuary 26, 2019 at 4:34 pm #1059155Hi Ismael,
I am not looking to insert just GA code.
I want to implement also other tracking(e.g. fb pixel tracking) and definitely want to manage all tracking via GTM.So are you saying that one could implement GTM by pasting the code into Enfold > Google Services panel > Tracking Code for GA?
GTM is not GA though. And what about the after body script which needs to use the after_body hook?I am asking if the code which I proposed above would work for a clean implementation of GTM with Enfold via functions.php in child theme. Do you see any problems with it? If that works then this would be a better solution than copying header.php into child theme, but I might well miss a point here.
Thank you for helping out.
January 30, 2019 at 8:52 am #1060374Hi,
Thanks for the update.
Yes, that should work but you have to replace the “after_body” with another hook called “wp_footer” because it’s not a valid hook.
Best regards,
IsmaelJanuary 31, 2019 at 1:39 am #1060666Hi Everyone,
I found a very simple way to install GTM in Enfold using a plugin, I’ve included a link below that explains everything.
I could not believe how simple this was.Watch the video (the first part explains what to do on the Google side), it walks you through the entire process.
Enjoy (and hope this was helpful),
David
January 31, 2019 at 2:41 am #1060670I want to manually install GTM without any additional plugins.
February 4, 2019 at 2:17 am #1062447Hi,
You can use the “wp_head” or the “wp_footer” hooks to render those snippets inside the head tag or after the body tag.
Best regards,
IsmaelFebruary 6, 2019 at 2:52 pm #1063644Hi Ismael,
I just tried to implement GTM with my proposed code and your correction of using the correct hook.
It looks like it´s working as Google Tag Assist is showing that GTM is installed on the site.Just a general question because I am not confident in php and WP development:
Is this way of installing GTM via the above code in functions.php of child theme sound and robust? Isn´t it even better than installing GTM via copying the header.php into child theme, like proposed in other threads in the forum?Here is the correct code which goes into functions.php:
function google_tag_head_script() { ?> <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= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-XXXXXXX'); </script><?php } add_action( 'wp_head', 'google_tag_head_script' ); function google_tag_after_body_script() { ?> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --><?php } add_action('wp_footer', 'google_tag_after_body_script');
February 8, 2019 at 5:35 am #1064408Hi,
Yes, using hooks is a valid way of doing things with WordPress. Using the “wp_head” hook for example, is the same as putting the code directly in the head tag of the header.php file.
// https://codex.wordpress.org/Function_Reference/wp_head
The same goes with the “wp_footer” hook.
Best regards,
IsmaelFebruary 12, 2019 at 10:53 am #1065888Hi,
ok, thank you for the support ;)
February 12, 2019 at 3:59 pm #1065978 -
AuthorPosts
- The topic ‘Google tag manager’ is closed to new replies.