Set Google Analytics User ID after creating the tracker - javascript

The Google Analytics User ID documentation page shows that you set the User ID when you create the tracker. I have a javascript only project with sign-in after page load, is there any way to set the User ID after login? Something like ga("set", "userid", "UNIQUEID")
On the field reference page it says you can only set user-id in the create function. Are we able to call create again with the user-id? Will that create a new tracker, or override the old one?
EDIT: The beta version of User ID tracking showed that you could specify the userid after creating the tracker using ga('set', '&uid', {{ USER_ID }}); and that would try to unify the session from before the userid was set with the one after. That seems to be very different than what the docs current specify. Is there any way to do this with the current method?

This one works
ga('set', '&uid', '<dynamic user id here>');

The documentation seems to indicate that, with analytics.js, you should do the following:
// Alternatively, you may set the user ID via the `set` method.
ga('set', 'userId', 'as8eknlll');
I'm not sure how this differs from the current answer (which uses &uid), but this solution is working for us.

You should set userId beofre sending event to Google Analytics. I couldn't manage to send userId after sending events pageview and ecommerce:send.

It appears from Justin Cutroni's blog that he's saying that data stitching (in your case matching the userid prior to login to the userid post-login) is not currently offered in Google Analytics.
http://cutroni.com/blog/2014/04/10/understanding-cross-device-measurement-and-the-user-id/
See specifically this sentence: "Google Analytics will not go back in time and stitch every single session from a given user together."

If you are using Google Tag Manager you can set the User ID by added '&uid' as a field to your Google Analytics Setting variable:

Related

Connect data to a google analytics session in javascript

