在 AMP 網頁中加入 Analytics (分析)

Accelerated Mobile Pages (AMP) 是一個用來建構可快速轉譯的靜態內容網頁的平台。AMP 包含可評估使用者互動的 <amp-analytics> 元素,而且內建 Google Analytics (分析) 支援。

評估網頁瀏覽量的基本設定

如要在 AMP 網頁上建立基本 Google Analytics (分析) 安裝作業,請複製這個程式碼片段,並將 <GA_MEASUREMENT_ID> 換成您的 Google 代碼 ID。找出 Google 代碼 ID

<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": "<GA_MEASUREMENT_ID>",
    "config" : {
      "<GA_MEASUREMENT_ID>": { "groups": "default" }
    }
  }
}
</script>
</amp-analytics>

將資料傳送至多個目的地

您可以使用同一個 <amp-analytics> 代碼將資料傳送至多個目的地,只要在 config 指令中加入新的評估 ID <GA_MEASUREMENT_ID_NEW> 即可。

<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" : {
    "config" : {
      "<GA_MEASUREMENT_ID>": { "groups": "default" },
      "<GA_MEASUREMENT_ID_NEW>": { "groups": "default" }
    }
  }
}
</script>
</amp-analytics>

運作方式

<amp-analytics> 元素是延伸的 AMP 元件,在指令碼標記中明確以 custom-element 的形式啟用。

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

<amp-analytics> 元素區塊已設為支援 Google 評估產品。請將 <amp-analytics>type 屬性設為「gtag」(啟用 gtag.js 支援功能),並將 data-credentials 屬性設為「include」(啟用 Cookie)。

<amp-analytics type="gtag" data-credentials="include">
  ...
</amp-analytics>

AMP 規定除了專用的程式庫之外,不允許任何 JavaScript,因此設定是透過 JSON 執行。系統會將含有效 <GA_MEASUREMENT_ID>gtag_id 屬性加到 vars 區塊,下方則會將含有 <GA_MEASUREMENT_ID>: {} 的設定屬性新增為值。

評估事件

使用含有已定義值的 triggers 評估 AMP 網頁中的事件。這些屬性用於觸發條件設定:

  • selector:用於指定目標元素的 CSS 選取器。
  • on:指定事件類型。
  • vars:在 event_name 中指定事件類型,並視需求新增其他參數。

本例說明如何設定基本的 Google Analytics (分析) 事件。建立名為「button」的觸發條件,在使用者點擊 ID 值為「the-button」的元素時觸發。這項觸發條件會將 event_name 的值 (「login」) 傳送給 Google Analytics (分析),並將 method 值設為 Google

<amp-analytics type="gtag" data-credentials="include">
  <script type="application/json">
    {
      "vars": {
        "gtag_id": "<GA_MEASUREMENT_ID>",
        "config": {
         "<GA_MEASUREMENT_ID>": { "groups": "default" }
        }
      },
      "triggers": {
        "button": {
          "selector": "#the-button",
          "on": "click",
          "vars": {
            "event_name": "login",
            "method": "Google"
          }
        }
      }
    }
  </script>
</amp-analytics>

Google Analytics (分析) 事件是 Google Analytics (分析) 專用的事件類別,常用於建立廣告活動報表。您可以在 vars 區塊中使用 event_categoryevent_labelvalue 參數指定這些值:

<amp-analytics type="gtag" data-credentials="include">
  <script type="application/json">
    {
      "vars": {
        "gtag_id": "<GA_MEASUREMENT_ID>",
        "config": {
          "<GA_MEASUREMENT_ID>": { "groups": "default" }
        }
      },
      "triggers": {
        "button": {
          "selector": "#login",
          "on": "click",
          "vars": { 
            "event_name": "login",
            "method": "Google"
          }
        }
      }
    }
  </script>
</amp-analytics>

如要進一步瞭解觸發條件設定,請參閱 amp-analytics 說明文件

修改參數

如要覆寫預設的 Google Analytics (分析) 參數或新增參數,請將所需值加進 config 區塊的 parameter 部分。本例會覆寫 page_titlepage_location 的預設網頁瀏覽和事件值:

<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
  "vars" : {
    "gtag_id": "<GA_MEASUREMENT_ID>",
    "config" : {
      "<GA_MEASUREMENT_ID>": {
        "groups": "default",
        "page_title": "Beethoven symphonies",
        "page_location": "https://www.example.com/beethoven.html"
      }
    }
  }
}
</script>
</amp-analytics>

