---
metadata:
  - name: generator
    content: Diplodoc Platform v5.54.5
alternate:
  - https://yandex.com/support/metrica/en/code/install-counter-csp.md
  - https://yandex.com/support/metrica/es/code/install-counter-csp.md
  - https://yandex.com/support/metrica/pt/code/install-counter-csp.md
  - https://yandex.com/support/metrica/ru/code/install-counter-csp.md
  - https://yandex.com/support/metrica/tr/code/install-counter-csp.md
  - https://yandex.com/support/metrica/zh/code/install-counter-csp.md
  - href: zh/code/install-counter-csp.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
title: 在使用 CSP 的网站上安装计数器
description: 如何在使用 CSP 的网站上安装计数器。
---
> **Documentation Index:** Fetch the complete configuration index at https://yandex.com/support/metrica/zh/llms.txt


# 在使用 CSP 的网站上安装计数器

Yandex Metrica 生成的代码片段旨在内联插入页面的 HTML 代码中。 在使用[内容安全政策](https://www.w3.org/TR/CSP/)技术的网站上，除非您采取特定步骤，否则此代码将不会运行。 特别是，您需要使用 HTTP 标头来授予浏览器处理数据的权限。 HTTP 标头应包含 [src-script](https://www.w3.org/TR/CSP/#directive-script-src) 指令与 [nonce](https://www.w3.org/TR/CSP/#framework-directive-source-list) 属性（这需要对 `script` 元素的内容进行签名）以及处理`noscript`元素内容的 [img-src](https://www.w3.org/TR/CSP/#directive-img-src) 指令。

此外，Yandex Metrica 还提供了另一种启用[计数器代码](*кода-счетчика)的方法：仅在页面的 HTML 代码中放置 `noscript` 元素的内容，并将 `script` 元素的内容移到[外部脚本](#separate-code-file)中（例如，移动到 JS 文件）。

无论您选择哪种方法，都必须修改 HTTP 标头以添加访问 Yandex Metrica 的权限。

{% cut "一般地址列表" %} {#urls}

```
https://mc.yandex.ru
https://mc.yandex.az
https://mc.yandex.by
https://mc.yandex.co.il
https://mc.yandex.com
https://mc.yandex.com.am
https://mc.yandex.com.ge
https://mc.yandex.com.tr
https://mc.yandex.ee
https://mc.yandex.fr
https://mc.yandex.kg
https://mc.yandex.kz
https://mc.yandex.lt
https://mc.yandex.lv
https://mc.yandex.md
https://mc.yandex.tj
https://mc.yandex.tm
https://mc.yandex.uz
https://mc.webvisor.com
https://mc.webvisor.org
https://yastatic.net
wss://mc.yandex.ru
wss://mc.yandex.az
wss://mc.yandex.by
wss://mc.yandex.co.il
wss://mc.yandex.com
wss://mc.yandex.com.am
wss://mc.yandex.com.ge
wss://mc.yandex.com.tr
wss://mc.yandex.ee
wss://mc.yandex.fr
wss://mc.yandex.kg
wss://mc.yandex.kz
wss://mc.yandex.lt
wss://mc.yandex.lv
wss://mc.yandex.md
wss://mc.yandex.tj
wss://mc.yandex.tm
wss://mc.yandex.uz
wss://mc.webvisor.com
wss://mc.webvisor.org
```

{% endcut %}

{% cut "frame-ancestors 的地址" %} {#frame-ancestors-urls}

```
metrika.yandex.ru
analytics.yandex.by
analytics.yandex.com
analytics.yandex.com.tr
analytics.yandex.kz
analytics.yandex.ru
metr.yandex.by
metr.yandex.com
metr.yandex.com.tr
metr.yandex.kz
metr.yandex.ru
metrica.ya.ru
metrica.yandex
metrica.yandex.by
metrica.yandex.com
metrica.yandex.com.tr
metrica.yandex.kz
metrica.yandex.ru
metrika.ya.ru
metrika.yandex
metrika.yandex.by
metrika.yandex.com
metrika.yandex.com.tr
metrika.yandex.kz
metrika.yandex.uz
```

{% endcut %}

## 将计数器代码插入网站页面的 HTML 代码中 {#nonce}

{% note alert %}

这些示例并未列出 Yandex Metrica 工作所需的所有地址。

- [一般地址列表](#urls)
- [frame-ancestors 的地址](#frame-ancestors-urls)

{% endnote %}

如果您使用此方法，`Content-Security-Policy` 或者 `Content-Security-Policy-Report-Only` HTTP 标头必须包含以下指令：

- 带 [nonce](https://www.w3.org/TR/CSP/#framework-directive-source-list) 属性的 [script-src](https://www.w3.org/TR/CSP/#directive-script-src)。 此属性必须包含具有随机字符（拉丁字母和数字）序列的字符串值。 该值必须在服务器上针对每个请求单独随机生成。
    
    ```no-highlight
    Content-Security-Policy: script-src 'nonce-<character sequence>';
    ```
    
    网站页面上计数器代码的 `script` 元素中 `nonce` 属性必须包含相同的字符序列。
    
    ```javascript
    ...
    <!-- Yandex Metrica tag -->
    
```

1. 在 Yandex Metrica 界面中，转到 **Settings** (**Tag**)，然后从字段中复制 `script` 元素的内容。
1. <!-- source: zh/_includes/code/separate-code-file/id-separate-code-file/7.md -->
   将此代码添加到 `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 %}
   <!-- endsource: zh/_includes/code/separate-code-file/id-separate-code-file/7.md -->
    
1. <!-- source: zh/_includes/code/separate-code-file/id-separate-code-file/4.md -->
   为了让计数器收集禁用 JavaScript 的用户的数据，请将 `noscript` 元素添加到网站页面的 HTML 代码中：

   ```
   <noscript><div><img src="https://mc.yandex.ru/watch/XXXXXX" style="position:absolute; left:-9999px;" alt="" /></div></noscript> 
   ```
   <!-- endsource: zh/_includes/code/separate-code-file/id-separate-code-file/4.md -->
    
<!-- source: zh/_includes/buttons/chat-button.md -->
[与我们聊天](https://yandex.com/chat/#/user/036e6a02-3620-9cdc-4c5e-a34667a7379e?utm_source=spravka){.button}

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

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

<!-- source: zh/_includes/code/texing-button.md -->
<div class="cut-button">

{% cut "发送电子邮件" %}

<!-- source: zh/_includes/popup/id-qanda/check1.md -->
如果您无法独立检查计数器，请遵循以下建议。
<!-- endsource: zh/_includes/popup/id-qanda/check1.md -->

{% list tabs radio %}

- 数据未发送到控制台

  {% include [qanda-check2-2](../_includes/popup/id-qanda/check2-2.md) %}

  {% cut "这些建议没有帮助" %}


  <div style="padding: 15px;
         margin: 10px 0;
         background: #FFFFFF;
         border-radius: 10px;
         border: 1px solid var(--g-color-line-generic);">
    <iframe style="background: #FFFFFF;"
            height="700"
            width="100%"
            frameborder="0"
            src="https://forms.yandex.com/surveys/1706/?&theme=Dannye v konsoli ne otpravlyayutsya&iframe=1&lang=en">
    </iframe>
  </div>

  {% endcut %}

- 控制台有数据，但报表不显示

  <!-- source: zh/_includes/popup/id-qanda/check2-3.md -->
  这意味着信息正在传输到 Yandex Metrica。 但是，由于以下任一原因，数据可能不会显示在报表中：
  <!-- endsource: zh/_includes/popup/id-qanda/check2-3.md -->

  <!-- source: zh/_includes/popup/id-qanda/check2-4.md -->
  - 数据被发送到具有不同编号的计数器。
  - 计数器设置中的 **Filters** 选项卡指定了过于严格的筛选条件。 删除不需要的筛选条件。
  - 计数器设置中的 **Filters** 选项卡已启用 **Don't count my sessions** 筛选条件。 这意味着计数器不会统计您自己的会话。 尝试使用您的浏览器在“隐身”模式下访问该网站。
  <!-- endsource: zh/_includes/popup/id-qanda/check2-4.md --> 

  {% cut "这些建议没有帮助" %}


  <div style="padding: 15px;
           margin: 10px 0;
           background: #FFFFFF;
           border-radius: 10px;
           border: 1px solid var(--g-color-line-generic);">
    <iframe style="background: #FFFFFF;"
              height="700"
              width="100%"
              frameborder="0"
              src="https://forms.yandex.com/surveys/1706/?&theme=Dannye v konsoli est, no otchety ne otobrazhayutsya&iframe=1&lang=en">
    </iframe>
  </div>

  {% endcut %}

{% endlist %}

{% endcut %}

</div>

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

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

<!-- 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 -->

[*кода-счетчика]: 代码片段由两部分组成：`script` 元素中的 JavaScript 代码以及 `noscript` 元素中的 HTML 代码。
{% included (../_includes/popup/id-qanda/check2-2.md) %}
   发生这种情况的原因可能如下：
   - 计数器安装不正确。 例如 CMS 修改了计数器代码。 重新安装计数器或联系您的 CMS 的支持服务。
   - 损坏的脚本导致 Yandex Metrica 计数器无法在网站上运行。 您可以在浏览器控制台中检查这一点。


{% endincluded %}