Tagged: google tag manager, GTM
-
AuthorPosts
-
January 31, 2019 at 2:32 pm #1060992
Hi there,
I want to install Google Tag Manager (GTM) via child themes functions.php. I want to do it this way because it should be robust and efficient. I came up with the following code which I included in child theme functions.php
function add_custom_gtm(){ ?> <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-XXXXXXX'); </script><?php } add_action('wp_head', 'add_custom_gtm'); function add_custom_gtm_noscript() { ?> <noscript><iframe src=”https://www.googletagmanager.com/ns.html?id=GTM-XXXXXX” height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript> <?php } add_action('ava_after_body_opening_tag', 'add_custom_gtm_noscript');
However, it seems that GTM is not installed with this code, because Chromes extension Google Tag Assistant does not recognize GTM.
What´s wrong and how to fix it?
(Note: In the code example above I hide my GTM Id by replacing it with GTM-XXXXXXX )Thank you for the support.
February 3, 2019 at 2:38 pm #1062225Hey P3T3R_0ne,
Can you give us temporary admin access to your website in the private content box below, so that we can have a closer look?
Are there any errors in the JavaScript console?
Best regards,
VictoriaFebruary 6, 2019 at 2:54 pm #1063647Hi Victoria,
I tried again installing GMT again. It worked with the following code( the hook for the second function was wrong):
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');
Just a general question to finish this:
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?
February 12, 2019 at 10:27 pm #1066112Hi,
The functions.php is just the quickest way. If u ask me, I also go to the functions.php file of the child theme and do it from there, as a function.
But for everyone it is based on their knowledge. If you prefer modifying the header.php that is also fine.Best regards,
BasilisFebruary 26, 2020 at 6:24 pm #1188150So, is this a good (the best) way to implement GTM?
Do you see any issues?
If good, then you can close this thread.
Best regards, Peter
February 27, 2020 at 7:33 pm #1188580Hi P3T3R_0ne,
This is the proper way to implement it :)
Glad you got it working for you! :)
If you need further assistance please let us know.
Best regards,
Victoria -
AuthorPosts
- The topic ‘Clean GTM implemantation via child themes functions.php’ is closed to new replies.