I would like to divide the traffic between registered and unregistered users with a custom variable. The Analytics documentation suggests to use a session-level custom variable for this purpose.
However, what is very common for our users is to log out when they are done using our service, because they are often working on shared computers. At log out, the session-level custom variable would be overwritten by 'unregistered', because this happens in the same session.
Is it somehow possible to end the Google Analytics session when a user logs out and start a new session?
With analytics.js
this is now possible to do.
Simply run ga('send', 'pageview', {'sessionControl': 'start'});
and it'll start a new session.
You can set _setSessionCookieTimeout and _setVisitorCookieTimeout to 0 to force a new session on browser close, but it won't work if visitor A logs out of your site but keeps the browser open and visitor B hops in the chair and logs in...
One thing you could do instead is to delete GA's cookies when a user logs out. This will immediately end the session and start a new session on next page load.
In case you aren't aware, there are some things to note about this stuff, since multiple people are using the same computer. Basically you can't rely on certain metrics like visitors and unique visitors.
Whether sessions timeout naturally, or whether they are forced from first option (the visitor cookie is preserved), metrics will show up as the same visitor having multiple visits.
If you go the 2nd route (deleting the cookies), it will count each new session as a new visitor, but you are going to destroy being able to see visitors coming in for multiple visits.
So no matter what you do, there's no easy way to track multiple users on the same computer and get reliable visit metrics.
Some things that you can do to help:
Designate a custom variable to be populated when a visitor is logged in to your site. Make it a unique value for each user. WARNING: read up on GA's privacy policy before deciding what value to use. For instance, you CANNOT use personally identifiable information, nor can you use any value that can directly be tied to personally identifiable data within your own site.
You can also grab GA's current visitor id cookie upon visitor logout and store it and then upon login, set the GA cookie to that values before outputting the GA code. It is basically the same principle as GA's cross-domain tracking solution except strangely, GA doesn't offer a baked in function to easily set their visitor ID (you can do it by passing it in a URL param but not with a function call - but you can set the cookie directly yourself).
Again with the warning: if you save GA's visitor id so that you can pop the cookie when they login again, make sure it is not directly tied to personally identifiable info (like in your database). Read their ToS and Privacy Policy, consult a lawyer, blahblah.
But anyways, if you do that, it will give you some measure of reliable data for individual visitors sharing the same computer. No way to account for before they actually login though, but still, better than nothing.
There are two ways in which you can do this appropriately.
- Page level : If you are keep the status of the user per request, it will be better to use this.
- Visitor level : If you want to set the variable when the user logs in only.
Clearing Cookies is not the right way to handle this problem.
Session variables are good for variables that need to be overridden at some point during the current session.
When selecting a scope, think of the features it provides:
- Session allows values to be overridden.
- Page level will be kept for next visit.
- Visitor will track page by page.