Skip to content

getDataLayerCurrent β€” GTM Variable Template for GTM

VARIABLES β€Ί GTM
getDataLayerCurrent CORE GTM
Direct (.tpl) Apply (.tpl)

Gets the current dataLayer object for a specific event or a property value from it.


GTM Utilities

Access GTM-specific APIs: dataLayer, debug mode, container settings.


Full object by event
INPUT
templateStorage: {
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
OUTPUT
en

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" )
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


πŸ“œ 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