Skip to content

getValue β€” GTM Variable Template for Object

VARIABLES β€Ί OBJECT
getValue CORE Object
Direct (.tpl) Apply (.tpl)

Returns the value of a property from an object by key.


Object Operations

Access, merge, pick, and transform object properties and structures.


Get existing key value
INPUT
Object: {name: 'John', age: 30, city: 'Paris'}
Key: name
OUTPUT
John
Missing key returns undefined
INPUT
Object: {name: 'John', age: 30}
Key: email
OUTPUT
undefined

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
Key
πŸ’Ύ The key of the value to retrieve.

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
getValue()


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