Metrica

Separate counter code file

Attention! 

The counter code placed in a file on your server may become outdated when the code is updated on the Yandex.Metrica side. This is why we recommend using the standard approach for installing the counter code on a site.

To connect the Yandex.Metrica counter code to a site in a separate file:

  1. Add this line to the HTML code on all of the site's pages:

    <script type="text/javascript" src="/metrika.js"></script>
  2. In order for the counter to collect data on users who have JavaScript disabled, add the content of the noscript element to the HTML code of the site's pages:

    <noscript><div><img src="https://mc.yandex.ru/watch/XXXXXX" style="position:absolute; left:-9999px;" alt="" /></div></noscript> 
  3. In the Yandex.Metrica interface,

    add the counter.

  4. Go to the Counter code section and copy the content of the script element.

  5. Add this code to the metrika.js file.

    Example of file contents
    (function (d, w, c) {
        (w[c] = w[c] || []).push(function() {
            try {
                w.yaCounterXXXXXX = new Ya.Metrika({id:XXXXXX,
                        clickmap:true,
                        trackLinks:true,
                        accurateTrackBounce: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");

    Where XXXXXX is the Yandex.Metrica counter number.

In this case, the counter code on the site will not be checked. On the page with the list of counters, the counter will have the status: — “not installed on homepage, but data is being received”.