Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to manually end a Google Analytics Session?

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?

like image 264
user1551278 Avatar asked Jul 25 '12 10:07

user1551278


People also ask

How does a session end in Google Analytics?

If 30 minutes pass without any kind of interaction from Bob, the session ends. However, every time Bob interacts with an element (like an event, social interaction, or a new page), Analytics resets the expiration time by adding on an additional 30 minutes from the time of that interaction.

What conditions cause a session visit to end in Google Analytics?

1. After 30 minutes of inactivity. By default, a session ends when a user did nothing on your site for 30 minutes straight. For example, a user looks at a page on your site, then reads a blog post and leaves that open without doing anything on that page for 30 minutes, the session ends.

How can a session in Google Analytics change?

By default, a GA session expires after 30 minutes of user inactivity. You can change this setting by changing the session timeout setting in your Google Analytics view. You can make a session expire after three minutes of user inactivity or after three hours of user inactivity.

What is the default timeout for a session in Google Analytics?

By default, a session ends (times out) after 30 minutes of user inactivity.


2 Answers

With analytics.js this is now possible to do.

Simply run ga('send', 'pageview', {'sessionControl': 'start'}); and it'll start a new session.

like image 89
hadees Avatar answered Oct 16 '22 23:10

hadees


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.

like image 27
Crayon Violent Avatar answered Oct 16 '22 23:10

Crayon Violent