使用 Measurement Protocol

本文說明如何將常見命中傳送至 Measurement Protocol。

總覽

本文說明如何設定 HTTP 要求的格式,將常見的命中類型傳送至 Google Analytics (分析) Measurement Protocol。請閱讀:

傳送必要值

如要傳送使用者互動資料,請向這個端點發出 HTTP POST 要求。

POST /collect HTTP/1.1
Host: www.google-analytics.com

payload_data

每個酬載都需要下列參數:

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.
&t=              // Hit Type.

每個酬載都必須包含有效的命中類型,且每個命中類型都有一組專用的必填欄位。因此,如要傳送 /home 頁面的網頁瀏覽,您應使用下列酬載:

v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome

以下各節將列舉常見的命中類型。

在單一要求中批次處理多個命中

如要在單一要求中傳送多個命中,請使用 /batch 端點 (而非 /collect),並逐行指定每個酬載。

舉例來說,如要傳送網站的「首頁」、「關於我們」和「聯絡資訊」等網頁網頁瀏覽命中,您可以傳送下列批次要求。

POST /batch HTTP/1.1
Host: www.google-analytics.com

v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fhome
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fabout
v=1&tid=UA-XXXXX-Y&cid=555&t=pageview&dp=%2Fcontact

批次限制

除了 Measurement Protocol 命中的標準限制之外,批次請求還具有下列額外限制:

  • 每個請求最多可以指定 20 筆命中。
  • 所有命中酬載的總大小不得大於 16,000 個位元組。
  • 單一命中酬載不得大於 8,000 個位元組。

傳送常見命中類型

以下舉例說明如何將常見的命中類型傳送至 Google Analytics (分析)。除了這些範例,您也可以混搭各種參數,藉此建立新的資料關係。舉例來說,如要瞭解事件發生的網頁,請將 pagePath 參數 p 和事件追蹤參數傳送給我們 (詳情請見下方說明)。

如需可傳送至 Google Analytics (分析) 的所有參數完整清單,請參閱 參數參考資料

網頁追蹤

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=pageview      // Pageview hit type.
&dh=mydemo.com   // Document hostname.
&dp=/home        // Page.
&dt=homepage     // Title.

Measurement Protocol Hit Builder 中查看這筆命中。

事件追蹤

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=event         // Event hit type
&ec=video        // Event Category. Required.
&ea=play         // Event Action. Required.
&el=holiday      // Event label.
&ev=300          // Event value.

Measurement Protocol Hit Builder 中查看這筆命中。

加強型電子商務追蹤

請使用加強型電子商務命中,來取代電子商務 命中。如果您已經導入電子商務追蹤,且想要開始使用加強型電子商務追蹤,則有兩個選項:

使用新資源

您可以建立新資源,並將加強型電子商務命中傳送至新資源。

遷移現有資源

請將所有電子商務命中遷移至加強型電子商務命中,請遵照下列範例。先前使用電子商務命中收集的交易和項目資料不會受到影響,且會保留在原始傳送目的地的資源和檢視畫面中。

評估曝光次數

v=1                                      // Version.
&tid=UA-XXXXX-Y                          // Tracking ID / Property ID.
&cid=555                                 // Anonymous Client ID.
&t=pageview                              // Pageview hit type.
&dh=mydemo.com                           // Document hostname.
&dp=/home                                // Page.
&dt=homepage                             // Title.

&il1nm=Search%20Results                  // Impression list 1. Required.
&il1pi1id=P12345                         // Product Impression 1 ID. Either ID or name must be set.
&il1pi1nm=Android%20Warhol%20T-Shirt     // Product Impression 1 name. Either ID or name must be set.
&il1pi1ca=Apparel%2FT-Shirts             // Product Impression 1 category.
&il1pi1br=Google                         // Product Impression 1 brand.
&il1pi1va=Black                          // Product Impression 1 variant.
&il1pi1ps=1                              // Product Impression 1 position.
&il1pi1cd1=Member                        // Custom dimension.

&il2nm=Recommended%20Products            // Impression list 2.
&il2pi1nm=Yellow%20T-Shirt               // Product Impression 1 name.
&il2pi2nm=Red%20T-Shirt                  // Product Impression 2 name.

Measurement Protocol Hit Builder 中查看這筆命中。

評估動作

v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID / Property ID.
&cid=555                              // Anonymous Client ID.
&t=event                              // Event hit type.
&ec=UX                                // Event Category. Required.
&ea=click                             // Event Action. Required.
&el=Results                           // Event label.

&pa=click                             // Product action (click). Required.
&pal=Search%20Results                 // Product Action List.
&pr1id=P12345                         // Product 1 ID. Either ID or name must be set.
&pr1nm=Android%20Warhol%20T-Shirt     // Product 1 name. Either ID or name must be set.
&pr1ca=Apparel                        // Product 1 category.
&pr1br=Google                         // Product 1 brand.
&pr1va=Black                          // Product 1 variant.
&pr1ps=1                              // Product 1 position.
    

Measurement Protocol Hit Builder 中查看這筆命中。

結合曝光與動作

