使用 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 個位元組。
  • 單一命中酬載不得超過 8K 位元組。

傳送常見的命中類型

以下舉例說明如何將常見的命中類型傳送到 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 中查看這筆命中。