Google Measurement Protocol not registering custom dimensions - google-analytics

I'm using the Google Analytics Measurement Protocol to register events with HTTP requests. I'm building this for a Roku app I'm working on. The events are getting sent and registered, but it seems the custom dimensions aren't working.
Here's what I'm sending
Destination URL:
https://ssl.google-analytics.com/collect
POST body:
v=1 // version
&dh=www.my-site.com // document hostname
&tid=UA-XXXXXXXX-X // Tracking ID / Property ID
&uid=(a user id) // user id
&cid=(an assigned UUID) // Anonymous Client ID
&sr=1280x720 // screen resolution of the tv
&t=event // Event hit type
&ec=ExampleCategory // Event Category. Required
&ea=ExampleAction // Event Action. Required
&el=ExampleLabel // Event label
&cd1=WAAA // Custom dimension 1, Station
&cd2=(some id) // Custom dimension 2, User ID
&z=1432199801 // Cache buster
When I go to Behavior > Events > Top Events in Google Analytics and set the secondary dimension as being Station for instance, none of the events show up. The events show up when I take off the secondary dimension though, so it seems like Google Analytics just isn't liking something about the custom definitions.
I've tried waiting 24 hours to see if there's a time delay problem, but that didn't help.
I've also made sure to turn on the custom definitions and set them to "Hit" scope type from the Admin panel in Analytics as well.
When I submit the above to https://ssl.google-analytics.com/debug/collect (the validation server that returns errors if there's something wrong with the request), it says the request is valid.
Any idea what could be going on here?
Thanks for the help!

Related

Measuring Page Load Time of single-page app with GA

