Google Analytics Multi-device Tracking and Measurement Protocol

by Member of TrueSocialMetrics team ~ 4 min

Nowadays, marketing and analytics are facing a new challenge after the PC era. It’s multi-device tracking. Customers are using smartphones, laptops, home pcs, etc., and our analytics software counts them as different user sessions. But they aren’t. And the wrong data doesn’t help to answer the main question: in which channels should you invest your time and money to succeed?

Let’s track multi-device sessions

First, you have to sign up for a GA Universal account. google analytics universal create account

The general code looks like:

<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','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-XXXX-Y');
  ga('send', 'pageview');
</script>

But GA Universal is providing a special userId parameter in the configuration to setup your custom ID for GA cookies instead of the random userId. This significant change helps us to track users on multiple devices, because we can use custom visitor ID based on customer email or ID in your database to merge smartphone/laptop/pc sessions into one cross-device session.

So let’s add userId options to auth users and save GA auto-generated cookie id only for non-auth visitors.

<?if ($isAuthenticatedUser):?>
    ga('create', 'UA-XXXX-Y', {'userId': '<?=$userId?>' });
<?else:?>
    ga('create', 'UA-XXXX-Y');
<?endif;?>

$isAuthenticatedUser - true, if current visitor is authenticated
$userId - authenticated user’s unique id from database

This small change makes a significant difference: from now on all users who sign in to your website will be tracked correctly through all those devices. But as well as using a new GA Universal account, I’d recommend you also keep the Classic GA code on your website until GA Universal collects enough data.

How it works ?

Before sign in visitors have a _ga cookie: google analytics universal create account

But after sign in google analytics universal create account

As you can see, the _ga cookie was changed after users’ authorization, but it works properly and all history from the visitor session will be merged into the customer session. So, despite the fact of changing ID, the session transition in GA works perfectly well, because at the moment when the user sees the first page after authorization (or registration) - GA gets access to legacy visitor id_ga value and new setup by userId parameter. All those allow GA to merge 2 user sessions: before and after sign in (sign up) into 1 global session. The result: you will observe smooth session startup from the first visit and flowing through all user's devices.

We’ve tested it for the last 4 months and it works brilliantly!

Update from 10/17/2013

If you're worried about using the same userId on different websites, don’t be. There's no issue because GA uses cookie value + domain as a unique internal identifier. It means, if 2 websites set up the same userId value based on customer email, the data won't be corrupted, because they are using different domains (and different GA profile id).

Update from 03/22/2015

Updated article to follow last chnages in Google Analytics: clientId was renamed to userId.

Measurement protocol

The next big change is a measurement protocol. With the help of this new version you can track actions internally from your application without having to add some ugly paraments to urls to tell GA about a successful payment or registration.

The simple code looks like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/path/for/soap/url/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    ‘v’ => 1,
    ‘tid’ => ‘UA-XXXX-Y’,
    ‘cid’ => $_COOKIE[‘_ga’],
    ‘t’ => ‘pageview’,
    ‘dp’ => ‘/payment/success’,
));
curl_exec($ch);

there:
v - protocol version, now "1"
tid - GA tracking id
cid - client id (it can be md5(email) or md5(id) for auth user)
t - hit type (pageview/event)
dp - page for pageview

This code will send pageviews to GA for the page “/payment/success”. You can use it in your application to track any actions. Also there’s the same simple code for event tracking (if you want to use it in a/b testing or fire some application event).

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://example.com/path/for/soap/url/");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
    ‘v’ => 1,
    ‘tid’ => ‘UA-XXXX-Y’,
    ‘cid’ => $_COOKIE[‘_ga’],
    ‘t’ => ‘event’,
    ‘ec’ => ‘payment’,   // category
    ‘ea’ => ‘small’,         // package
    ‘el’ => ‘small package 1 year subscription’,     // label
    ‘ev’ => ‘324’, // value of this action $324 in my case
));
curl_exec($ch);

So, finally, you will have programmatically generated events in GA. google analytics universal create account

Sources (You can find more samples under the links below): - The Magic of Universal Analytics: Strategy, Tactics, Implementation Tips



When you’re ready to rock your social media analytics

give TrueSocialMetrics a try!


Start Trial
No credit card required.






Continue reading




Google Plus Communities Analytics: Social Media Communities
When you choose in which community to participate or compare your own community with competitive ones you usually look at the number of followers. The more, the better. But on practice, it’s not always the case. What really matters is how active they are. Nobody likes deadly silent unresponsive communities.


The Most Viral Type of Pins: Barney’s on Pinterest
I stumbled upon an interesting technique Barney’s uses to present their clothes and accessories on Pinterest. They always make 2 types of pins for each product: an “Individual Product” and a “Product in a Setting”. I couldn’t just walk by and not compare the effectiveness of both methods of product representation. Which one is more likely to be re-pinned and liked - an “individual product” or a “product in a setting” format?


Editorial Calendar Template to Save Time and Boost Your Social Presence
Let’s face it. Content creation is messy. You’ll need to post regularly to see results; don’t just sit and wait for your muse. But that’s not how it usually happens. There’s a way to make it less chaotic, though: Using Editorial calendar. Today I’m happy to share with you a template from Editorial calendar that we use here at TrueSocialMetrics.


10 Tiny TrueSocialMetrics Tweaks to Save More Time On Your Social Media Analytics
If you are already using TrueSocialMetrics to analyze your social media pages (Thanks! We love you!) and are looking for ways to make the experience better and faster, you’ve come to the right person! :) I want to share with you 10 things you can do in your True account that will help you save tons of time everyday and gain insights faster.