getValue β GTM Variable Template for Object
getValue CORE Object
Returns the value of a property from an object by key.
When to Use This
Section titled βWhen to Use ThisβObject Operations
Access, merge, pick, and transform object properties and structures.
Examples
Section titled βExamplesβGet existing key value
INPUT
Object: {name: 'John', age: 30, city: 'Paris'}
Key: name
Key: name
OUTPUT
John
Missing key returns undefined
INPUT
Object: {name: 'John', age: 30}
Key: email
Key: email
OUTPUT
undefined
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
getValue
Object
πΎ The object to retrieve the value from.
Supported formats:
β Object
Supported formats:
β Object
Key
πΎ The key of the value to retrieve.
Supported formats:
β String
Supported formats:
β String
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the object before internal logic (e.g., parse JSON string to object, normalize structure). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., val => val || 'default', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Object object
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Key string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
getValue()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Retrieves a value from an object using a key. * * @param {Object} data.obj - The object to retrieve the value from. * @param {string} data.key - The key of the value to retrieve. * @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 obj before processing. * * @returns {*} The value at the specified key, or undefined if not found. * * @framework ggLowCodeGTMKit */const getType = require('getType');
const getValue = function(obj, key) { if (getType(obj) !== 'object' || obj === null) { return undefined; } return obj[key];};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// getValue - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedObj = applyCast(data.pre, data.obj);return out(getValue(processedObj, data.key));// ===============================================================================// getValue(...) β Apply Mode// ===============================================================================/*return function(value, key) { key = data.rp1 ? key : data.key; return out(getValue(value, key));};*/π§ͺ View Test Scenarios (10 tests)
β
'[example] Get existing key value'β
'[example] Missing key returns undefined'β
Object with numeric value - should return numberβ
Object with boolean value - should return booleanβ
Object with array value - should return arrayβ
Object with nested object value - should return nested objectβ
Empty object - should return undefinedβ
Non-object input (string) - should return undefinedβ
Object with undefined value explicitly set - should return undefinedβ
Object with null value - should return null