v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID /  Property ID.
&cid=555                              // Anonymous Client ID.
&t=event                              // Event hit type.
&ec=UX                                // Event Category. Required.
&ea=click                             // Event Action. Required.
&el=Results                           // Event label.

&pa=detail                            // Product action (detail). Required.
&pr1id=P12345                         // Product 1 ID. Either ID or name must be set.
&pr1nm=Android%20Warhol%20T-Shirt     // Product 1 name. Either ID or name must be set.
&pr1ca=Apparel                        // Product 1 category.
&pr1br=Google                         // Product 1 brand.
&pr1va=Black                          // Product 1 variant.
&pr1ps=1                              // Product 1 position.

&il1nm=Related%20Products             // Impression list.
&il1pi1id=P12345                      // Product Impression 1 ID.
&il1pi1nm=Android%20Warhol%20T-Shirt  // Product Impression 1 name.
&il1pi1ca=Apparel%2FT-Shirts          // Product Impression 1 category.
&il1pi1br=Google                      // Product Impression 1 brand.
&il1pi1va=Black                       // Product Impression 1 variant.
&il1pi1ps=1                           // Product Impression 1 position.
   

Measurement Protocol Hit Builder 中查看這筆命中。

評估購買

v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID / Property ID.
&cid=555                              // Anonymous Client ID.
&t=pageview                           // Pageview hit type.
&dh=mydemo.com                        // Document hostname.
&dp=/receipt                          // Page.
&dt=Receipt%20Page                    // Title.

&ti=T12345                            // Transaction ID. Required.
&ta=Google%20Store%20-%20Online       // Affiliation.
&tr=37.39                             // Revenue.
&tt=2.85                              // Tax.
&ts=5.34                              // Shipping.
&tcc=SUMMER2013                       // Transaction coupon.

&pa=purchase                          // Product action (purchase). Required.
&pr1id=P12345                         // Product 1 ID. Either ID or name must be set.
&pr1nm=Android%20Warhol%20T-Shirt     // Product 1 name. Either ID or name must be set.
&pr1ca=Apparel                        // Product 1 category.
&pr1br=Google                         // Product 1 brand.
&pr1va=Black                          // Product 1 variant.
&pr1ps=1                              // Product 1 position.
    

Measurement Protocol Hit Builder 中查看這筆命中。

評估退款金額

如果您需要使用事件傳送退款資料,且事件不屬於正常在網站上的行為 (也就是不是使用者發起),建議您傳送 非互動事件。這樣可以避免事件影響跳出率、工作階段持續時間等指標。

 // Refund an entire transaction and send with a non-interaction event.
v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID / Property ID.
&cid=555                              // Anonymous Client ID.
&t=event                              // Event hit type.
&ec=Ecommerce                         // Event Category. Required.
&ea=Refund                            // Event Action. Required.
&ni=1                                 // Non-interaction parameter.

&ti=T12345                            // Transaction ID. Required.
&pa=refund                            // Product action (refund). Required.
    

Measurement Protocol Hit Builder 中查看這筆命中。

 // Refund a single product.
v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID / Property ID.
&cid=555                              // Anonymous Client ID.
&t=event                              // Event hit type.
&ec=Ecommerce                         // Event Category. Required.
&ea=Refund                            // Event Action. Required.
&ni=1                                 // Non-interaction parameter.

&ti=T12345                            // Transaction ID. Required.
&pa=refund                            // Product action (refund). Required.
&pr1id=P12345                         // Product 1 ID. Required.
&pr1qt=1                              // Product 1 quantity. Required.
    

Measurement Protocol Hit Builder 中查看這筆命中。

評估結帳程序

1. 評估結帳步驟
v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID / Property ID.
&cid=555                              // Anonymous Client ID.
&t=pageview                           // Pageview hit type.
&dh=mydemo.com                        // Document hostname.
&dp=/checkout                         // Page.
&dt=Checkout                          // Title.

&pa=checkout                          // Product action (checkout).
&pr1id=P12345                         // Product 1 ID. Either ID or name must be set.
&pr1nm=Android%20Warhol%20T-Shirt     // Product 1 name. Either ID or name must be set.
&pr1ca=Apparel                        // Product 1 category.
&pr1br=Google                         // Product 1 brand.
&pr1va=Black                          // Product 1 variant.
&pr1pr=29.20                          // Product 1 Price.
&pr1qt=1                              // Product 1 quantity.
&cos=1                                // Checkout step #1.
&col=Visa                             // Checkout step option.
    

Measurement Protocol Hit Builder 中查看這筆命中。

2. 評估結帳選項
v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID / Property ID.
&cid=555                              // Anonymous Client ID.
&t=event                              // Event hit type
&ec=Checkout                          // Event Category. Required.
&ea=Option                            // Event Action. Required.

&pa=checkout_option                   // Product action (checkout_option).
&cos=2                                // Checkout step.
&col=FedEx                            // Checkout step option.
    

Measurement Protocol Hit Builder 中查看這筆命中。

評估內部促銷活動

