---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.6
alternate:
  - https://yandex.com/support/metrica/en/code/separate-code-file.md
  - https://yandex.com/support/metrica/es/code/separate-code-file.md
  - https://yandex.com/support/metrica/pt/code/separate-code-file.md
  - https://yandex.com/support/metrica/ru/code/separate-code-file.md
  - https://yandex.com/support/metrica/tr/code/separate-code-file.md
  - https://yandex.com/support/metrica/zh/code/separate-code-file.md
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/support/metrica/zh/llms.txt

# 单独的计数器代码文件


{% note alert %}

当在 Yandex Metrica 端更新代码时，放置在您服务器上文件中的计数器代码可能会过时。 正因如此，我们建议使用[标准方法](https://yandex.com/support/metrica/zh/quick-start.md)在网站上安装计数器代码。

{% endnote %}


要在单独文件中将 Yandex Metrica 计数器代码连接到网站：

1. 将此行添加到网站所有页面上的 HTML 代码中：
    
    ```javascript
    
    ```
    
1. 为了让计数器收集禁用 JavaScript 的用户的数据，请将 `noscript` 元素添加到网站页面的 HTML 代码中：
    
    ```
    <noscript><div><img src="https://mc.yandex.ru/watch/XXXXXX" style="position:absolute; left:-9999px;" alt="" /></div></noscript>
    ```
    
1. 在 Yandex Metrica 界面中，[添加计数器](https://metrica.yandex.com/add)。
1. 转到 **Tag** 部分并复制 `script` 元素的内容。
    
1. 将此代码添加到 `metrika.js` 文件。
    
    {% cut "文件内容示例" %}
    
    ```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();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(XXXXXXXX, "init", {
    id:XXXXXX,
    clickmap:true,
    trackLinks:true,
    accurateTrackBounce:true
    });
    ```
    
    其中 `XXXXXXXX` 是 Yandex Metrica 计数器号码。
    
    {% endcut %}
    






[联系我们](https://yandex-ad.cn/contact){.button}




<!-- source: zh/_includes/footer-links.md -->
- - -

<div class="borderless-table">

#|
||
实用链接

- [演示计数器](https://metrica.yandex.com/r/dashboard?)
- [添加计数器](https://metrica.yandex.com/add/)
- [免费计数器设置](https://yandex.com/promo/freeservice/metrica?utm_source=help_metrica_en&utm_medium=cpc&utm_campaign=1)
- [Yandex Metrica API](https://tech.yandex.com/metrika/)
- [提出您的想法](https://yandex.com/support/metrica/troubleshooting/idea.html)
|
线上培训



- [参加培训课程](https://yandex.com/adv/edu/online/metrika?utm_source=metrika_help&utm_medium=web&utm_campaign=static&utm_content=useful_links)

||
|#

</div>

<!-- source: zh/_includes/styles/table-style.md -->

<!-- endsource: zh/_includes/styles/table-style.md -->
<!-- endsource: zh/_includes/footer-links.md -->

<!-- source: zh/_includes/styles/cut-button.md -->

<!-- endsource: zh/_includes/styles/cut-button.md -->

<!-- source: zh/_includes/styles/href-to-button.md -->

<!-- endsource: zh/_includes/styles/href-to-button.md -->
