Automatically reduce Google Analytics payload length – News Couple

Automatically reduce Google Analytics payload length

Here we are, reunited with customTask. This time, we will put this wonderful mechanism to work for a very good cause. One of the lesser known “features” of Google Analytics is that when the payload size (the request body that is actually sent to Google Analytics with each request) exceeds a certain limit, namely 8192 bytes, the result is aborted Without warning. This can come as a surprise, because there is no indication anywhere in Google Analytics that you are losing results because of this.

The solution? cute little customTask The script, which iteratively removes parts of the payload until the length is slightly less than the cap.

The idea is that you will be able to define a list of payload parameters to be removed one by one from the payload until the size is safely below the max of 8192. The first things that will go are some default dimensions that nobody really uses, like ‘enable java’ and ‘enable coding’ the document”. Then, you can list all the custom dimensions you want to get rid of, in order of priority.

Finally, you will be able to list the Enhanced Ecommerce impression parameters that you want to remove from each impression in the payload. The text will stop once the length is less than 8192, so you will likely end up with some impressions with more detail than others.

Why stop at impressions? Because, from experience, this is where most of the problems lie. Customizing the script shouldn’t be too difficult to also include promotions and even ‘normal’ Ecommerce Enhanced Product Objects.

Once you implement this in your optimized ecommerce tags, you should never see the following error in the console (if the Google Analytics Debugger is activated, or if you have Universal Analytics running in debug mode):

Load length warning

I recommend doing this customTask Along with my solution to send the payload length as a custom dimension. This way you will be able to detect if you are getting close to your maximum payload length with how often you hit, and you can then proceed to implement a payload length limiter as necessary.

For an alternative solution, take a look at this excellent article by the awesome Dan Wilkerson from the awesome Bounteous Blog.


Simmer . Newsletter

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

The customTask Worker

to construct customTask, you need to create a file custom javascript variable. call it something like customTask – reduce payload length.

Noticeable! If you already have a file customTask implemented in your own optimized ecommerce brands, you will need to combine the below code with customTask The script you already have. For tips on how to do this, check out this guide.

Add the following code to the file customTask variable body:

