getDataLayerValue β GTM Variable Template for GTM
getDataLayerValue CORE GTM
Retrieves a value from the GTM dataLayer by variable name.
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βTop-level variable
INPUT
DataLayer Variable Name: userId
OUTPUT
12345
Nested variable
INPUT
DataLayer Variable Name: ecommerce.purchase.value
OUTPUT
99.99
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
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
{{toNumber()}} - convert input to number
{{toLowerCase()}} - convert input to lowercase
{{trim()}} - remove whitespace from input
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
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 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