Tagged: 

Viewing 17 posts - 1 through 17 (of 17 total)
  • Author
    Posts
  • #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.

    #904213

    Hey 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,
    Ismael

    #904222

    Thank 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?

    #904614

    Hi,

    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,
    Ismael

    #1047266

    Can I remove all code in the header.php in the child theme? And add new necessary code.

    #1047612

    Hi,

    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,
    Ismael

    #1053382

    Hi,

    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 GTM

    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('after_body', 'google_tag_after_body_script');

    Can we do something similar with enfold?

    Thanks in advance.

    #1053492

    Hi,

    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,
    Ismael

    #1059155

    Hi 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.

    #1060374

    Hi,

    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,
    Ismael

    #1060666

    Hi 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

    #1060670

    I want to manually install GTM without any additional plugins.

    #1062447

    Hi,

    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,
    Ismael

    #1063644

    Hi 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');
    
    #1064408

    Hi,

    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,
    Ismael

    #1065888

    Hi,

    ok, thank you for the support ;)

    #1065978

    Hi,

    No problem. Let us know if you need anything else. :)

    Best regards,
    Ismael

Viewing 17 posts - 1 through 17 (of 17 total)
  • The topic ‘Google tag manager’ is closed to new replies.