Skip to content

getDataLayerValue β€” GTM Variable Template for GTM

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

Retrieves a value from the GTM dataLayer by variable name.


GTM Utilities

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


Top-level variable
INPUT
DataLayer Variable Name: userId
OUTPUT
12345
Nested variable
INPUT
DataLayer Variable Name: ecommerce.purchase.value
OUTPUT
99.99

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

Read-only Preview
getDataLayerValue
DataLayer Variable Name
πŸ’Ύ The name of the dataLayer variable to retrieve.

Supports dot notation for nested properties:
  βœ“ Top-level variable: "userId", "eventName"
  βœ“ Nested property: "user.email", "ecommerce.purchase.value"
  βœ“ Array index: "products.0.name"

Supported formats:
  βœ“ "String"
Input Setup
Input Function (optional)
βš™οΈ Optional function to apply to the input before processing.

Supported formats:
   βœ“ Function

{{toNumber()}} - convert input to number
{{toLowerCase()}} - convert input to lowercase
{{trim()}} - remove whitespace from input
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 a value from the dataLayer
*
* @param {string} data.dlVariableName - The dataLayer variable name to retrieve (supports dot notation)
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform variable name before retrieval.
*
* @returns {*} The value from the dataLayer, or undefined if not found
*
* @framework ggLowCodeGTMKit
*/
const copyFromDataLayer = require('copyFromDataLayer');
const getDataLayerValue = function(variableName) {
if (!variableName) {
return undefined;
}
return copyFromDataLayer(variableName);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getDataLayerValue - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const variableName = applyCast(data.pre, data.dlVariableName);
return out(getDataLayerValue(variableName));
// ===============================================================================
// getDataLayerValue() – Apply Mode
// ===============================================================================
/*
return function(variableName) {
return out(getDataLayerValue(variableName));
};
*/
___WEB_PERMISSIONS___
[
{
"instance": {
"key": {
"publicId": "read_data_layer",
"versionId": "1"
},
"param": [
{
"key": "allowedKeys",
"value": {
"type": 1,
"string": "any"
}
}
]
},
"clientAnnotations": {
"isEditedByUser": true
},
"isRequired": true
}
]
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Top-level variable'
βœ… '[example] Nested variable'
βœ… Test undefined variable returns undefined
βœ… Test empty variable name returns undefined
βœ… Test with output function transformation