Skip to content

getValue — GTM Variable Template for Object

VARIABLES › OBJECT
getValue CORE Object

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


When to Use This

Object Operations

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


Examples

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

GTM Configuration

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


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 ? data.key : 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