getDataLayerCurrent β GTM Variable Template for GTM
getDataLayerCurrent CORE GTM
Gets the current dataLayer object for a specific event or a property value from it.
When to Use This
Section titled βWhen to Use ThisβGTM Utilities
Access GTM-specific APIs: dataLayer, debug mode, container settings.
Examples
Section titled βExamplesβFull object by event
INPUT
templateStorage: {
getItem: function(key) { return storage[key]; },
setItem: function(key, value) { storage[key] = value; }
}
Return Type: fob
getItem: function(key) { return storage[key]; },
setItem: function(key, value) { storage[key] = value; }
}
Return Type: fob
OUTPUT
page_view
Property value by event
INPUT
templateStorage: {
getItem: function(key) { return storage[key]; },
setItem: function(key, value) { storage[key] = value; }
}
Return Type: dlv', key: 'page
getItem: function(key) { return storage[key]; },
setItem: function(key, value) { storage[key] = value; }
}
Return Type: dlv', key: 'page
OUTPUT
en
GTM Configuration
Section titled βGTM ConfigurationβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
Read-only Preview
getDataLayerCurrent
Return Type
Property Key
πΎ Property to extract.
Supported formats:
β String (e.g. "page", "page_category, "ecommerce" )
Supported formats:
β String (e.g. "page", "page_category, "ecommerce" )
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it.
Supported formats:
β Function
{{toCamelCase()}} - convert string to camelCase
{{undefinedTo("x")}} - convert undefined value to "x"
{{filter(GreaterThan(10))}} - keep values greater than 10
Supported formats:
β Function
{{toCamelCase()}} - convert string to camelCase
{{undefinedTo("x")}} - convert undefined value to "x"
{{filter(GreaterThan(10))}} - keep values greater than 10
Related Variables
Section titled βRelated VariablesβSame category: GTM
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Retrieves the current dataLayer event object or a specific property by gtm.uniqueEventId. * Searches up to 2 levels deep to handle wrapped structures. * * @param {string} [data.add] - Mode: "fob" for full object, "dlv" for dataLayer variable. * @param {string} [data.key] - Property name to extract (when add="dlv"). * @param {Function|string} [data.out] - Optional output handler. * * @returns {Object|*} Full object (or {}) when fob, property value (or undefined) when dlv. * * @framework ggLowCodeGTMKit */const copyFromDataLayer = require('copyFromDataLayer');const copyFromWindow = require('copyFromWindow');const templateStorage = require('templateStorage');
const CACHE_KEY = 'dlEvt';const EVENT_ID_KEY = 'gtm.uniqueEventId';const gtmId = copyFromDataLayer(EVENT_ID_KEY);
const getEventObject = function() { if (!gtmId) return undefined;
const cache = templateStorage.getItem(CACHE_KEY); if (cache && cache.id === gtmId) { return cache.obj; }
const dataLayer = copyFromWindow('dataLayer'); if (!dataLayer) return undefined;
const cacheAndReturn = function(obj) { templateStorage.setItem(CACHE_KEY, { id: gtmId, obj: obj }); return obj; };
const findEventObject = function(item) { if (!item || typeof item !== 'object') return undefined;
if (item[EVENT_ID_KEY] === gtmId) { return item; }
for (let key in item) { if (!item.hasOwnProperty(key)) continue; const nested = item[key]; if (nested && typeof nested === 'object' && nested[EVENT_ID_KEY] === gtmId) { return nested; } }
return undefined; };
for (let i = dataLayer.length - 1; i >= 0; i--) { const item = dataLayer[i]; if (!item) continue;
const found = findEventObject(item); if (found) { return cacheAndReturn(found); }
const itemId = item[EVENT_ID_KEY]; if (itemId && itemId > gtmId) continue; if (itemId && itemId < gtmId) break; }
return undefined;};
const eventObject = getEventObject();const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// getDataLayerCurrent - Direct mode// ===============================================================================if (data.add === 'dlv') { return out(eventObject ? eventObject[data.key] : undefined);} else { return out(eventObject || {});}// ===============================================================================// getDataLayerCurrent() β Apply Mode// ===============================================================================/*if (data.add === 'dlv') { return function(key) { return out(eventObject ? eventObject[key] : undefined);π§ͺ View Test Scenarios (5 tests)
β
'[example] Full object by event'β
Test Full Object mode with event not found returns empty objectβ
'[example] Property value by event'β
Test DataLayer Variable mode with event not found returns undefinedβ
Test DataLayer Variable mode with key not found returns undefined