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
GTM Utilities
Access GTM-specific APIs: dataLayer, debug mode, container settings.
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
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
_____________
✏️ Examples
{{toCamelCase()}} - convert string to camelCase
{{undefinedTo("x")}} - convert undefined value to "x"
{{filter(GreaterThan(10))}} - keep values greater than 10
Supported formats:
✓ Function
_____________
✏️ Examples
{{toCamelCase()}} - convert string to camelCase
{{undefinedTo("x")}} - convert undefined value to "x"
{{filter(GreaterThan(10))}} - keep values greater than 10
Related Variables
Same category: GTM
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