使用 Measurement Protocol

本文档将介绍如何向 Measurement Protocol 发送常规匹配。

概览

本文档将演示如何设置 HTTP 请求的格式,以便向 Google Analytics(分析)Measurement Protocol 发送常见匹配类型。请参阅:

  • Protocol 参考 – 关于如何设置 Protocol 请求格式的完整说明。
  • 参数参考 – 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 个匹配。
  • 所有匹配负载的总大小不能超过 16K 字节。
  • 单个匹配负载不能超过 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 匹配构建器中查看此匹配。

事件跟踪

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 匹配构建器中查看此匹配。

增强型电子商务跟踪

增强型电子商务匹配应用于代替电子商务匹配。如果您之前实现了电子商务跟踪,现在想要开始使用增强型电子商务跟踪,则有两种选择:

使用新的媒体资源

您可以创建一个新的媒体资源,并将增强型电子商务匹配发送到该媒体资源。

迁移现有媒体资源

要将任意电子商务匹配迁移到增强型电子商务匹配,请参照以下示例。此前使用电子商务匹配收集到的交易和商品数据不会受到影响,可以继续在对应的媒体资源和数据视图中查看。

衡量展示

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 匹配构建器中查看此匹配。

衡量操作

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 匹配构建器中查看此匹配。

合并展示和操作数据

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 匹配构建器中查看此匹配。

衡量购买

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 匹配构建器中查看此匹配。

衡量退款

如果您需要使用事件来发送退款数据,但该事件不属于通常衡量的网站上行为(即并非由用户发起),则建议您发送非互动事件。这可让跳出率和会话时长等指标免受该事件的影响。

 // 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 匹配构建器中查看此匹配。

 // 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 匹配构建器中查看此匹配。

衡量结帐流程

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 匹配构建器中查看此匹配。

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 匹配构建器中查看此匹配。

衡量内部促销

促销信息展示
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 匹配构建器中查看此匹配。

促销信息点击
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 匹配构建器中查看此匹配。

电子商务跟踪

要发送电子商务数据,您需要发送一个 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 匹配构建器中查看此匹配。

商品匹配

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 匹配构建器中查看此匹配。

社交互动

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 匹配构建器中查看此匹配。

异常跟踪

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 匹配构建器中查看此匹配。

用户计时跟踪

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 匹配构建器中查看此匹配。

应用/屏幕跟踪

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 匹配构建器中查看此匹配。

使用代理服务器

在某些环境下无法直接向 Google Analytics(分析)发送匹配。例如,无法运行 JavaScript 的旧式手机,或是位于防火墙后的企业内部网。在这些情况下,常见的做法是向代理服务器发送请求,然后该服务器再使用 Measurement Protocol 向 Google Analytics(分析)转发匹配。

要从客户端设备而非代理服务器收集 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 匹配构建器中查看此匹配。