• Resolved Argentum

    (@argentum)


    Hello!

    What would be the best way to add some Google Tag code and script to the HEAD and the BODY tag for all pages? I use child theme of Customizr.

    All the best
    M

    • This topic was modified 6 years, 9 months ago by Argentum.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Argentum

    (@argentum)

    I think I managed to add the applicable code just after the <body> tag by copying the header.php to the child theme folder and editing it. I then used the following in the functions.php:

    
    function hook_googletags() {
        ?>
            <!-- 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','xxxxxxxx');</script>
    
    <!-- End Google Tag Manager -->
        <?php
    }
    add_action('wp_head', 'hook_googletags', 0);
    

    The Google Tag code is inserted i the middle of the <head> section. How could I add the code just after the opening <head> tag? Priority 0 didn’t work.

    • This reply was modified 6 years, 9 months ago by Argentum.

    Hi there,
    WordPress allows you to use negative priority values too ??

    Hope this helps.

    Thread Starter Argentum

    (@argentum)

    Hm, all right. I changed to -9999. Quite high but not after the opening tag. Mind telling me which file to override to be able to place it after the opening <head> tag? ??

    
    <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="profile" href="https://gmpg.org/xfn/11" />
    <link rel="pingback" href="https://xxxxx/xmlrpc.php" />
    
    				   <!-- html5shiv for IE8 and less  -->
    				    <!--[if lt IE 9]>
    				      <script src="https://www.xxxxxx/wp-content/themes/customizr/inc/assets/js/html5.js"></script>
    				    <![endif]-->
    				            <!-- 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','xxxxxxx');</script>
    
    <!-- End Google Tag Manager -->
    
    • This reply was modified 6 years, 9 months ago by Argentum.

    hello, I put the google tag manager in a PHP code widget in the footer of the theme, do you think that it’s really bad ? thanks.

    Thread Starter Argentum

    (@argentum)

    @analyticshop

    I don’t know, but the instructions say so.

    https://developers.google.com/tag-manager/quickstart

    If you use the classical style the head tag opens in inc/czr-front.php which contains all the front templates (pretty big file to override in a child-theme -> hard to maintain).
    For the modern style the head tag opens in templates/head-no-model.php

    Hope this helps.

    Thread Starter Argentum

    (@argentum)

    OK, thanks. Good to know! I will keep is at is is now. ??

    All the best
    A

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Add google tag manager to head and body’ is closed to new replies.