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


# 在使用 AMP 技术的网站上安装和设置计数器

Yandex Metrica 计数器执行 JavaScript。 如果您在使用[加速移动页面](https://amp.dev/about/how-amp-works/) (AMP) 的网站上安装代码片段，该网站的页面将不会通过[验证](https://validator.ampproject.org/)。 这是因为 AMP 技术限制了 JavaScript 和 HTML5 的使用。 要解决此问题，您可以通过特殊方式安装计数器。


{% note info %}


请注意，在这种情况下，以下 Yandex Metrica 功能将不受支持：E-commerce、[用户行为回放](https://yandex.com/support/metrica/zh/webvisor/info.md) 以及跟踪 Share 按钮的点击情况。

{% endnote %}


## 在使用 AMP 技术的网站上启用计数器 {#how-to}


{% note alert %}

使用加速移动页面 (AMP) 技术的网站无法收集内容分析数据。

{% endnote %}


更改网站页面上的 HTML 代码：

1. 跟踪使用 AMP 的网站上的用户活动使用额外的 [amp-analytics](https://amp.dev/documentation/components/amp-analytics/?format=websites) 组件。 将其添加到网页代码中，`head` 元素内：
    
    ```javascript
    <head>
        ...
            
        ...
    </head>
    ```
    
1. 编辑 `body` 元素：添加 `amp-analytics` 元素。 要将会话数据传输到 Yandex Metrica，请将 `type` 属性设置为值 `metrika`，并使用 `counterId` 变量来设置计数器号码。
    
    ```javascript
    <body>
        ...
            <amp-analytics type="metrika">
                
            </amp-analytics>
        ...
    </body>
    ```
    


## 传输数据 {#data}


{% note alert %}

当用户打开 AMP 页面时，Yandex Metrica 统计一次[浏览](*pageview) (`页面浏览`）。 这意味着您不需要将此事件传输到 Yandex Metrica。 如果您真的发送了此事件，Yandex Metrica 报表将显示错误的页面浏览次数。

{% endnote %}


要在计数器初始化时传输数据，请使用：

- 用于发送用户会话参数的 yaParams 变量。
- 用于传输事件（例如目标完成等）[触发器](https://amp.dev/documentation/components/amp-analytics/?format=websites#triggers)的属性。

### 发送会话参数和用户参数 {#params}

在会话期间使用 yaParams 变量传输自定义会话参数和用户参数的示例：

```javascript
...
"vars": {
             "counterId": "XXXXXX",
             "yaParams": "{\"key\":\"value\",\"__ymu\":{\"user_param_key\":\"user_param_value\"}}"
},
...
```

您还可以仅传输会话参数，或仅传输用户参数：


{% list tabs %}

- Session parameters

  
  ```javascript
  ...
  "vars":{
               "counterId": "XXXXXX",
               "yaParams": "{\"key\":\"value\"}"
  },
  ...
  ```
  

- 用户参数

  
  ```javascript
  ...
  "vars": {
               "counterId": "XXXXXX",
               "yaParams": "{\"__ymu\":{\"user_param_key\":\"user_param_value\"}}"
  },
  ...
  ```

{% endlist %}


### 准确的跳出率 {#bounce}

要获得准确的跳出率，请使用 [timer](https://amp.dev/documentation/components/amp-analytics/?format=websites#timer-trigger) 触发器属性：

```javascript
{
    ...
    "triggers": {
        "notBounce": {
            "on": "timer",
            "timerSpec": {
                "immediate": false,
                "interval": 15,
                "maxTimerLength": 14
            },
            "request": "notBounce"
        },
        ...
    }
}
```

### 转化 {#goal}

要在点击特定页面元素时跟踪目标完成情况，请使用 [click](https://amp.dev/documentation/components/amp-analytics/?format=websites#click-trigger) 触发器属性。

```javascript
{
    ...
    "triggers": {
        "someGoalReach": {
            "on": "click",
            "selector": "#Button",
            "request": "reachGoal",
            "vars": {
                "goalId": "superGoalId",
                "yaParams": "{\"key\": \"value\"}" // 当目标完成时，事件中的变量值将用作会话参数
            }
        },
        ...
    }
}
```


#|
||
**字段**
|
**类型**
|
**描述**
||
||
`goalId`
|
字符串
|
在 Yandex Metrica 界面中创建 [JavaScript 事件](https://yandex.com/support/metrica/zh/general/goal-js-event.md)目标时设置的目标标识符。
||
|#

### 页面滚动 {#scroll}

您可以使用 [scroll](https://amp.dev/documentation/components/amp-analytics/?format=websites#scroll-trigger) 触发器属性来确定向下滚动到页面上的特定点（页面高度百分比）。 您可以将此事件设置为目标。

```javascript
{
    ...
    "triggers": {
        "halfScroll": {
            "on": "scroll",
            "scrollSpec": {
                "verticalBoundaries": [
                    50
                ]
            },
            "request": "reachGoal",
            "vars": {
                "goalId": "halfScrollGoal"
             }
        },
        "partsScroll": {
            "on": "scroll",
            "scrollSpec": {
                "verticalBoundaries": [
                    25,
                    90
                ]
            },
            "request": "reachGoal",
            "vars": {
                    "goalId": "partsScrollGoal"
            }
        },
        ...
    }
}
```

### 信息流无限滚动 {#next-page-scroll}

无限滚动可用于查看相互跟随的文章。 要统计文章到文章的点击率和每个文章的浏览次数，请使用 [amp-next-page-scroll](https://amp.dev/documentation/components/amp-next-page/#analytics) 触发器。

```javascript
{
    ...
    "triggers": {
        "trackScrollThrough": {
             "on": "amp-next-page-scroll",
             "request": "pageview"
        },
        ...
    }
}
```

### 加载单个页面元素 {#visible}

当页面上的元素显示在用户屏幕上时，使用 [visible](https://amp.dev/documentation/components/amp-analytics/?format=websites#page-and-element-visibility-trigger) 触发器属性统计。

### 计数器代码的一般示例 {#general-amp}

提供代码示例只是为了说明计数器的功能。 复制时，删除注释 (//&amp;lt;...&amp;gt;)，将 XXXXXXXX 替换为您的计数器号码，并根据需要进行其他更改（例如，配置传输用户参数和会话参数）。

```
<body>
    ...
        <amp-analytics type="metrika">
            
        </amp-analytics>
    ...
</body>
```

### 了解详情

- [使用 AMP 的网站上的分析工具集成的一般信息](https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/integrating-analytics.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 -->

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

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

[*pageview]: 当用户导航到某个网站页面时加载该页面。 页面浏览还包括页面刷新、AJAX 站点更新以及使用 [hit 方法](https://yandex.ru/support/metrica/objects/hit.html)发送数据。 

[*goalid]: **类型**

String

**描述**

在 Yandex Metrica 界面中创建 [JavaScript 事件](https://yandex.ru/support/metrica/general/goal-js-event.html)目标时设置的目标标识符。
{% included (../_includes/popup/id-qanda/check2-2.md) %}
   发生这种情况的原因可能如下：
   - 计数器安装不正确。 例如 CMS 修改了计数器代码。 重新安装计数器或联系您的 CMS 的支持服务。
   - 损坏的脚本导致 Yandex Metrica 计数器无法在网站上运行。 您可以在浏览器控制台中检查这一点。


{% endincluded %}