促銷活動曝光次數
v=1                                      // Version.
&tid=UA-XXXXX-Y                          // Tracking ID / Property ID.
&cid=555                                 // Anonymous Client ID.
&t=pageview                              // Pageview hit type.
&dh=mydemo.com                           // Document hostname.
&dp=/home                                // Page.
&dt=homepage                             // Title.

&promo1id=PROMO_1234                     // Promotion 1 ID. Either ID or name must be set.
&promo1nm=Summer%20Sale                  // Promotion 1 name. Either ID or name must be set.
&promo1cr=summer_banner2                 // Promotion Creative.
&promo1ps=banner_slot1                   // Promotion Position.
    

Measurement Protocol Hit Builder 中查看這筆命中。

促銷活動點擊
v=1                                   // Version.
&tid=UA-XXXXX-Y                       // Tracking ID / Property ID.
&cid=555                              // Anonymous Client ID.
&t=event                              // Event hit type
&ec=Internal%20Promotions             // Event Category. Required.
&ea=click                             // Event Action. Required.
&el=Summer%20Sale                     // Event label.

&promoa=click                         // Promotion action (click). Required.
&promo1id=PROMO_1234                  // Promotion 1 ID. Either ID or name must be set.
&promo1nm=Summer%20Sale               // Promotion 1 name. Either ID or name must be set.
&promo1cr=summer_banner2              // Promotion Creative.
&promo1ps=banner_slot1                // Promotion Position.
    

Measurement Protocol Hit Builder 中查看這筆命中。

電子商務追蹤

如要傳送電子商務資料,請傳送一個 transaction 命中來代表整筆交易,然後為交易中的每個項目傳送 item 命中。交易 ID ti 會連結所有命中,代表整個購物交易。

交易命中

v=1               // Version.
&tid=UA-XXXXX-Y   // Tracking ID / Property ID.
&cid=555          // Anonymous Client ID.

&t=transaction    // Transaction hit type.
&ti=12345         // transaction ID. Required.
&ta=westernWear   // Transaction affiliation.
&tr=50.00         // Transaction revenue.
&ts=32.00         // Transaction shipping.
&tt=12.00         // Transaction tax.
&cu=EUR           // Currency code.

Measurement Protocol Hit Builder 中查看這筆命中。

項目命中

v=1               // Version.
&tid=UA-XXXXX-Y   // Tracking ID / Property ID.
&cid=555          // Anonymous Client ID.

&t=item           // Item hit type.
&ti=12345         // Transaction ID. Required.
&in=sofa          // Item name. Required.
&ip=300           // Item price.
&iq=2             // Item quantity.
&ic=u3eqds43      // Item code / SKU.
&iv=furniture     // Item variation / category.
&cu=EUR           // Currency code.

Measurement Protocol Hit Builder 中查看這筆命中。

社交互動

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=social        // Social hit type.
&sa=like         // Social Action. Required.
&sn=facebook     // Social Network. Required.
&st=/home        // Social Target. Required.

Measurement Protocol Hit Builder 中查看這筆命中。

例外狀況追蹤

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=exception       // Exception hit type.
&exd=IOException   // Exception description.
&exf=1             // Exception is fatal?

Measurement Protocol Hit Builder 中查看這筆命中。

使用者載入時間追蹤

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=timing        // Timing hit type.
&utc=jsonLoader  // Timing category.
&utv=load        // Timing variable.
&utt=5000        // Timing time.
&utl=jQuery      // Timing label.

 // These values are part of browser load times

&dns=100         // DNS load time.
&pdt=20          // Page download time.
&rrt=32          // Redirect time.
&tcp=56          // TCP connect time.
&srt=12          // Server response time.

Measurement Protocol Hit Builder 中查看這筆命中。

應用程式 / 畫面追蹤

v=1                         // Version.
&tid=UA-XXXXX-Y             // Tracking ID / Property ID.
&cid=555                    // Anonymous Client ID.

&t=screenview               // Screenview hit type.
&an=funTimes                // App name.
&av=1.5.0                   // App version.
&aid=com.foo.App            // App Id.
&aiid=com.android.vending   // App Installer Id.

&cd=Home                    // Screen name / content description.

Measurement Protocol Hit Builder 中查看這筆命中。

使用 Proxy 伺服器

部分環境無法直接將命中資料傳送至 Google Analytics (分析)。舉例來說,舊款手機無法執行 JavaScript,也無法在防火牆後執行公司內部網路。在這類情況下,通常會將要求傳送至 Proxy 伺服器,然後使用 Measurement Protocol 將命中轉送至 Google Analytics (分析)。

如要從用戶端裝置 (而非 Proxy 伺服器) 收集 IP 和使用者代理程式,您可以在 Measurement Protocol 中指定兩個值,這些值會覆寫 Google Analytics (分析) 通常從要求標頭取得的值。

v=1              // Version.
&tid=UA-XXXXX-Y  // Tracking ID / Property ID.
&cid=555         // Anonymous Client ID.

&t=pageview      // Pageview hit type.
&uip=1.2.3.4     // IP address override.
&ua=Opera/9.80   // User agent override.

Measurement Protocol Hit Builder 中查看這筆命中。