I'm using Google Analytics in an SPA. For any virtual page redirects (like an AJAX call to refresh the body of the page), I'm getting a page load time of 0ms. Is there a way to track how long that takes, just as if it was a full page refresh? I'm hoping to include how long it takes for the AJAX call and also the time to download and display images that are loaded as a result.
As you have found, Google Analytics will not provide page timings for SPA's. This includes if you increase the site speed sample rate to 100. This is because Google Analytics calculate the page timings using the Navigation Timing API.
For example, DOM loaded would be:
$(document).ready(console.log((Date.now() -
performance.timing.domComplete)/1000))
To over come this problem, you will need to use custom metrics. The solution has three steps.
1) Set up a custom metric in GA.
Go to Admin > Property > Custom Definitions > Custom Metric.
Create a new Custom Metric, with the scope of Hit and the formatting type of time. Note: Specify time in seconds, but it appears as hh:mm:ss in your reports.
2) Set up a timer.
You will need to capture the time when you want to start the measurement of page load time.
An example solution to this might be by decorating all of your internal links, e.g:
$('a[href*="stackoverflow"]').click(function(){
time1 = Date.now()
})
3) Send the time eclipsed (in sec) to Google Analytics on the virtual pageview event.
When the virtual pageview event occurs (which triggers your virtual pageviews), retrieve the difference between the current time (Date.now()) and the time which the timer was started (time1).
Using Google Tag Manager, a custom javascript variable can be created as below:
function(){
return (Date.now() - time1)/1000
}
This value then needs to be sent with the pageview, against the custom metric index set up in step1.
ga('send', 'pageview', {
'metricX': pageLoadSpeed
});
Using the custom metric along with calculated metrics (e.g. {{virtualPageTimings}}/{{pageViews}}, you will be able to calculate your average virtual page timings.
Bonus:
To make the measurement more accurate, set up a secondary custom metric to count the number of virtual pageviews. This will make sure that pageviews which users are directly navigating to are not taken into consideration.
To do this, create a custom metric with the scope hit and the formatting integer.
Then with every virtual pageview, send the value 1 against the custom metric index. E.g:
ga('send', 'pageview', {
'metricX': pageLoadSpeed,
'metricX': 1
});
This allows for the calculated metric:
{{virtualPageTimings}}/{{virtualPageViews}}
If you checkout the Google Analytics docs, you can find out about the siteSpeedSampleRate option, which basically allows you to turn on your site tracking beacons for a percentage of your users.
By default this value is at 1, but I'm assuming you might want to turn it to 100. It might affect a bit in term of network usage since it will have to transfer more data to GA, so take that into account depending on your users and how they access your website (through mobile, bad coverage in some countries...).
You'll have to modify your tracking code to integrate something like the following:
ga('create', 'UA-XXXX-Y', { siteSpeedSampleRate: 10 })
You can send the timing manually, As everything on Google Analytics. But it's a little bit tricky to do,if I'm honest I would no do it unleast it's necessary. All the data on the time report are based on a hit called timing, this hit is send after the pageView and contains the following information.
If you can see my example, I forced the tool to send the hit, just after the pageview goes another hit with a special list of parameters.
plt : Specifies the time it took for a page to load. The value is in milliseconds.
pdt : Specifies the time it took for the page to be downloaded. The value is in milliseconds.
dns : Specifies the time it took to do a DNS lookup.The value is in milliseconds.
rrt : Specifies the time it took for any redirects to happen. The value is in milliseconds.
srt : Specifies the time it took for the server to respond after the connect time. The value is in milliseconds.
tcp : Specifies the time it took for a TCP connection to be made. The value is in milliseconds.
dit : Used to send a random number in GET requests to ensure browsers and proxies don't cache hits. It should be sent as the final parameter of the request since we've seen some 3rd party internet filtering software add additional parameters to HTTP requests incorrectly. This value is not used in reporting.
clt : pecifies the time it took for the DOMContentLoaded Event to fire. The value is in milliseconds.
More info of this parameters on : https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters and
You can see more info of this hit on
https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings
So what happens now?, if I launch another pageview in this SPA, the second pageview on the same page will not carry this hit and you will ever get 0 of loadtime. You can use the command as the official documentation but if you use it you will notice that is not the same hit (i have to double check that). Other option is send it manually using the command 'send' and attaching the desire information. Check your pageview hit structure to be sure that your timming is actually attaching to your previous hit.
The comand to send the timming after the pageview is send will be something like that, use the &dl parameter or 'dp' parameter to attach the timming to the ajax page.
ga('send', {
hitType: 'timing',
'&plt': 1,
'&pdt': 1,
'&dns': 1,
'&rrt': 1,
'&srt': 1,
'&dit': 1,
'&clt': 1,
'&dl': 'http://cl.edreams.com/',
});
Now all the values '1' needs to be updated for the correct one, now how to determine the time of each parameter not sure at all. Also remember that the sampling by default is only for the 1% of the sessions, send this hit only in a few cases.
Disclaimer : This is a very experimental implementation, we are forcing the Js to send unexpected information. Test it well before to pass it to a final project
Greetings

How to tie together front and back end events in google analytics?

I am tracking user events on the front end with google analytics, but I would also like to send back end events and be able to match up events for the same user in google analytics.
It looks like I should be able to pass the uid parameter: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uid but it looks like I also have to pass the tid parameter https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#tid .
The docs say that "All collected data is associated by this ID" (the tid).
What should I pass for the tid? Why can't I just pass the uid, if that is supposed to be a mechanism for tying events together?
I would like the backend to pass the uid to the front end (actually a one-way hash of the email), and then refer to the user in google analytics with this uid.
Is this feasible? I'm a bit confused about how to implement this.
Many thanks!
The "tid" - Tracking ID - is the Web Property, i.e. the "slot" in your Analytics account that the data goes to. If you do not send a tracking id the calls will disappear in limbo. You find the tid in your property settings under "Tracking Code". It is a string that starts "UA-" and so is also sometimes referred to as UA-ID).
The User ID will not help you to identify users, at least not by default, since it is not exposed in the Analytics interface (it should really be called the "cross device identification id", since that is what it's for). You need to create a custom dimension and pass the value of the User ID there if you want to identify users. Per TOS you must take care that no third party, including Google, can resolve your User ID (or any other datapoint) into something that identifies a person, altough of course you can use yourself to connect data to other data in your backend system.
Actually there is a proper way. I've implemented this for myself.
There's a Client ID parameter, that should be passed with your requests.
And here's you have two options:
Create this client id manually (by generating UUID) on server-side and pass it to front-end. Then use this value when you create your tracker and also use it for server-side requests.
//creating of a tracker with manually generated client id
ga('create', 'UA-XXXXX-Y', {
'storage': 'none',
'clientId': '76c24efd-ec42-492a-92df-c62cfd4540a3'
});
Of course, you'll need to implement some logic of storing client id in cookie, for example.
You can use client id that is being generated automatically by ga and then send it to the server-side by your method of choice. I've implemented it through cookies:
// Creates a default tracker.
ga('create', 'UA-XXXXX-Y', auto);
// Gets the client ID of the default tracker and logs it.
ga(function(tracker) {
var clientId = tracker.get('clientId');
//setting the cookie with jQuery help
$.cookie("client-id", clientId , { path : "/" });
});
Then on the back-end just access this cookie and use that client id for your requests.
Also some information con be found here: What is the client ID when sending tracking data to google analytics via the measurement protocol?

Universal Google Analytics virtual pageview location

Situation
On our site we implement virtual pageviews for ajax filters and page scrollings.
When user open Rent page on our site:
real url: "/rent"
send to GA:
ga("send", "pageview", {page: "/rent/"})
When user scroll to second page:
we change real url via js: "/rent/?page=2"
send to GA (we care only about main page):
ga("send", "pageview", {page: "/rent/"})
Or when user drills down:
we change real url via js: "/rent/appartment/?page=2"
send to GA (we care about path):
ga("send", "pageview", {page: "/rent/appartment"})
But we noticed that really GA code does not take current real url (window.location) each time we call ga, but uses first time location. So when user drill down GA code send:
GET collect?...&t=pageview&dl=http://example.com/rent&dp=/rent/appartment&...
....
Referrer: http://example.com/rent/appartment
Here dl parameter is location of our page (not really current location) and dp is a page parameter in ga call. Notice that referrer is ok.
We decided to provide ga with real location (like in referrer) and change code to:
ga("send", "pageview", {page: "/rent/appartment", location: "http://example.com/rent/appartment"})
Problem
From now on I was satisfied, but was not guys that uses GA to analyze Paid Search effectiveness: bounce rate raised drammaticaly (something like 20% to 70%).
Drilling to the problem I've noticed that GA "loses" user when we remove campaign parameters (utm...) and send location without them.
Questions
Should I care about real location in this situation? How does location affects pageviews? How can I workaround or solve this problem?
Additional information
Seems like the main problem is that we drop CPC parameters when we change location for GA:
landing page: /rent?utm_source=...&... (or gclid for Google)
scroll down to second page: /rent?page=2 - there are no CPC parameters.
Additional information here: https://support.google.com/analytics/answer/1714454?hl=en
But I'm still can't figure out an appropriate solution.
When you send a hit to GA with analytics.js, it's going to send the data it has stored on the tracker object that you created when you invoked ga('create', ...);. You can override those values by passing in an object (as you did), but if no overrides are specified, that tracker data is used.
When the tracker object is created it collects data about the current page (e.g. url, title, window size, etc) and stores that information. It does not recollect that info before you send hits. You have to update it if that data changes.
That means if you're updating the page information in an AJAX site and want analytics.js to "remember" that you've updated the page, you'll have to set it on the tracker. The advantage to setting it on the tracker is that if you send other hit types (e.g. events, social, exceptions), you won't have to specify those new values each time.
So, instead of doing:
ga("send", "pageview", {page: "/rent/appartment"})
Do this:
ga("set", {page: "/rent/appartment"});
ga("send", "pageview");
Now, if you send an event later, the event will be associated with the apartment page.
UPDATE: there is now an official guide for how to properly track single page applications with Google Analytics.

Google Analytics - How to get the client_id (anonymous client id) sent with Measurement Protocol

we are seding requests like this throught the google analytics measurement protocol to track user interaction there. For us its important to be to link a interaction to a specific user so we where happy to be able to set the "cid" attribute of the protocol.
my problem is that I cannot find this Client ID somewhere inside the reports of google analytics. Even when customizing the reports I cannot find a Dimension which allows me to display this information.
http://www.google-analytics.com/collect?
v=1 // Version.
&tid=UA-XXXX-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.
&cm18=8000
&cd6=crocodile
Do you have an idea of how the cid could be found there?
Client id is assigned to a user(client) viewing your site by google analytics. Here is an example of how you can get the client id if you're using jQuery.
$(document).ready(function() {
setGaClientId();
});
var setGaClientId = function(){
if(typeof(ga) != 'undefined'){
ga(function(tracker) {
console.log(tracker.get('clientId')); //actually do something here
});
}
}

tracking sign-ups with google analytics

our marketing consultant has asked me to help with configuring google analytics to setup Goal Tracking for User Signups... from what I've read this is accomplished by assigning the URL of the page the user is directed to after a successful signup to the Goal you are trying to track.
but what if the URL your user is directed to is a URL they regularly visit? e.g. after signing up they are directed to their profile page - which is the same page they visit every time they login. is there some way to configure a referer to go with the goal URL so that it is the pair of them that define the goal? e.g. user visits /profile and is referred by /signup.
further complication: what if after signup the user is directed to a different page depending on the user type? it would be nice to configure a single Goal with multiple URLs (but still using the referer restriction described above).
an alternative would be to use an event... if the server creates a user then it could signal the view to output the javascript code that generates the appropriate event.
or... is there a way for server-side code to send events to google analytics? is there an api?
If your users are directed to is a URL they regularly visit you can make a conditional statement for the goal occasion with a virtual pageview, have a look at this: http://services.google.com/analytics/breeze/en/et_vps/index.html
If after signup the user is directed to a different page depending on the user type, you can use regular expressions to set goal URL pattern, like this ^user./login./(type1|type2|type3)/$, look here for more info: http://services.google.com/analytics/breeze/en/regex_ga/index.html
I came across this question in google and believe I found a more appropriate answer.
Google Analytics now provide a service called Analytics Measurement Protocol.
This will allow you to send server-side requests to track your users activities in your analytics account.
To track a sign up event as mentioned in the question, the best way I can see would be to first create an event based goal in your account with the category set to 'users' and an action as 'sign_up' (you can optionally provide a label and value). Then in your code once a successful sign-up occurs (maybe when a user confirms their activation email link?), send a POST request to www.google-analytics.com/collect with the following payload:
v=1 // Version.
&tid=UA-XXXXX-Y // Tracking ID / Property ID.
&cid=555 // Anonymous Client ID.
&t=event // Event hit type
&ec=user // Event Category. Required.
&ea=sign_up // Event Action. Required.
&el=label // Event label.
&ev=1 // Event value.
A full list of the parameters and their meanings are defined here: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters
You could use an event that is triggered when the user submits the sign-up form :
onsubmit="_gaq.push(['_trackEvent', 'Category','Action','Label','Value']);"
and use the Category / Action / Label data model in Google Analytics to pass whatever data you need to pass, for example :
onsubmit="_gaq.push(['_trackEvent', 'Sign-up','Premium','6-month',6]);"
You then have to configure this Goal in Google Analytics as an Event, which lets you choose which combination of each of the fields Category / Action / Label / Value you want to use.
Another option, maybe less maintenance-heavy, is to use a query-string to identify people that get back to the Profile page after having just signed up. So instead of sending them back to /user/profile after they sign up, you send them back to /user/profile/?sign-up=true.
You will then be able to track these pages as a URL goal in GA using a Regular expression like :
\?sign-up=true
I wrote a helper function for tracking GA events.
function trackGoogleAnalyticsEvent(category, action, label) {
try {
ga('send', 'event', category, action, label);
} catch (e) {
console.warn('Google analytics error: ' + e);
}
}
Usage:
trackGoogleAnalyticsEvent('Signup', 'Signup Success', 'Facebook');

Resources