Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #987140

    Greetings,

    I’m troubling shooting the best way to setup my google tag manager in my header.php. I activated a child theme and trying to figure out the best way about going about this. I have included the header.php file with the tags. Can i just add this file to the child theme root and it will work?

    Regards,

    #987163

    Hi,

    Moving header.php file to your child theme should not be necessary. Please refer to this post – https://kriesi.at/documentation/enfold/add-custom-js-or-php-script/#add-a-script-to-head-section and add your tags using functions.php file of your child theme.

    Regards,
    Yigit

    #987166

    Thanks Yigit,

    Interesting, this is a little above my coding knowledge… Given the file in the private section can you advise further? I have tried several plugins to add the google tag manager but non seem to work properly given where the code needs to be placed.

    #987575

    Hi,

    You can try to follow this tutorial: https://www.wpbeginner.com/beginners-guide/how-to-install-and-setup-google-tag-manager-in-wordpress/ and use this plugin: https://wordpress.org/plugins/insert-headers-and-footers/ to add custom scripts to the head section without coding. Setting up the google tag manager is beyond the scope of our support forum and you may need to hire a developer if you’re not familiar with the implementation of google tags.

    Best regards,
    Dude

    #987665

    Hello Dude.

    I have actually already tried that solution but from what I see a few people on the forums can’t get this to work with insert head and footers solution. I’ve actually tried a few google tag mangers plugins with the Enfold theme without any luck.

    #988211

    Hi mesagaragedoors,

    Can you show us the code that you’re trying to add?

    Best regards,
    Victoria

    #989409

    Here is the code (See Private)

    #989695

    Hi mesagaragedoors,

    Yes, if you want to keep this code in the header file, you need to copy it to the child theme and you will need to update it manually everytime you update the theme.

    Or you can use the codes to add in the functions.php, it is a cleaner way and requires less maintenance.

    If you need further assistance please let us know.
    Best regards,
    Victoria

    #989823

    Hi Victoria,

    The problem is when I use the codes to add in the functions.php the code does not work with the enfold theme.

    #989837

    Hi,

    Please try adding following code to bottom of Fuctions.php file in Appearance > Editor

    Best regards,
    Yigit

    #989907

    Ahoy Yigit,

    Would this be in the child theme functions.php? Do I need to do anything else? Right now I do not have https://wordpress.org/plugins/insert-headers-and-footers/ to add custom scripts instead seeing it wasn’t working. I uninstalled it. The problem is when I used it with ENFOLD it didn’t work.

    #990574

    Hi,

    You need to add it to the child theme funtions.php file (i.e. at the very bottom). Yigit’s code contained some invalid quote characters (I think the forum script converted them accidentally) – please use this code instead:

    
    //-------------------------------
    // Google Tag Manager script in head section
    //-------------------------------
    
    function av_google_tag_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-THXCK3T');</script>
    <!– End Google Tag Manager –>
    <?php
    }
    add_action('wp_head', 'av_google_tag_head');
    
    //-------------------------------
    // Custom script in body
    //-------------------------------
    
    function av_google_tag_body(){
    ?>
    <!– Google Tag Manager (noscript) –>
    <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-THXCK3T"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <!– End Google Tag Manager (noscript) –>
    <?php
    }
    add_action('wp_footer', 'av_google_tag_body');
    
    

    You also need to check the google tag manager id. At the moment it’s set to GTM-THXCK3T in the code above (two occurrences) but you may need to replace it with your id, otherwise the code won’t work.

    Best regards,
    Dude

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.