Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #1473734

    Hi
    I need to install code to get some analytics working and I am not sure how to do it

    The 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&#8221;
    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

    #1473736

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

    #1473740
    #1473741

    Hi 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&#8221;
    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 ‘}’

    #1473744

    And 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

    #1473745

    and 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');
    #1473748

    Hi,

    Thanks for helping out @guenni007. Did you manage to get your code working @ivanglaser?

    Best regards,
    Rikard

    #1473751

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

    #1473752

    Hi…not working yet. Same error when saving. Sorry, but I am not experienced in this area.

    #1473765

    Hi,

    How can we access the site where you have added the code?

    Best regards,
    Rikard

    #1473789

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

    #1473809

    Hi,

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

    #1473812

    Works like a charm!
    You guys are the best.
    Thank you so much!
    Ivan

    #1473815

    Hi,

    No problem! Glad we could be of help. Please don’t hesitate to open another thread if you have more questions about the theme.

    Have a nice day.

    Best regards,
    Ismael

Viewing 14 posts - 1 through 14 (of 14 total)
  • The topic ‘Install Google Tag Manager’ is closed to new replies.