Google Tag Manager (GTM) Tag developers sometimes create a js dataLayer object snapshot using custom html tag to local storage to persist data across page loads. This is useful for tracking user journeys that span multiple pages, such as e-commerce checkout flows or multi-step form completions.
GTM templates can facilitate the process of creating such type of tags and provide more stabilty , consistency and transperency in futher managing and updating . Consider next use case:
// Enter your template code here.
const JSON = require('JSON');
const queryPermission = require('queryPermission');
const localStorage = require('localStorage');
const copyFromWindow = require('copyFromWindow');
const key = '6VBpU97si8asF3WA4Arxsm366VxynbCi';
let dataLayerCopy;
if(queryPermission('access_globals', 'read', 'dataLayer')) {
dataLayerCopy = copyFromWindow('dataLayer');
}
if (typeof dataLayerCopy == 'object' && queryPermission('access_local_storage', 'write', key)) {
localStorage.setItem(key,JSON.stringify(dataLayerCopy));
data.gtmOnSuccess();
}
When this tag is fired, it writes the stringified value of the window.dataLayer object, which contains current information about any tags that have been fired, to local storage