On Google Analytics, in Javascript, is there a way to tie certain behavior to which session ID a user is on?
For example, every time the user changes GA sessions, we want to show the user a different version of the site.
It seems that GA doesn't really offer up the session ID.
We are tracking site versions via Google Analytics custom events. So if there's a way to see which custom events have previously been called for this session, that would also solve the problem.
Something like this would be ideal:
ga(function(tracker) {
var clientId = tracker.get('clientId');
});
However, that is only for the clientId, not for the session ID.
GA UA (analytics.js) doesn't surface session ids. GA UA calculates/generates them later on because of how complex the rules for them are.
GA4 (gtag.js) does generate a session id and stores it on the front-end since the rules for sessions are now way simpler. It stores it in a cookie that starts from _ga and then has your tracking code in it. Here is how it looks here, on this page on SO:
Finally, here is how you get the value from gtag (it's cleaner than parsing the cookie):
gtag('get', 'G-WCZ03SZFCQ', 'session_id', (field) => console.log(field))
If you want to read more on gtag api, here's the documentation.
Note that it wants a callback to get the field:
Not sure why Google decided to make the get function async just to get a cookie. Probably they just decided to follow the same pattern and some items to require async for some reason.
Google Analytics does not provide a way to access the session ID directly. However, you can use the clientId to tie certain behavior to a user's session.
The clientId is a unique identifier that is generated by GA for each user. It is stored in the browser's cookies, and is used to identify a user across multiple sessions.
You can use the ga function to access the clientId as you mentioned in your example:
ga(function(tracker) {
var clientId = tracker.get('clientId');
// use the clientId to tie certain behavior to a user's session
});
You can also use custom events to track different versions of the site, as you mentioned. You can then use the clientId to see which custom events have previously been called for a user's session.
It's worth noting that if you're using GA cookies with the same domain across different subdomains, you'll have to use the linker plugin to properly track the client ID across those subdomains.
ga('create', 'GA_MEASUREMENT_ID', 'auto', {'allowLinker': true});
ga('require', 'linker');
ga('linker:autoLink', ['domain1.com', 'domain2.com']);
In summary, while GA does not offer a direct way to access the session ID, you can use the clientId and custom events to tie certain behavior to a user's session and track different version of the site.

Google Analytics - Different users on same public computer have same User ID metric

I have an app where we track user id's from session and pass to custom metric in Google Analytics.
What we're seeing is that if you log out, and log back in as a different user, both users will use the latest user id within google analytics.
Is it possible to "clear" this common link between each session so that each sign in starts it's own session in GA?
Here's the same thread topic on ga forum: https://support.google.com/analytics/thread/96259041?hl=en
You have to set the clientId when you create the tracker. i.e.:
ga('create', 'UA-XXXX-Y', {
'clientId': '<my custom clientid>'
});
You will have to find a way to get it new on next time and pass it to the snippet above (or via GTM). For example, you can write it in a cookie that is deleted when you close the browser, in this way for the entire duration of the navigation it will be maintained and the next time you open the browser you have another one created and you will use that, and so on.

Google Tag Manager Not Sending Ecommerce Data to Analytics (Standard Ecommerce)

I have been racking my brain on this one and not sure why our Google Tag Manager code isn't sending data to our Google Analytics account.
I have researched multiple questions on here that have similar issues, but haven't worked for me. In addition this is a great guide I've followed https://www.analyticsmania.com/post/ecommerce-tracking-with-google-tag-manager/ - but still no luck in sending the ecommerce data to GA.
Below is the data layer that we're using on our /ticketing/confirm/ page where our customers go when they complete checkout:
<script>
dataLayer = [{
'theatreLocation': 'xxxx',
'theatreState': 'xx',
'userID': '0',
'userEmail': 'xxx',
'movieTitle': 'xxx',
'transactionId': 'xxx',
'transactionTotal': xxx,
'transactionProducts': [
]
}];
</script>
These variables are automatically updated based on the product that is purchased. This tag is also placed above our Google Tag Manager script, which fires just below this one.
Within Google Tag Manager, we have an ecommerce tag connected to our GA account that is a transaction type tag. This tag is set to fire on a trigger of the order confirmation URL. At this point the data should be sending to GA
Tag setup - https://i.imgur.com/M5LhUuO.png
Trigger setup - https://i.imgur.com/dRIZyaJ.png
When checking an order confirmation page with the tag manager extension, it shows our tags being fired on page view of the confirmation page: https://i.imgur.com/mPncBqc.png
View of the overall summary: https://i.imgur.com/TUMxhKS.png
Detailed view of the eCommerce tag: https://i.imgur.com/84ZQaW7.png
I also have the tag assistant extension installed and here is what that looks like: https://i.imgur.com/PGgNp4S.png
And the detailed view of the Analytics tag: https://i.imgur.com/7PDTqgb.png
According to the article I linked above, the detailed view of that analytics tag within tag assistant should have an "other event" listed which should be the transaction info being sent to GA - but as you can see from our screenshot mine does not have that. I'm not sure where I'm going wrong as I think I have everything setup correctly, but clearly there is a break somewhere.
Any help or input is really appreciated.
Remove the settings variable from the tracking ID field in the tag setup. Assuming you want to use the tracking ID you set in the settings variable, uncheck the option to override the settings variable.
If you do want to override the tracking ID from your settings variable, this needs to be a string or variable that returns a tracking ID and not an entire settings variable.

How to reliably get session id generated by Google Analytics?

Using the latest analytics.js code (as of today) is there a way through their JS API I can get the session id?
If you're talking about the client ID, you can get that as follows:
ga(function(tracker) {
var clientId = tracker.get('clientId');
console.log(clientId);
});
Here's where that's stated in the documentation:
https://developers.google.com/analytics/devguides/collection/analyticsjs/cookies-user-id#getting_the_client_id_from_the_cookie
Keep in mind that the client ID is (usually) stored in a cookie, so if the user clears their cookies, they'll be assigned a new client ID that may not match the one you have.
You can read the cookie created by Google but:
It's a privately (by google) chosen variable name/format and could change at any time, thus breaking your code
It appears to break their TOS if you store that information
https://webmasters.stackexchange.com/questions/51866/can-you-track-one-users-session-with-google-analytics-by-using-the-utma-cooki
Linking Google Analytics event logs to unique session ID
http://www.analytics-ninja.com/blog/2011/08/how-google-analytics-calculates-visits.html
Be aware! Some ad-blockers like uBlock Origin replaces GA script with its own polyfill.
This means that you can't correctly check is it real GA script are loaded on your page.
Also uBlock's polyfill changes GA API, so when you using ga(function(tracker){}) callback will never called!
So don't relay on callback and never wait for its calling
The better way is to manually store custom clientId in cookie and tell this clientId to GA
https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference?hl=en#clientId

Analytics Tracking of a dynamic page

I am using Google analytics and wish to differentiate between two different cases on the home page, specifically depending on the user being logged in or logged out (similar to facebook).
It was suggested to use a different URL for each page, but I am loathe to do at it involves modifying the website structure and involves needless redirects and complexity.
Is there a way to modify the Google analytics to track this (eg so paths through the site are tracked differently for each case)? I have read about tracking events but I don't think this is correct.
Write out a <meta> tag on the server side if the user is logged in, then check in the js for it and change what is being tracked in the _trackPageview() call?
this may make it look like another webpage but I use this as an onclick event so I don't see why it could be used here some how;
pageTracker._trackPageview('/unauthorised-homepage-viewer');
alternatively use the _setCustomVar method
You could alternatively use custom variables. You'll find the documentation in the Google Analytics help site.
Edit: See especially example 2 under the "Example Use Cases":
For example, if your website offer
users the ability to login, you can
use a custom variable scoped to the
session level for user login status.
In that way, you can segment visits by
those from logged in members versus
anonymous visitors.
pageTracker._setCustomVar(
1, // This custom var is set to slot #1
"User Type", // The name of the custom varaible
"Member", // Sets the value of "User Type" to "Member" or
"Visitor" depending on status
2 // Sets the scope to session-level );
pageTracker._trackPageview();
Google analytics allows you to see content by url, the default in overview, or by title.
You can change dynamically the title of the page.

Categories