Separate counter code file


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 installing the complete counter code on the site.

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

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

    <script type="text/javascript" src="/metrika.js"></script>
  2. Add the counter in the Yandex.Metrica interface.

  3. On the counter editing page (the Counter code tab), copy the content of the script element from the counter code.

  4. 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,
            } 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 = (d.location.protocol == "https:" ? "https:" : "http:") + "//";
        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 a list of counters, the counter will have the status: “not installed on homepage, but data is being received”.