Create and update a Google Analytics session timeout cookie – News Couple

Create and update a Google Analytics session timeout cookie

In Google Analytics, the concept of a Period It is the main collecting unit for all the data you work with. It’s fundamental to all the major metrics you use (conversion rate, bounce rate, session duration, landing page), yet there’s a basic complexity that I’m pretty sure many GA users don’t recognize. However, since this session idea is so central to GA (to the point that it’s overbearing), it’s annoying that Browser It is not specific to any of the session parameters that Google Analytics applies to results sent from the browser to its servers.

Well, to correct this, I wrote a very simple command customTask The solution, which basically simulates the session timeout (30 minutes by default) in a browser cookie. You can use this cookie for a number of things, such as preventing Event results from being sent if the session times out, or to convert these event results to Noninteractive He hits.

Naturally, I also updated my guide to customTask Plus my customTask Builder with this trick, so you can start using it with all the other cool stuff customTask There are tricks!


Simmer . Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava right in your inbox!

The customTask cipher

Here’s what’s file customTask The method looks like:

var _customTask = function() {
  // Update expiresMs to be the number of milliseconds when the cookie should expire.
  // Update domain to match the parent domain of your website.
  var updateSessionCookie = 
    expiresMs: 1000*60*30,
    domain: ''

  var globalSendHitTaskName   = '_ga_originalSendHitTask';

  return function(customTaskModel) {

    window[globalSendHitTaskName] = window[globalSendHitTaskName] || customTaskModel.get('sendHitTask');

    customTaskModel.set('sendHitTask', function(sendHitTaskModel) 

      var originalSendHitTaskModel = sendHitTaskModel,
          originalSendHitTask      = window[globalSendHitTaskName];

      var hitType, nonInteraction, d;


        // updateSessionCookie
        if (typeof updateSessionCookie === 'object' && updateSessionCookie.hasOwnProperty('expiresMs') && updateSessionCookie.hasOwnProperty('domain')) 
          hitType = sendHitTaskModel.get('hitType');
          nonInteraction = sendHitTaskModel.get('nonInteraction');
          if (nonInteraction !== true && (hitType === 'pageview' 
        // /updateSessionCookie


If you are using Google Tag Manager, copy and paste that into a custom JavaScript variable, then change the first line to function() { And remove the last character from the code block (the last semicolon). Then add it as a file The field to be set In the Google Analytics tag settings.

If you are using Google Analytics, you need to run JavaScript before the Google Analytics snippet, and then when you build the tracker, modify it to:

ga('create', 'UA-XXXXX-Y');
ga('set', 'customTask', _customTask); // <-- Add this
ga('send', 'pageview');

This is amazing customTask It should be added to all the tags and hits you want to have an effect on the session refresh. Basically, it should be activated with all pageviews and events, because these are the two that keep the session alive.

How it works

Which mark or stroke use this customTask Some additional code will now run when the result is submitted to Google Analytics. The code checks if the result is interactive (since non-interactive results don’t keep the session alive) and whether it is an event or pageview (since these are the two hits that keep the session alive).

Either way they are correct, a cookie named _session_UA-XXXXX-Y It is created (or updated if it already exists) with an expiration set for whatever you have configured as the value for expiresMs The property is at the beginning of the code block.

In other words, you will now have a cookie which, if any, means that there is most likely an active Google Analytics session. I say “probably”, because there are many ways in which sessions can be interrupted even before timeouts.

This cookie is a file reasonable abstraction From the session timeout chart in Google Analytics. It’s not perfect, but it does give you a stretch idea.

Things you can do with it

Well, the easiest thing is to use it to prevent events from being triggered if the session cookie is not present. why? Because it is exhausting to have sessions that only contain events. Although there’s really no problem with triggering events before Pageviews (even if all the documentation tries to tell you otherwise), there’s a risk that you’ll have sessions Just her events.

Those sessions will be spoiled by the ugly (not set) The landing page, because Google Analytics is tyrannical in sessions that always require a page view (I have no idea why).

So, in Google Tag Manager, you can do this by creating a file First Party Cookie A variable for the session cookie. Remember to change “UA-XXXXXX-Y” to any tracking identifier of the site you are monitoring session timeout for.

Session cookie

Next, create a new custom event trigger that checks if this cookie contains the value true:

Trigger to block the session if not live

Now, any tag you add this trigger as an exception will do Not Fire if the session times out.

This is pretty brutal – you don’t send event results to GA because you’re afraid they’ll create orphan sessions. It’s a valid fear to have with a GA session planner, but it may be overkill.

So another option is to convert all event results to Noninteractive Results, because these do not increase the numbers of sessions or dimensions in your data set, but the data is still collected.

To do this, you need a file custom javascript variable which looks like this:

  return UA-XXXXX-Y session !== 'true';

This returns JavaScript false If the session is active, and true If there is no active session. By adding this to lack of interaction In event tags, the result will be non-interactive if there is no active session.

You can even edit a file customTask to do this logic for you. within customTask way, right after that try {, you can do something like this (you will need a file First Party Cookie For this):

  if (UA-XXXXX-Y session !== 'true' && sendHitTaskModel.get('hitType') === 'event' && sendHitTaskModel.get('nonInteraction') !== true) 

This extension prevents the click from being fired (and from creating/updating the cookie) if the session is Not Alive and the blow is interactive event. For all other types of results, the result is sent and a cookie is created. This way you don’t need to mess with triggers – you can do it customTask Do all the work for you.


This is amazing customTask The trick can be used to gain insight into the browser whether a session is currently active in Google Analytics or not.

It’s not perfect, since the session depends on many things (many of which happen in the gut for GA processing servers), but it can be used indication Whether there is an active session or not.

This solution is also It is not necessary, especially if you are using BigQuery. The raw data you collect from the site is useful and valid even if there is no page view to start the session. So make sure you have a use case for this before you dive in.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button