The Google Analytics Measurement Protocol is a set of rules that allows developers to make HTTP requests to send raw hit data directly to Google Analytics servers. This allows developers to measure how visitors interact with their business from almost any environment, device or system.
Through the Measurement Protocol you can collect and send raw user interaction hits directly to a Google Analytics server from any device which can be connected to the internet. These devices or systems can be tablet PCs, smart phones, point of purchase systems, or digital appliances (like coffee machines, washing machines, etc.).
What problem it solves
Measurement Protocol is used and helpful for implementing cross-device tracking.
Before you can send your data to the Google Analytics server you need to format your data according to the Measurement Protocol. For example, if you want to send data from your coffee machine to the Google Analytics server, then you first need to format the data according to the Measurement Protocol. Without such formatting, you can’t send data to Google Analytics.
How to send data to Measurement Protocol
This article explains how to format HTTP requests to send payload data (hits) to the Google Analytics Measurement Protocol.
There are 2 elements to sending data to Google Analytics through Measurement Protocol:
- Transport – where and how you send data
- Payload – the data you send
Transport describes where and how to send the payload data. All data must be sent securely with the HTTPS protocol. Send data using either GET or POST requests. Below is an example of transport.
GET or POST https://www.google-analytics.com/collect
Transport consists of:
- GET or POST method: how to send payload data
- Location of GA server: where to send payload data
- URL end point: whether a single or multiple data (hits) should be sent to the Google Analytics server or whether to send the payload data to the Measurement Protocol validation server
Note: Google recommends sending payload data using the POST method because it allows you to send large amounts of data.
Example of POST http request:
user_agent_string – is a formatted user agent string that your web browser sends to a web server in order to identify itself, e.g. browser, platform, and mobile capabilities.
Note: If user agent value is not set, the data above will not be processed.
payload_data – data or hit sent to the Google Analytics server through Measurement Protocol.
Example of GET http request:
For any situation where you cannot send POST data, you can also send the data using HTTP GET requests to the same end point:
GET /collect?payload_data HTTP/1.1
The data you send to the Google Analytics server through Measurement Protocol Is the BODY section of the post request. The body needs to include exactly 1 URL encoded payload and must be no longer than 8192 bytes.
The payload looks like a URL query string where each parameter consists of a key=value pair, and each pair is separated by an & (ampersand) character.
Required Values for All Hits (payload)
Supported Data Types for Measurement Protocol
Following data types are supported by Measurement Protocol in the format of key value pair
Final Event Payload example:
Final PageView Payload example:
Validating Measurement Protocol Hits:
Google provides a great tool to validate Measurement Protocol hits and displays the full hit and its validation status. Here is the link to access the tool:
Note: Hits sent to the the Measurement Protocol Validation Server won’t show up in Google Analytics reports. They are for debugging only.