Tagged: google tag manager, GTM
-
AuthorPosts
-
December 29, 2014 at 1:59 pm #372558
A.
We need to add the “Google Tag Manger» http://www.google.com/tagmanager/ code to our Enfold site at http://KundeKontroll.noB.
We would like to add the code as a «hook» or in some other way so the Google Tag manger code does not get overwritten / deleted every time there is a new version of WordPress or the Enfold theme. If I add the code to the /enfold/functions.php or header.php file then it’s being overwritten when upgrading to the next version of Enfold or WordPress. How can I add the code so there is no need to re-enter the GTM code every time there is a new version of WordPress or Enfold?C.
This is the code that needs to be placed just after the opening / start <body> tag.<!– Google Tag Manager –>
<noscript><iframe src=”//www.googletagmanager.com/ns.html?id=GTM-MN3P7B”
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-MN3P7B’);</script>
<!– End Google Tag Manager –>December 29, 2014 at 3:08 pm #372577Hi limedrop!
You can use a child theme to avoid making the changes each time you update the theme – http://kriesi.at/documentation/enfold/using-a-child-theme/
You can download pre-built Enfold Child theme here – http://kriesi.at/documentation/enfold/downloads/Cheers!
YigitDecember 29, 2014 at 7:25 pm #372756Should the GTM code be added to the file:
/enfold/functions.php
or
header.php
December 30, 2014 at 11:38 am #373011Hey!
Please try adding following code to Functions.php file of your child theme
function add_custom_gtm(){ ?> <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-MN3P7B" 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-MN3P7B');</script> <?php } add_action('wp_head', 'add_custom_gtm');
Best regards,
Yigit- This reply was modified 9 years, 5 months ago by Yigit.
January 6, 2015 at 3:23 pm #375698Hello Yigit
I have done as described above, but “GTM” is not working on https://KundeKontroll.no
Please see the two PNG’s below
This is the code:
https://www.evernote.com/shard/s126/sh/74e92cbb-047f-43a0-89ee-162e34b5cfa2/b9458a5eb98fae08e9584204a2ca8873/deep/0/Screen-Shot-2015-01-06-at-14.12.53.pngGoogle Tag Assistent does not find the tag
https://www.evernote.com/shard/s126/sh/24fccbfe-c7cd-45c3-8099-8c55e7dd3581/3ca111a4d5c659e2cf7a3b023ab7ad43/deep/0/Screen-Shot-2015-01-06-at-14.18.16.pngJanuary 8, 2015 at 4:06 pm #376804Hey!
Please try adding the code you posted in your first post C, to header.php file of your child theme.
Cheers!
YigitJune 24, 2015 at 5:57 am #463771Hey!
add_action(‘wp_header’, ‘add_custom_gtm’);
should be
add_action(‘wp_head’, ‘add_custom_gtm’);
Please change it and it will work – I add it here for feature references, from search engine results.
Regards,
Basilis- This reply was modified 9 years, 5 months ago by Basilis.
August 20, 2016 at 8:31 pm #675082Hi Team, picking up on this issue – the solution proposed here places the GTM code near the </head> since we’re using wp_head(). Is there a hook we can right after <body>?
August 24, 2016 at 4:05 am #676728Hi,
Use the “ava_after_main_container” action hook.
add_action( 'ava_after_main_container', 'ava_after_main_container_mod', 10); function ava_after_main_container_mod() { // do something here }
Best regards,
IsmaelSeptember 1, 2016 at 4:10 am #680426Thanks. I will call you “Ismael.”
Just FYI, I had a very bad experience last weekend. I was using a GTM WordPress plugin that totally hosed our site. It had been working fine, for about a year, until I got a call that the client’s web site was down. I checked and sure enough…At first I had no idea what it was then I looked at the error log and traced it to this plugin (I prefer not to say it’s name). I have limited time to debug such arcane things but I suspect it was related to the most recent WP update or the most recent Theme update. Not pointing fingers, just saying it was VERY painful.
<panic>I could not login as the Admin to delete the plugin</panic>
Eventually I uninstalled the plugin and contacted the developer who gave me a long workaround. But I can’t have that kind of thing happening on a client site.I think I will try Ismael’s suggestion above.
September 2, 2016 at 6:56 am #681145Hi @Frank,
Great, let us know how you get on with it. We’ll leave the thread open in case you should need any further help.
Regards,
RikardNovember 19, 2016 at 8:51 pm #714371Hi guys,
Whenever I try to use Ismael’s code and add the Tag Manager to the <body> it gives me the following error:
Parse error: syntax error, unexpected ‘<‘ in /wp-content/themes/enfold-child/functions.php on line 10
What am I missing?
The code in question is below.Cheers,
ChipNovember 20, 2016 at 8:55 am #714432Hi Chip,
Please try this instead.
add_action( 'ava_after_main_container', 'ava_after_main_container_mod', 10); function ava_after_main_container_mod() { ?> <noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=GTM-5BLRX8S” height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript> <?php }
Best regards,
RikardDecember 9, 2016 at 10:11 pm #722625I am unclear of the steps to make this happen yet reluctant to start new thread. Please help with google tag manager code placement-
To sum, Google tag manager requires
1) code high in the <head> of the page
and
2) Code immediately after the <body> tagCan you provide a solution for both? I am using a child theme and would love it if you could say
“Do this “xyz [PASTE YOUR GOOGLE HEAD CODE HERE] in child theme (xyz) file
and
then do
“Do this ”abc code then [PASTE YOUR GOOGLE BODY CODE HERE] in chil theme xyz filelike many who buy themes that promise no custom coding, I am code illiterate so please bear that in mind :) thanks in advance
December 12, 2016 at 9:05 pm #723370Bump
December 13, 2016 at 12:45 am #723467Hi thanp73,
Since you are using child theme, you can copy header.php of the parent theme (Enfold) and paste it in your child theme. Edit header.php in your child theme.
1. Find this line of code (line 48):
</head>
and place the googletagmanager script above it, the code should look something like this:<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-XXXXXX’);</script>
2. Find this line of code:
<body id="top" <?php body_class($style." ".$avia_config['font_stack']." ".$blank." ".$sidebar_styling); avia_markup_helper(array('context' => 'body')); ?>>
it should just below the head tag. And place the code that looks like this:<noscript><iframe src=”//www.googletagmanager.com/ns.html?id=GTM-XXXXXX” height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
Hope this helps.
Regards,
NikkoDecember 13, 2016 at 12:52 am #723471If I copy the header.php into the child theme, wont I have to copy it again and re-insert this code once Enfold has a new update?
The ideal solution here is a HOOK added by the Kriesi team to the parent theme. We could then add some code to the functions.php to inject the GTM pixel. Is that something we can request from the Dev team?
December 13, 2016 at 1:07 am #723476Hi,
Yes, you can request it here: https://kriesi.at/support/enfold-feature-requests/
If I copy the header.php into the child theme, wont I have to copy it again and re-insert this code once Enfold has a new update?
No, the purpose of child theme is to override the parent theme and if there’s a theme update it won’t be lost, the only time you need to re-insert this is when there’s a change in header.php and you need update the header.php of your child theme. Though I would agree that the best solution is to use hooks especially if your not using a child theme.
Regards,
NikkoDecember 13, 2016 at 1:07 am #723477makes sense to me- My guy added header php to the child theme and manually put in my GTM code there. it’s working, now, but there is also a pending enfold theme update that I’m not sure what it’ll do.
For some reason, too, the GTM script check routine shows all my tags being fired twice per page load.
so, still an open issue here.
December 13, 2016 at 1:13 am #723481Nikko- thanks for the clear instructions, that’s just what a layman needs and what we did.
incidentally,the file reads like this:
/* Always have wp_head() just before the closing </head> * tag of your theme, or you will break many plugins, which * generally use this hook to add elements to <head> such * as styles, scripts, and meta tags. */ wp_head(); ?> </head>
So I read that to mean, don’t put your GTM code between the last ?> and the </head>
Instead, my guy put it higher up, just after the <head>.. but that said, we are still getting double fires of pixels. maybe it’s OK to put the GTM code right before the </head>?
December 13, 2016 at 4:35 pm #723797Hi thanp73,
It’s okay if you put it after the
<head>
or before
</head>
as long as it’s in the head. Just to clarify you’re statement above, do you mean it executes the gtm code twice? try to deactivate all plugins and check if it still does that, if yes, can you give us a link to your site, a temporary admin access and ftp access? so we can check it further.Cheers!
NikkoDecember 13, 2016 at 7:27 pm #723929the issues with double firing tags were on my end, there was another coder’s attempt to make this work buried in code somewhere I did not see so all is cleared up. the solution you had outlined works perfectly-
in short, copy header to child theme, insert in the places as shown, and good to go.
Question- how would we know when an update to Enfold would require an update to our child theme’s header file? Is there a reason why you don’t make the header.php automatically part of child themes?
- This reply was modified 7 years, 11 months ago by thanp73.
December 19, 2016 at 7:39 am #725962Hey!
Question- how would we know when an update to Enfold would require an update to our child theme’s header file? Is there a reason why you don’t make the header.php automatically part of child themes?
There will be no indication for that, unfortunately. You will know if there are changes in the file if you encounter an error or something is off in the frontend. You don’t have to worry about this because the header.php file is not updated frequently.
The child theme only requires a functions.php and a style.css file in order to work.
// https://codex.wordpress.org/Child_Themes
Regards,
IsmaelApril 9, 2017 at 11:06 pm #775159I added Google Tag Manager (GTM) code as suggested by Ismael inside <head> and <body> tag.
Chrome Tag Manager extension can find GTM, but Google Search Console says GTM is on wrong place and will not confirm my website.
Anybody had the same problem? Trying now for more than one hour but always get this error message in Google Search Console.April 17, 2017 at 8:52 am #778580Hi Ilvy,
Sorry for the late response, can you post a link to your site? so we can check the code’s location, just put it in Private Content, if you want to keep the link private.
Best regards,
NikkoFebruary 13, 2018 at 5:25 pm #911795Hi,
For everyone still coming to this page… The code is fine and works from functions.php but I had the issue that my adblocker was actually blocking the tags inside of the tag manager.
So I could see with Ghostly that the tag manager was running but got errors on the scripts (tags) I was trying to run. You can see this with the Chrome extension: https://chrome.google.com/webstore/detail/tag-assistant-by-google/kejbdjndbnbjgmefkgdddjlbokphdefk?hl=en-US
It will show this error: “ERR_BLOCKED_BY_CLIENT”
Cheers
- This reply was modified 6 years, 9 months ago by Liv.
February 14, 2018 at 3:41 am #912071Hi,
@liv2day_com: Thanks for the info.Please create a new thread or ticket if you’re still having this issue. We’ll check it there.
Best regards,
Ismael -
AuthorPosts
- The topic ‘Adding the Google Tag Manager code permanently’ is closed to new replies.