getDataLayerValue — GTM Variable Template for GTM
getDataLayerValue CORE GTM
Retrieves a value from the GTM dataLayer by variable name.
When to Use This
GTM Utilities
Access GTM-specific APIs: dataLayer, debug mode, container settings.
Examples
Top-level variable
INPUT
DataLayer Variable Name: userId
OUTPUT
12345
Nested variable
INPUT
DataLayer Variable Name: ecommerce.purchase.value
OUTPUT
99.99
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
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"
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
___________
✏️ Examples
{{toNumber()}} - convert input to number
{{toLowerCase()}} - convert input to lowercase
{{trim()}} - remove whitespace from input
Supported formats:
✓ Function
___________
✏️ Examples
{{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
___________
✏️ 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 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