Separate code snippet file
The code snippet 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 code snippet on a site.
To connect the Yandex Metrica code snippet to a site in a separate file:
Add this line to the HTML code on all of the site's pages:
<script type="text/javascript" src="/metrika.js"></script>
In order for the tag 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>
- In the Yandex Metrica interface, add a tag.
Go to the Tag section and copy the content of the
script
element.Add this snippet to the metrika.js file.
Example of file contents(function(m,e,t,r,i,k,a){m[i]=m[i]||function(){(m[i].a=m[i].a|| []).push(arguments)}; m[i].l=1*new Date();k=e.createElement(t),a=e.getElementsByTagName(t) [0],k.async=1,k.src=r,a.parentNode.insertBefore(k,a)}) (window, document, "script", "https://mc.yandex.ru/metrika/tag.js", "ym") ym(XXXXXX, "init", { id:XXXXXX, clickmap:true, trackLinks:true, accurateTrackBounce:true, ut:"noindex" });
Where
XXXXXX
is the Yandex Metrica tag ID.
In this case, the code snippet on the site isn't checked. On the page with the list of tags, the tag has the status: — “not installed on homepage, but data is being received”.