竟然没有人汇总怎么往AMP或MIP页面里插入广告或是统计代码,网上搜到的都是零散的片段。这年头谁还会去看官方说明文档呢?哎,这份苦差事就交给我吧!

Google AdSense

AMP

<head></head>处引入脚本:

<script async custom-element="amp-auto-ads" src="https://cdn.ampproject.org/v0/amp-auto-ads-0.1.js"></script>

<body></body>处引入组件:

<amp-auto-ads type="adsense" data-ad-client="ca-pub-id"></amp-auto-ads>

下同。

MIP

<script async src="https://c.mipcdn.com/static/v1/mip-adsense/mip-adsense.js"></script>
<mip-adsense ad-client="ca-pub-id" ad-slot="id" ad-format="auto"></mip-adsense>

Google analytics

AMP

<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<amp-analytics type="gtag" data-credentials="include">
    <script type="application/json">
      {
        "vars": {
          "gtag_id": "UA-XXXXX-Y",
          "config": {
            "UA-XXXXX-Y": {
              "groups": "default"
            }
          }
        }
      }
    </script>
  </amp-analytics>

MIP

参考:mip Google Analytics统计

<script src="https://c.mipcdn.com/static/v1/mip-stats-google/mip-stats-google.js"></script>
<mip-stats-google>
    <script type="application/json">
        [
            ["create", "UA-XXXXX-Y", "auto"],
            ["send", "pageview"]
        ]
    </script>
</mip-stats-google>

百度统计

AMP

参考:在 AMP 页面中添加百度统计

<amp-analytics type="baiduanalytics" id="analytics2">
    <script type="application/json">
      {
        "vars": {
          "token": "你的token"
        },
        "triggers": {
          "trackPageview": {
            "on": "visible",
            "request": "pageview"
          }
        }
      }
    </script>
  </amp-analytics>

MIP

参考:mip-stats-baidu 百度统计

<script src="https://c.mipcdn.com/static/v2/mip-stats-baidu/mip-stats-baidu.js"></script>
<mip-stats-baidu>
  <script type="application/json">
    {
      "token": "你的token",
      "_setCustomVar": [1, "login", "1", 2],
      "_setAutoPageview": [true]
    }
  </script>
</mip-stats-baidu>