How to check ga dimensions(Google Analytics)? - google-analytics

As per my code I am setting the 'dimension1' field.
Defined fields: email(contains user email)
ga('set', 'dimension1', email);
ga('send', 'pageview');
How to see the dimension1 values in GA?
Original requirement: Track id of each user.

Related

Google Analytics - Match UserID with my site's account ID

Each of my registered clients have an unique account ID (eg: agent n°: 00173393).
I want to retrieve this information through google analytics... It's not a personal information and for statistics use only.
I implemented userID, but how to match userID and the accounts IDs ?
Is it possible to create a variable for the account ID number ?
Why don't you use the account ID as User ID as well? That way you would have a 1-to-1 match:
ga('create', 'UA-XXXXX-Y', 'auto', {
userId: accountId
});
Please note however that the User ID is not exposed back by Google Analytics. If you want to retrieve it, you need to save it inside a custom dimension:
Admin -> Property -> Custom Dimensions -> Create one with User scope
Add custom dimension tracking to your code
For instance:
ga('create', 'UA-XXXXX-Y', 'auto', {
userId: accountId,
});
ga('set', 'cd1', accountId); // if it's custom dimension n1
ga('send', 'pageview');
Then you can retrieve the User ID via the UI:
or the API

ClientId not getting captured

We have three custom dimensions defined in Google Analytics:
ClientId (dimension1): Session-scoped
SessionId (dimension2): Session-scoped
Hit Timestamp (dimension3): Hit-scoped
And these are being fed from an on-page script:
$(document).ready(function() {
(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');
function getClientId() {
try {
var trackers = ga.getAll();
var i, len;
for (i= 0, len = trackers.length; i < len; i += 1) {
if (trackers[i].get('trackingId') === 'UA-XXXXXXXX-1') {
return trackers[i].get('clientId');
}
}
}
catch(e){
//do nothing
}
return 'false';
}
function getSessionId() {
return new Date().getTime() + '.' + Math.random().toString(36).substring(5);
}
function getTimeStamp() {
return new Date().getTime();
}
ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('require', 'displayfeatures');
ga('require', 'linkid', 'linkid.js');
var clientId = getClientId();
var sessionId = getSessionId();
var timeStamp = getTimeStamp();
ga('send', 'pageview', {
'dimension1' : clientId,
'dimension2' : sessionId,
'dimension3' : timeStamp
});
});
</script>
Now, the marketing team tells us that ClientId is not getting captured. They shared data where we had some 24,000 rows, out of which only two had valid client ids. By contrast, Session ID and Hit Timestamp are being captured perfectly.
When I do a quick check on the website (by assigning the code for getClientId() to another temporary function and calling it), I get the ClientId.
I'm really not sure what's causing this to be missed on the live website. Can someone point out something that might be awry?
You should consider loading the GA library as recommended in the <head> section of your site rather than on $(document).ready. This is so that the analytics tracking has the soonest possibility to start tracking user engagements. In addition, if you load the analytics after DOM is ready or after the page has completely loaded, then there is the chance you miss some metrics if, for example, a user lands on your page, and then navigates away before the analytics library has a chance to capture their data.

How to use a custom event fieldsObject

Let say my endpoint give me response {success:true,id:1234}. I want to send this data to Google Analytics on particular event hit. How I can do that?
I tried:
ga('send', 'event', 'GetProfile', 'onPageLoad','getprofile',1,this.userdata);
this.userdata = {success:true,id:1234} .
I can only see the getprofile as event level and value as 1, but I cannot find where my this.userdata logged

Does google analytics keep custom dimensions value over time?

Does a "user scope" dimension will keep track on the changed to the dimension value?
Will it remember the value that was set to the dimension on a past period even if it was already being updated?
Will the dimension value it always correlated to the report time period or that it is being overwrite every update?
To back up DalmTo's comment with some evidence I did the science and built a very small test page:
<html>
<head>
<title>Test</title>
<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-XXXXXX-XX', 'auto');
var d = new Date();
var currentTime = d.toLocaleTimeString();
var dimensionValue = 'I am userscope' + d + " - " + currentTime;
ga('set', 'dimension1', dimensionValue);
var dimensionValue = 'I am sessionscope' + d + " - " + currentTime;
ga('set', 'dimension2', dimensionValue);
var dimensionValue = 'I am hitscope' + d + " - " + currentTime;
ga('set', 'dimension3', dimensionValue);
ga('send', 'pageview');
</script>
</head>
<body>
</body>
</html>
So this sends a stupid message plus the datetime as dimension value.
The purpose of the test case was to make sure that nobody but me accesses the site and that all calls come from the same client id/user (albeit in multiple sessions). As an added benefit with just a few hits processing time in GA is just a few minutes, so I got to see the results quickly.
I called this a few times around 12 o'clock and then again a few minutes ago.
Results:
hit scoped custom dimensions are set per hit (no suprise here)
session scoped dimensions get the last value in the session (dito)
user scoped dimensions get the last value in the session. But they are assigned per session, not per user.
Potentially relevent screenshot:
These are two sessions for the same user which nonetheless show two different values for a user scoped custom dimension. So it's like Daimto said, only now you have a test case you can reproduce and do not need to rely on anyone's experience :-)
Of course that means you should take care yourself not to set user scoped dimensions more than once per user (else you might get inconsistencies in your segments).
(For completeness sake this would probably need to be repeated in a UserId view with session stitching enabled, but I doubt that it'll make much of a difference).

Google Analytics: Change userID at runtime in a SPA

The documentation indicates that the userId must be set this way:
ga('create', 'UA-XXXX-Y', { 'userId': 'USER_ID' });
But in a Single Page Application (SPA), the user starts as anonymous and then logs in. So the app will start with:
ga('create', 'UA-XXXX-Y', 'auto');
And when he logs in, then I would like to change to a specific ID for tracking that user, but when I try:
ga('create', 'UA-XXXX-Y', { 'userId': 'USER_ID' });
Nothing happens, the user ID does not appears in subsequent requests.
Which is the right way of setting the userId at runtime?
Thanks.
Unfortunately, the documentation is currently incorrect. It is possible to set the user ID outside of the create method.
The reason your example isn't working is because you're calling create twice. What you want to do is call set. Here's how:
// Create the tracker instance.
ga('create', 'UA-XXXX-Y', 'auto');
// Once you know the user ID, set it on the current tracker.
ga('set', { userId: USER_ID });
Now all subsequent hits sent to GA will be associated with this user ID.
UPDATE:
The user ID documentation now reflects that it can be set outside of the create method.

Resources