-
AuthorPosts
-
December 16, 2024 at 8:05 am #1473734
Hi
I need to install code to get some analytics working and I am not sure how to do itThe instructions say I need to put this code as high in the <head> code as possible
<!– 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=
‘https://www.googletagmanager.com/gtm.js?id=’+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,’script’,’dataLayer’,’GTM-XXXXX’);</script>
<!– End Google Tag Manager –>And this code immediately after the opening <body> tag
<!– Google Tag Manager (noscript) –>
<noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX”
height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
<!– End Google Tag Manager (noscript) –>I’ve seen a couple of posts on hoe to do the <head> but I can’t find anything about the <body>
I’m a bit of a dummy when it comes to this stuff. Sorry
December 16, 2024 at 9:15 am #1473736Hey ivanglaser,
Please add this to your child themes functions.php file for the head part:
function ivanglaser_gtm_head(){ ?> Head script goes here <?php } add_action('wp_head', 'ivanglaser_gtm_head');
And this for the body part:
function ivanglaser_gtm_body(){ ?> Body script goes here <?php } add_action('wp_body_open', 'ivanglaser_gtm_body');
Best regards,
RikardDecember 16, 2024 at 9:26 am #1473740read here – replace your tag manager ID : https://kriesi.at/support/topic/adding-google-tag-manager-in-enfold/#post-1473587
December 16, 2024 at 9:41 am #1473741Hi Rikard. Thanks
I added this code as you suggested:
function ivanglaser_gtm_head(){
?>
<!– 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=
‘https://www.googletagmanager.com/gtm.js?id=’+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,’script’,’dataLayer’,’GTM-XXXX’);</script>
<!– End Google Tag Manager –>
<?php
}
add_action(‘wp_head’, ‘ivanglaser_gtm_head’);function ivanglaser_gtm_body(){
?>
<!– Google Tag Manager (noscript) –>
<noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=GTM-XXXX”
height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
<!– End Google Tag Manager (noscript) –>
<?php
}
add_action(‘wp_body_open’, ‘ivanglaser_gtm_body’);However when I try and update teh file I get the error :
Your PHP code changes were not applied due to an error on line 11 of file /srv/htdocs/wp-content/themes/enfold-child/functions.php. Please fix and try saving again.
Unmatched ‘}’
December 16, 2024 at 11:28 am #1473744And you only trust Mods ?
function ava_g_tag_custom_script() { ?> <!-- 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= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-XXXXX'); </script> <!-- End Google Tag Manager --> <!-- Google Tag Manager (noscript) --> <noscript> <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe> </noscript> <!-- End Google Tag Manager (noscript) --> <?php } add_action('wp_head', 'ava_g_tag_custom_script');
the reason why this fails – is that a lot of participants do not use the code tag on posting snippets here : https://kriesi.at/support/topic/adding-google-tag-manager-in-enfold/#post-1473588
December 16, 2024 at 11:52 am #1473745and if you realy like to have that noscript inside body tag use instead:
function ava_google_tag_custom_script() { ?> <!-- 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= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','GTM-XXXXX'); </script> <!-- End Google Tag Manager --> <?php } add_action('wp_head', 'ava_google_tag_custom_script'); /*** Google Tag Manager Body Script *****/ function google_tag_manager_body_code() { ?> <noscript> <iframe src="https://www.googletagmanager.com/ns.html?id=GTM-XXXXX" height="0" width="0" style="display:none;visibility:hidden"></iframe> </noscript> <!-- End Google Tag Manager (noscript) --> <?php } add_action( 'ava_after_body_opening_tag', 'google_tag_manager_body_code');
December 16, 2024 at 12:07 pm #1473748Hi,
Thanks for helping out @guenni007. Did you manage to get your code working @ivanglaser?
Best regards,
RikardDecember 16, 2024 at 12:12 pm #1473751Hi. Thanks but I don’t seem to have that same issue as you found in https://kriesi.at/support/topic/adding-google-tag-manager-in-enfold/#post-1473588
I have 2 single quotes not one double quote
I still get the ” Unmatched ‘}’ ” error using your code above
December 16, 2024 at 12:27 pm #1473752Hi…not working yet. Same error when saving. Sorry, but I am not experienced in this area.
December 16, 2024 at 3:47 pm #1473765December 16, 2024 at 9:22 pm #1473789Hi Rikard.
You can use the link below.Then go to APPEARANCE…THEME FILE EDITOR (you probably knew that)
There is currently nothing in the child functions.php file
Thanks
IvanDecember 17, 2024 at 6:00 am #1473809Hi,
Thank you for the login token.
We added the code in the functions.php file — make sure to update the “GTM-XXXXX” placeholders with your own UID.
Best regards,
IsmaelDecember 17, 2024 at 6:12 am #1473812Works like a charm!
You guys are the best.
Thank you so much!
IvanDecember 17, 2024 at 6:38 am #1473815 -
AuthorPosts
- The topic ‘Install Google Tag Manager’ is closed to new replies.