📜 ⬆️ ⬇️

GA Measurement Protocol - complete guide


On Habré there is already one review of the Measurement Protocol, but since there are a number of additions and the presence of cases, I decided to write a complete guide.

Content


Measurement Protocol (MP) is a basic tool by which Google Analytics (GA) collects information. That is, it is a technology that is used to transmit information about page views and other events in GA.

Technical side


The protocol works on HTTP, has a single entry point: www.google-analytics.com/collect

Data can be sent with POST and GET requests.

Required parameters


v - the current protocol version is 1 (v = 1).
tid - the identifier of your resource in GA.
cid - a unique identifier of the user (either the standard GA JS-code puts it in the Cookie, or you need to generate it yourself - we will take a closer look at this below).
t - type of interaction, for example pageview / event (t = event).

That is, the request will look like this:

v=1&tid=UA-123456-1&cid=1482535147.1490885188&t=event 

Since we chose the type of interaction Event, we need to add the required parameters describing the event:

ec (event category) - event category (ec = registration).
ea (event action) - event action (ea = form).

 v=1&tid=UA-123456-1&cid=1482535147.1490885188&t=event&ec=registration&ea=form 

Full list of parameters

GA event transfer



You can send a request using one of the following methods:

POST

 User-Agent: user_agent_string // https://en.wikipedia.org/wiki/User_agent#Format POST /collect HTTP/1.1 Host: www.google-analytics.com v=1&tid=UA-123456-1&cid=1482535147.1490885188&t=event&ec=registration&ea=form 

Get

In order to avoid problems with caching, it is recommended to add z, with the last parameter, with a unique value for each of the requests.

 GET /collect?v=1&tid=UA-123456-1&cid=1482535147.1490885188&t=event&ec=registration&ea=form&z=12345 HTTP/1.1 Host: https://www.google-analytics.com User-Agent: user_agent_string 

Service to verify the correctness of the data sent

Parameters cid, uid, ni


There are often questions on these parameters, so we will take a closer look.

cid
cid is the client id, the unique identifier of the user's client application (for example, a web browser), the cid is stored in a _ga cookie for a specific domain.

The _ga cookie stores something similar to: GA1.2.602320690.1474476467

In the cid parameter you need to transfer everything that is located after the second point. That is, in this case:

 &cid=602320690.1474476467 

Cook _ga is put down at the moment when Google Analytics Tracker is initialized:

 <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-12345678-1', 'auto'); //      ga('send', 'pageview'); //         Google Analytics </script> 

The cid parameter is required for all queries in the Measurement Protocol. If you have a delayed sending of events in GA to MP - you need to save the user's cid on the backend side.

What if there is no cid

Examples of cases:

1) you have not previously saved cid users
2) the user has cookies disabled or some browser extensions block the execution of the google analytics script.

Since the cid parameter is mandatory for sending data to GA, in such cases it is necessary to generate a cid on the backend side.

We generate a cid like this:

 $cookieId = rand(1000000000, 2147483647) . '.' . time(); 

What threatens self cid generation
The submitted data will not be associated with the user on the side of Google Analytics.

But you get a complete picture of the number of events. Because if you didn't send the information on the Measurement Protocol from the backend, it would generally pass by the statistics.

uid

The unique identifier of the user on your system (for example, on the site). The parameter is required to enable the User ID function.

This will allow you to create a separate view with statistics for all logged in users, where you can track the sequence of actions (views, pages, events, payments) for each of the users. A Cross Device report will also be available.

Learn more about User ID: support.google.com/analytics/answer/3123662

With this parameter, we can inform GA that this interaction was performed by a specific user. The parameter can take any string value, but you cannot transfer information that reveals the user's identity (for example, email or last name, first name).

We connected the User ID for all logged in users. But we prod uid not only into the base parameter, but also duplicated it in Custom Dimension 1 (this allowed us to use segments in the User ID view).

In the main GA JS code, this is done like this:

 ga('create', 'UA-XXXXX-Y', 'auto', { userId: USER_ID, 'dimension1': USER_ID }); ga('send', 'pageview'); 

This can be done more elegantly with Google Tag Manager, but this is a topic for a separate article.

To send requests from the backend, add parameters:

 &uid=USER_ID&cd1=USER_ID 

ni

The presence of the parameter will not initialize the new session.

If you do not use this parameter, then with pending events, a new session with empty parameters will be created in GA (the traffic source will go to direct / none, devices will be in (not set), geography will be built relative to your server’s IP). That is, it will be a session that carries no value, winds up the statistics counter and breaks the reporting on traffic sources.

For example, if this is a deferred event - change the status of the transaction in CRM. It usually happens when the user (who is assigned to this transaction) does not have an active session on the site. Therefore, without using & ni = 1, a new session will be created.

In general, for pending events, use & ni = 1.

Measurement Protocol in practice


GA standard tracking code


Even the most standard JS GA code uses the Measurement Protocol when executed. Let's see how it works.

  1. Open the Google Chrome window with the Developer Tools panel (CTRL + Shift + I or F12).
  2. In the address bar, enter habrahabr.ru.
  3. In the panel for developers we are interested in the Network tab. If you first opened the panel for developers, then opened the site - in this tab there will be information. If another sequence, then the tab will be empty, press F5 to rediscover the page.
  4. In the line Filter we write www.google-analytics.com/collect
  5. Open the details of the request.