網域連接器可讓在不同網域上有兩個以上的相關網站計為一個網站。指定要與 "linker": { "domains": [...] } 資源連結的網域:

<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
  "vars" : {
    "gtag_id": "<GA_MEASUREMENT_ID>",
    "config" : {
      "<GA_MEASUREMENT_ID>": {
        "groups": "default",
        "linker": { "domains": ["example.com", "example2.com"] }
      }
    }
  }
}
</script>
</amp-analytics>

根據預設,在已設定 Google Analytics (分析) 的 AMP 網頁中,系統會啟用從 AMP 快取連結到標準網域的功能。若想停用 Google Analytics (分析) 連結網域流量的功能,請在設定參數中加入 "linker": "false"

<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
  "vars" : {
    "gtag_id": "<GA_MEASUREMENT_ID>",
    "config" : {
      "<GA_MEASUREMENT_ID>": {
        "groups": "default",
        "linker": "false"
      }
    }
  }
}
</script>
</amp-analytics>

通用 Analytics (分析) 的網站速度

Google Analytics (分析) 已設為自動收集一小部分網站流量的網站速度資料。您可以變更取樣率來收集更多或減少資料。如要將取樣率設為 100%,請在設定中加入醒目顯示的程式碼:

<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
  "vars" : {
    "gtag_id": "<GA_MEASUREMENT_ID>",
    "config" : {
      "<GA_MEASUREMENT_ID>": {
        "groups": "default",
        "site_speed_sample_rate": 100
      }
    }
  }
}
</script>
</amp-analytics>

如要停止收集網站速度資料,請使用醒目顯示的程式碼:

<amp-analytics type="gtag" data-credentials="include">
<script type="application/json">
{
  "vars" : {
    "gtag_id": "<GA_MEASUREMENT_ID>",
    "config" : {
      "<GA_MEASUREMENT_ID>": {
        "groups": "default",
        "site_speed_sample_rate": 0
      }
    }
  }
}
</script>
</amp-analytics>

AMP 與非 AMP 流量

根據預設,AMP 流量使用的用戶端 ID 與網路流量不同。AMP 網頁的載入速度更快,顯示的流量模式與標準網頁對應的不同;這通常表示 AMP 和非 AMP 流量會有不同的指標。

使用獨立資源來評估 AMP 流量,即可更準確地分析指標,並更準確地掌握流量。如果您需要使用單一資源,請按照下列步驟區分 AMP 和非 AMP 流量:

  • 使用資料來源維度或自訂維度 (通用 Analytics (分析)。
  • 使用自訂事件參數 (Google Analytics (分析) 4)。

對設定進行偵錯

AMP 驗證工具可用來判斷網頁是否符合 AMP HTML 規格。在網頁網址中加入 #development=1,即可開啟驗證工具。

amp-analytics 擴充功能會提供警告和錯誤訊息,協助偵錯及排解設定問題。將 #log=1 新增至網頁網址,以便在網路瀏覽器的控制台中查看記錄的錯誤訊息。

完整範例

這個範例呈現的是單一頁面上包含單一按鈕的完整 AMP 網頁。這項設定會將標準網頁瀏覽資料和「button-click」事件傳送至 Google Analytics (分析):

<!doctype html>
<html ⚡ lang="en">
  <head>
    <meta charset="utf-8">
    <title>AMP gtag demo</title>
    <meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">

    <link rel="canonical" href="self.html" />
    <style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>

    <!-- Load AMP -->
    <script async src="https://cdn.ampproject.org/v0.js"></script>

    <!-- Load amp-analytics -->
    <script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
  </head>
  <body>
    <!-- Configure analytics to use gtag -->
    <amp-analytics type="gtag" data-credentials="include">
      <script type="application/json">
        {
          "vars": {
            "gtag_id": "<GA_MEASUREMENT_ID>",
            "config": {
              "<GA_MEASUREMENT_ID>": { "groups": "default" }
            }
          },
          "triggers": {
            "button": {
              "selector": "#the-button",
              "on": "click",
              "vars": {
                "event_name": "login",
                "method": "Google"
              }
            }
          }
        }
      </script>
    </amp-analytics>

    <h1>Make it so.</h1>
    <div>
      <button type="button" id="the-button">Engage!</button>
    </div>
  </body>
</html>