function() {
  return function(customModel) {
    // Add any other default parameters you want to strip from the payload
    // into this array in order of priority. 
    // For more details, visit:
    var defaultParams = ['&je', '&de', '&sd', '&vp', '&sr'];
    // List the (regular) Custom Dimensions you want to strip from the
    // payload in order of priority. Leave the array empty if you don't
    // want to remove Custom Dimensions. 
    // For more details, visit:
    var customDims = ['&cd198', '&cd199', '&cd200'];

    // List the impression object parameters you want to strip from the
    // payload in order of priority. Only list the field specifier, so
    // instead of '&il1pi2va', write 'va', and instead of '&il1pi4cd3', write
    // 'cd3'. For more details, visit:
    var impressions = ['va', 'br', 'ca', 'ps', 'nm'];
    // Don't touch the code below.
    var maxLength = 8192,
        globalSendTaskName = '_' + customModel.get('trackingId') + '_sendHitTask',
        originalSendHitTask = window[globalSendTaskName] = window[globalSendTaskName] || customModel.get('sendHitTask');
    customModel.set('sendHitTask', function(sendModel) 
      var ga = window[window['GoogleAnalyticsObject']];
      // Only log if in analytics debug mode.
      var log = function(msg) 
        if ('dump' in ga) 
          window.console.log.apply(window.console, [msg]);
      var hitPayload = sendModel.get('hitPayload');
      var removeKeys = function(hitPayload, keys) 
        var key, regex;
        while(hitPayload.length >= maxLength && keys.length) 
          key = keys[0];
          log('--> Removing ' + key);
          regex = new RegExp(key + '=[^&]+', 'gi');
          hitPayload = hitPayload.replace(regex, '');
          log('--> New length: ' + hitPayload.length);
        return hitPayload;
      var removeImpressions = function(hitPayload, keys) 
        var key, regex, oldKey;
        while(hitPayload.length >= maxLength && keys.length) 
          if (key !== keys[0]) 
            key = keys[0];
            log('--> Removing &ilNpiN' + key + ' from impression objects');
          regex = new RegExp('&ild+pid+' + key + '=[^&]+', 'i');
          if (!regex.test(hitPayload)) 
          hitPayload = hitPayload.replace(regex, '');
        log('--> New length: ' + hitPayload.length);
        return hitPayload;

      // If over payload length, remove default parameters.
      if (hitPayload.length >= maxLength) 
        log('Payload too long (' + hitPayload.length + '), removing default keys...');
        hitPayload = removeKeys(hitPayload, defaultParams);
      // If over payload length, remove custom dimensions.
      if (hitPayload.length >= maxLength) 
        log('Payload still too long (' + hitPayload.length + '), removing Custom Dimensions...');
        hitPayload = removeKeys(hitPayload, customDims);
      // If over payload length, clean up impression objects.
      if (hitPayload.length >= maxLength) 
        log('Payload still too long (' + hitPayload.length + '), cleaning up Impressions...');
        hitPayload = removeImpressions(hitPayload, impressions);
      // Send the modified payload.
      sendModel.set('hitPayload', hitPayload, true);

Make sure to edit a file defaultParamsAnd customDims, And impressions Arrays to include the keys you want to clear from a file dataLayer. Note that you will need to list the keys using their Measurement Protocol parameter names instead of their own analytics.js Field names. so instead of 'encoding'For example, you need to use '&de'.

Keys you added to a file defaultParams group is:

a key Noun Describe
&je enable java Whether or not the browser has Java enabled.
&de Document encoding What is the character encoding for the current page (eg UTF-8).
&sd Screen colors Screen color depth (eg 24-bits).
&vp Viewport size Viewable area (in pixels) of the browser or device.
&sr screen resolution Screen resolution (in pixels) of the browser or device.

Of course, you may want to keep some of these, for example &vp or &sr. Feel free to edit a file defaultParams Set as you see fit.

When adding items to a file impressions The matrix, the syntax is a little more complicated. Should Just Add the actual parameter selector, which is the key that comes next &ilNpiN. So if the parameter name of the product appearance variable is &il2pi1vaYou will just write 'va' in the matrix. Or, if you want to remove the product scope custom component from index 3 of the appear objects, instead of writing '&il1pi1cd3', you can write 'cd3 in the matrix.

Note that defaultParams And customDims You need to write the full parameter name With leading &.

Add the variable to your tag

This variable must be added to the tag that sends the optimized ecommerce payload with impression objects to Google Analytics. To add it to the tag, edit the Google Analytics settings variable in the impression tag, or add the field directly to the tag.

go to the More settings -> Fields to be set, and add the following field:

field name: customTask
Values: customTask – reduce payload length

Such that:

Fields to be set

And that’s it in preparation! Now, if your payload is more than 8192, the script will first chop the parameters specified in a file defaultParams matrix array. Then the dimensions will devour in customDims matrix array. Finally, if the payload is still too long, it will start cleaning up the impressions objects by removing the fields listed in impressions One by one, impression by impression.

How to correct

If you’ve looked at the code, and I’m sure you did, you might have noticed how I use a custom named method log() To send some debug data to the console. The thing is, I don’t want to pollute the JavaScript console with debug messages when Google Analytics is too Not In debug mode, so I do a simple check to see if the debugging version of analytics.js loader:

if ('dump' in window['ga']) 

Basically, if the global ga The object has the method dump, I assume the user is using the debug version of analytics.js.

note! If someone has come up with a more efficient way to check the debug version of analytics.js, please let me know! Thank you.

So, if you want to see debug messages, you can set the library to debug mode by browsing to More settings -> advanced configuration -> Use the patch version in the Google Analytics tag or the Google Analytics settings variable, and set it to true. Or you can use the Google Analytics Debugger browser extension.

Once the debug library is loaded, you can see debug messages like these if The payload exceeded the maximum length of 8192:

Correction messages

In these debug messages, you can see how the script goes through your payload, stripping parameters as necessary.


I hope analytics.js A more elegant way to handle very large loads. Just dropping them is a bit overkill, in my opinion. js, for example, can collect a custom result for GA when the maximum payload size is breached, and this may turn out to be a warning or notification in the Google Analytics UI. This way you will at least have proof that something is wrong.

So customTask The solution presented here, as is usual for scripts I write, is a bandage for something that we hope will be treated locally by the library itself in the future.

Of course, the “best” way to avoid very large payloads is to only send relevant data to Google Analytics. In the case of impressions, for example, it doesn’t really make sense to send every impression on the page in one fell swoop. Alternatively, you may only want to submit impressions that the user has already obtained look. Fortunately, I may have a guide coming up that will show you how to keep track of impressions that have only been viewed.

Let me know in the comments if you found this helpful or if you have suggestions for improving the method!

Source link

Related Articles

Leave a Reply

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

Back to top button