Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1171840

    Alex
    Participant

    Hello,

    I’m having issues with the GTM code.

    I had this Google Analytics code:

      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    
      ga('create', 'UA-69481740-1', 'auto');
      ga('send', 'pageview');

    It was inserted here:

    http://take.ms/TB8S8

    But when I replaced it with the code from GTM, it didn’t work though I did remove the <script> tags as you said. The original code is:

    <!-- Google Tag Manager -->
    <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-TC8DXX"
    height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
    <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-TC8DXX');</script>
    <!-- End Google Tag Manager -->

    I changed the code to:

    <!– Google Tag Manager –>
    <noscript><iframe src=”//www.googletagmanager.com/ns.html?id=GTM-TC8DXX”
    height=”0″ width=”0″ style=”display:none;visibility:hidden”></iframe></noscript>
    (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-TC8DXX’);
    <!– End Google Tag Manager –>

    Where did I make an error?

    The second part of the question is, I want to add Yandex.Metrika tracking code in addition to GTM, how should I change it to work with X theme? Here’s the code:

    <!-- Yandex.Metrika counter -->
    <script type="text/javascript">
        (function (d, w, c) {
            (w[c] = w[c] || []).push(function() {
                try {
                    w.yaCounter39567015 = new Ya.Metrika({
                        id:39567015,
                        clickmap:true,
                        trackLinks:true,
                        accurateTrackBounce:true,
                        webvisor:true
                    });
                } catch(e) { }
            });
    
            var n = d.getElementsByTagName("script")[0],
                s = d.createElement("script"),
                f = function () { n.parentNode.insertBefore(s, n); };
            s.type = "text/javascript";
            s.async = true;
            s.src = "https://mc.yandex.ru/metrika/watch.js";
    
            if (w.opera == "[object Opera]") {
                d.addEventListener("DOMContentLoaded", f, false);
            } else { f(); }
        })(document, window, "yandex_metrika_callbacks");
    </script>
    <noscript><div><img src="https://mc.yandex.ru/watch/39567015" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
    <!-- /Yandex.Metrika counter -->

    Thanks.


    Alex

    #1171990

    Joao
    Moderator

    Hi Alex,

    Please take a look at this link:

    Extension: Google Analytics

    Hope it helps,

    Joao

    #1173085

    Alex
    Participant

    Hello,

    Thanks, but I’d prefer not using another plugin for such a simple task as inserting a tracking code. That said, I want to add not Google Analytics code but Google Tag Manager code. And actually, more than that, code from Yandex.Metrica.

    How can I do so?

    Thanks.


    Alex

    #1173137

    Thai
    Moderator

    Hi Alex,

    Please add the following code under functions.php file locates in your child theme:

    add_action( 'wp_head', 'x_add_custom_scripts' );
    function x_add_custom_scripts(){
      ?>
      <!-- Google Tag Manager -->
      <noscript><iframe src="//www.googletagmanager.com/ns.html?id=GTM-TC8DXX"
      height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
      <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-TC8DXX');</script>
      <!-- End Google Tag Manager -->
    
      <!-- Yandex.Metrika counter -->
      <script type="text/javascript">
          (function (d, w, c) {
              (w[c] = w[c] || []).push(function() {
                  try {
                      w.yaCounter39567015 = new Ya.Metrika({
                          id:39567015,
                          clickmap:true,
                          trackLinks:true,
                          accurateTrackBounce:true,
                          webvisor:true
                      });
                  } catch(e) { }
              });
    
              var n = d.getElementsByTagName("script")[0],
                  s = d.createElement("script"),
                  f = function () { n.parentNode.insertBefore(s, n); };
              s.type = "text/javascript";
              s.async = true;
              s.src = "https://mc.yandex.ru/metrika/watch.js";
    
              if (w.opera == "[object Opera]") {
                  d.addEventListener("DOMContentLoaded", f, false);
              } else { f(); }
          })(document, window, "yandex_metrika_callbacks");
      </script>
      <noscript><div><img src="https://mc.yandex.ru/watch/39567015" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
      <!-- /Yandex.Metrika counter -->
      <?php
    }

    Hope it helps 🙂