Send Google Analytics payload length as a Custom Dimension

The maximum length of a Google Analytics payload is 8192 bytes. It is useful to check if you are approaching this value with some of your hits because if the payload length exceeds this, the hit is never sent to GA.

How can we know the payload size with each hit?

Today i will show you how to send the payload size as a custom dimension to GA with each hit. The tool is Google Tag Manager.

Before starting, creating a new hit-scoped custom dimension in GA is essential, named ‘Hit Payload Length’ and check its index, which will be used in the next step.

Then, create a custom task in GTM, the custom task is to let users modify the request sent to GA before it is sent. We can take Client ID as an example.

Custom task will work with a custom javascript variable. Here is the javacript code which we create in GTM.

function () {
// clientIdIndex: The Custom Dimension index to where you want to send the visitor's Client ID, my example is 7
var clientIdIndex = 7;

// payloadLengthIndex: The Custom Dimension index to where you want to send the length of the payload of the request, my example is 18

var payloadLengthIndex = 18;

// DO NOT EDIT ANYTHING BELOW THIS LINE
var readFromStorage = function (key) {
if (!window.Storage) {
// From: https://stackoverflow.com/a/15724300/2367037
var value = '; ' + document.cookie;
var parts = value.split('; ' + key + '=');
if (parts.length === 2) {
return parts.pop().split(';').shift();
}
} else {
return window.localStorage.getItem(key);
}
};

var writeToStorage = function (key, value, expireDays) {
if (!window.Storage) {
var expiresDate = new Date();
expiresDate.setDate(expiresDate.getDate() + expireDays);
document.cookie = key + '=' + value + ';expires=' + expiresDate.toUTCString();
} else {
window.localStorage.setItem(key, value);
}
};

var globalSendHitTaskName = '_ga_originalSendHitTask';

return function (customTaskModel) {

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

// clientIdIndex
if (typeof clientIdIndex === 'number') {
customTaskModel.set('dimension' + clientIdIndex, customTaskModel.get('clientId'));
}
// /clientIdIndex

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

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

try {

// payloadLengthIndex
if (typeof payloadLengthIndex === 'number') {
var _pl_hitPayload = sendHitTaskModel.get('hitPayload');
_pl_hitPayload += '&cd' + payloadLengthIndex + '=';
_pl_hitPayload += (_pl_hitPayload.length + _pl_hitPayload.length.toString().length);
sendHitTaskModel.set('hitPayload', _pl_hitPayload, true);
}
// /payloadLengthIndex

if (canSendHit) {
originalSendHitTask(sendHitTaskModel);
}

} catch(err) {
originalSendHitTask(originalSendHitTaskModel);
}

});

};
}

The last step is the add the custom task into your tags, we can scroll down to a tag and add a new field.

After this step, any tags which has this custom task will add the hit payload length as a custom dimension, whose value will be the custom task we just created.

How to debug your work?

Verify it through the developer tools on your browser.

  • Open the Network on the developer tool on your browser
  • Click the request to collect
  • Check if there is a payload parameter

With this custom dimension, we can monitor if the payload maximum size will be hit.

If you are interested in or have any problems with GTM, feel free to contact me.

Or you can connect with me through my LinkedIn.

Image for post
Image for post

Originally published at http://jacquiwucom.wordpress.com on October 8, 2020.

Written by

A current BI Analyst in a subsidiary under Webjet, with experience in applying data science techniques to business.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store