Google Analytics Measurement Protocol

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:

  1. Transport – where and how you send data
  2. Payload – the data you send

1) Transport

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: user_agent_string
POST https://www.google-analytics.com/collect
payload_data

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
Host: https://www.google-analytics.com
User-Agent: user_agent_string

2) Payload

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.

Example:Key1=Value1&Key2=Value2

Required Values for All Hits (payload)

The following required parameters must be in each payload:google-analytics-measurement-1

Supported Data Types for Measurement Protocol

Following data types are supported by Measurement Protocol in the format of key value pair

  • Text
  • Currency
  • Boolean
  • Integer
  • Number

Final Event Payload example:

http://www.google-analytics.com/collect?v=1&t=event&tid=UA-5566555-6&cid=394eb54b-9cb6-4d63-8647-4def4ee29877&ec=Annotations%20%5BMP%5D&ea=Development

Final PageView Payload example:

http://www.google-analytics.com/collect?v=1&t=pageview&tid=UA-5556655-6&cid=394eb54b-9cb6-4d63-8647-4def4ee69477&ec=Annotations%20%5BMP%5D&ea=Development

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:

https://ga-dev-tools.appspot.com/hit-builder/

Note: Hits sent to the the Measurement Protocol Validation Server won’t show up in Google Analytics reports. They are for debugging only.

 

 

 

If you like my articles,