Hướng dẫn dùng google analytics Informational, Transactional năm 2024

This document demonstrates how to format HTTP requests to send common hit types to the Google Analytics Measurement Protocol. Read the:

  • Protocol reference – a complete description of how to format Protocol requests.
  • Parameter reference – a list of all the parameters accepted by the Protocol.

Sending Required Values

To send user interaction data, make an HTTP POST request to this endpoint.

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

The following parameters are required for each payload:

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

Each payload must contain a valid hit type and each hit type has its own set of required fields. So to send a pageview for the

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

3 page, you would use the following payload:

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

The sections below give examples of common hit types.

Batching multiple hits in a single request

To send multiple hits in a single request, use the

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

4 endpoint instead of

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

5 and specify each payload on its own line.

For example, to send pageview hits for the "Home", "About Us", and "Contact" pages of a website, you could send the following batch request.

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

Batch limitations

In addition to the standard limitations of Measurement Protocol hits, batch requests have the following additional limitations:

  • A maximum of 20 hits can be specified per request.
  • The total size of all hit payloads cannot be greater than 16K bytes.
  • No single hit payload can be greater than 8K bytes.

Sending Common Hit Types

Below are examples of how to send common hit types to Google Analytics. Besides these examples, you can mix and match various parameters to achieve new data relationships. For example, to understand on which page an event occurred, send the pagePath parameter,

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

6, along with event tracking parameters as described below.

Read the parameter reference for a full list of all the parameters you can send to Google Analytics.

Page Tracking

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.

View this hit in the Measurement Protocol Hit Builder.

Event Tracking

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.

View this hit in the Measurement Protocol Hit Builder.

Enhanced Ecommerce Tracking

Enhanced Ecommerce hits should be used in place of hits. If you've already implemented Ecommerce tracking and want to start using Enhanced Ecommerce tracking there are two options:

Use a new property

You can create a new property and send Enhanced Ecommerce hits to the new property.

Migrate an existing property

Migrate any hits to Enhanced Ecommerce hits, following the examples below. Transaction and item data previously collected using hits will not be affected and will remain available in the properties and views to which they were originally sent.

Measuring Impressions

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.

View this hit in the Measurement Protocol Hit Builder.

Measuring Actions

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.


View this hit in the Measurement Protocol Hit Builder.

Combining Impressions and Actions

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.

View this hit in the Measurement Protocol Hit Builder.

Measuring Purchases

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.


View this hit in the Measurement Protocol Hit Builder.

Measuring Refunds

If you need to send refund data using an event and the event is not part of normally measured onsite behavior (i.e. not user initiated), then it’s recommended that you send a event. This will prevent metrics such as bounce rate, session duration, etc. from being affected by the event.

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

0

View this hit in the Measurement Protocol Hit Builder.

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

1

View this hit in the Measurement Protocol Hit Builder.

Measuring the Checkout Process

1. Measuring Checkout Steps

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

2

View this hit in the Measurement Protocol Hit Builder.

2. Measuring Checkout Options

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

3

View this hit in the Measurement Protocol Hit Builder.

Measuring Internal Promotions

Promotion Impressions

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

4

View this hit in the Measurement Protocol Hit Builder.

Promotion Clicks

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

5

View this hit in the Measurement Protocol Hit Builder.

Ecommerce Tracking

To send ecommerce data, send one

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

7 hit to represent an entire transaction, then send an

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

8 hit for each item in the transaction. The transaction ID

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

9 links all the hits together to represent the entire purchase.

Transaction Hit

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

6

View this hit in the Measurement Protocol Hit Builder.

Item Hit

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

7

View this hit in the Measurement Protocol Hit Builder.

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

8

View this hit in the Measurement Protocol Hit Builder.

Exception Tracking

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

9

View this hit in the Measurement Protocol Hit Builder.

User Timing Tracking

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

0

View this hit in the Measurement Protocol Hit Builder.

App / Screen Tracking

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

1

View this hit in the Measurement Protocol Hit Builder.

Using a Proxy Server

Some environments are not able to send hits to Google Analytics directly. Examples of this are older mobile phones that can't run JavaScript or corporate intranets behind a firewall. In these cases it's common to send requests to a proxy server that then uses the Measurement Protocol to forward the hits on to Google Analytics.

To collect the IP and user agent from the client device and not the proxy server, you can specify both values in the measurement protocol, and they will override the values Google Analytics normally obtains from the request headers.