# 如何设置 yandex metrica

**URL:** <https://meta.discourse.org/t/how-to-setup-yandex-metrica/301495>\
**Category:** Data & reporting\
**Created:** [2024年三月29日 13:25 UTC](https://meta.discourse.org/t/how-to-setup-yandex-metrica/301495 "2024-03-29T13:25:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrAli](https://avatars.discourse-cdn.com/v4/letter/m/7ea924/32.png) [@MrAli](https://meta.discourse.org/u/MrAli)\
**Post date:** [2024年三月29日 13:25 UTC](https://meta.discourse.org/t/how-to-setup-yandex-metrica/301495/1 "2024-03-29T13:25:31Z")

</div>

大家好

我想知道如何为自托管网站添加 Yandex Metrica。  
我做了一些搜索，但需要更多信息

> [@Adding Yandex Metrica counter to Discourse](https://meta.discourse.org/t/293282):
>
> I apologize for writing out of such stupidity, but my head is already boiling from this problem. I’ve tried a lot, but apparently I don’t know how to do it correctly. I need to install the Yandex.Metrica counter code in the , but due to CSP it does not work. I tried to somehow configure Goofle Tags, but apparently I did everything wrong there too. Please tell me step by step how I can do this. There are instructions from Yandex on this topic, but I’m stupid: Here is the counter code that ne…

```plaintext
<!-- Yandex.Metrika counter -->
<script type="text/javascript" >
   (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();
   for (var j = 0; j < document.scripts.length; j++) {if (document.scripts[j].src === r) { return; }}
   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(MY-CODE, "init", {
        clickmap:true,
        trackLinks:true,
        accurateTrackBounce:true,
        webvisor:true,
        ecommerce:"dataLayer"
   });
</script>
<noscript><div><img src="https://mc.yandex.ru/watch/MY-CODE" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
<!-- /Yandex.Metrika counter -->

```

我的想法是编辑主题， ~~或者创建一个新的组件。~~ 在 `Head` 部分，因为这是他们的指示

`/admin/customize/themes/1/common/head_tag/edit`

> 在网站 HTML 代码中安装标签时，请将代码尽可能靠近页面的顶部。例如，放在 `<head></head>` 或 `<body></body>` 标签内。

然后我可能会从我的 S3 提供 `tag.js` 以避免广告拦截器。我知道这不是个好主意，他们可能会随时更改 `tag.js`。

这是最好的方法吗？

Ali