You will see similar information:

Request URL:

 https://www.google-analytics.com/collect?v=1&_v=j50&a=643761009&t=pageview&_s=1&dl=https%3A%2F%2Fhabrahabr.ru%2F&ul=en-us&de=UTF-8&dt=%D0%9B%D1%83%D1%87%D1%88%D0%B8%D0%B5%20%D0%BF%D1%83%D0%B1%D0%BB%D0%B8%D0%BA%D0%B0%D1%86%D0%B8%D0%B8%20%D0%B7%D0%B0%20%D1%81%D1%83%D1%82%D0%BA%D0%B8%20%2F%20%D0%A5%D0%B0%D0%B1%D1%80%D0%B0%D1%85%D0%B0%D0%B1%D1%80&sd=24-bit&sr=1920x1080&vp=1109x966&je=0&fl=25.0%20r0&_u=QCCAgEAB~&jid=1630561303&cid=774042187.1492148509&tid=UA-726094-1&cd1=guest&cd4=no&cd5=other&z=1998272259 

Query String Parameters

 v:1 _v:j50 a:643761009 t:pageview _s:1 dl:https://habrahabr.ru/ ul:en-us de:UTF-8 dt:    /  sd:24-bit sr:1920x1080 vp:1109x966 je:0 fl:25.0 r0 _u:QCCAgEAB~ jid:1630561303 cid:774042187.1492148509 tid:UA-726094-1 cd1:guest cd4:no cd5:other z:1998272259 

What do we see here?

A number of parameters that were sent to GA. This is a standard page view. A description of each of the parameters, see the official documentation .

CRM Integration


We are faced with the situation that the Goal Conversion Rate does not provide a complete picture of the quality of traffic sources.

One of the goals on our site is an application for a free introductory lesson. There are cases when the user does not understand what they are talking about, but still fills out a form. Naturally, all applications fall into the CRM and load our sales department.

In GA, any successfully completed form = conversion, so even non-targeted applications will spin the conversion counter.

In order to assess in more detail the quality of each of the traffic channels, we decided to enter additional parameters: Target application and an introductory lesson.

These two parameters we can determine by the status of the transaction in CRM.

Since our CRM is able to send Webhook to our server when changing the status of the transaction, we have configured:


For each of the events set up tracking goals. As a result, got the opportunity to estimate the number of targeted applications.

Transaction tracking


We decided to consider not just billing, but directly the facts of receipt of money to the account of the payment acceptance system.

Quite often this is a postponed event: we decided to pay via bank transfer or payment terminal. Also as an example can be “Cash payment to the courier”.

When the payment acceptance system reported receipt of funds, we send the Transaction to Google Analytics:

 v=1&tid=UA-12345678-1&cid=455045511.1409504244&uid=12345&t=event&tr=225&pa=Transaction&ec=Oplata&ea=Individ&ev=225&ni=1&pr1id=12345_individual_id&pr1nm=Individ&pr1pr=225&pr1qt=1 

Return or partial refund


For more accurate statistics in GA, it is possible to issue returns (cancel transactions).
Since we have a very rare case, we have not automated it.

Example from official documentation:

  // 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. 

More information on the link:

developers.google.com/analytics/devguides/collection/protocol/v1/devguide#enhancedecom

Offline points of sale


Attendance
There are counters of store visitors (offline) with the ability to send information to the web. Using the Measurement Protocol, you can use this information in Google Analytics. By submitting Pageview to GA, you can track how your marketing activities affect the attendance of offline points.

Transactions
All payments can also be sent to GA, and here you can already use UserID.
In the future, you will be able to segment users who make purchases offline, and who - via the Internet.
Marketers will tell you Thank you.

Opening letters



Since the Measurement Protocol can work through GET requests, nothing prevents you from adding a picture to the list:

 <img src=”https://www.google-analytics.com/collect?v=1&tid=UA-123456-1&cid=1482535147.1490885188&uid=12345&t=event&ec=email&ea=open&dp=%2Fletter_name&z=12345”> 

Parameters z, uid and cid substitute depending on the user who leaves the letter.
dp (Document Path) - to make it clear which letter was opened.

Additional resources


→ Official documentation
→ Detailed description of the parameters
→ Service for checking the correctness of sent data

Libraries:
PHP: github.com/theiconic/php-ga-measurement-protocol
Python: github.com/mirumee/google-measurement-protocol
NodeJS: github.com/peaksandpies/universal-analytics
Ruby: github.com/tpitale/staccato

findings


The Measurement Protocol is quite a powerful tool that allows you to automate the uploading of any interactions to Google Analytics, which will help you for analytics in general and will be a great bonus for marketing activities.

If you are also using the Measurement Protocol - write your cases in the comments!

Bonuses from EnglishDom for Habr's readers


Online Courses
We give you access for a year to the English course for self-study "Online course".
To access, simply follow the link .

Individually by Skype
Specialized course " English for IT-specialists "
Classes are held at any time convenient for you.
Promo code for 15% discount: habra215
Valid until May 1st. Enter it when paying or use the link .

Among our students there are already students from Habr, GeekBrains, ITVDN, GoIT, BrainBasket, Softengi, Netology. Join now!

Source: https://habr.com/ru/post/326788/


All Articles