getValueByPath — GTM Variable Template for Object
getValueByPath CORE Object
Returns a value from a nested object using a dot-notation path.
When to Use This
Object Operations
Access, merge, pick, and transform object properties and structures.
URL Processing
Parse, build, decode, and manipulate URLs and query parameters.
Examples
Get nested value
INPUT
Source Object: {user: {profile: {name: 'John'}}}
Path: user.profile.name
Default Value: Unknown
Path: user.profile.name
Default Value: Unknown
OUTPUT
John
Missing path returns default
INPUT
Source Object: {user: {profile: {name: 'John'}}}
Path: user.settings.theme
Default Value: light
Path: user.settings.theme
Default Value: light
OUTPUT
light
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
getValueByPath
Source Object
💾 The object to retrieve the value from.
Supported formats:
✓ Object variable: {{myObject}}
✓ Object literal
Supported formats:
✓ Object variable: {{myObject}}
✓ Object literal
Path
💾 The path to the value using dot notation or array format.
Supported formats:
✓ String (dot notation): "user.profile.name"
✓ Array: ["user", "profile", "name"]
Supported formats:
✓ String (dot notation): "user.profile.name"
✓ Array: ["user", "profile", "name"]
Default Value
💾 The default value to return if the path doesn't exist in the object. If the path exists but the value is undefined, undefined will be returned instead of this default.
Supported formats:
✓ Any type: string, number, boolean, object, array
Supported formats:
✓ Any type: string, number, boolean, object, array
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the source object before retrieving the value (e.g., normalize object structure, add missing properties).
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the retrieved value before returning it (e.g.,
val => val.toString(), val => val || 'empty'). Useful for chaining transformations on the output.Source Object object
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Path array
Default Value string
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
getValueByPath()
Related Variables
Same category: Object
Under the Hood
📜 View Implementation Code
/**
* Safely retrieves a nested value from an object using a path string or array.
*
* @param {Object} data.obj - The object to retrieve the value from.
* @param {string|string[]} data.path - The path to the value (e.g., "a.b.c" or ["a", "b", "c"]).
* @param {any} data.def - The default value to return if the path doesn't exist.
* @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 nested path, or the default value if the path doesn't exist.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const getValueByPath = function(obj, path, defaultValue) {
if (getType(obj) !== 'object' || obj === null) {
return defaultValue;
}
const pathParts = getType(path) === 'array' ? path.map(function (part) { return part.toString(); }) : path.split('.');
// Handle empty path - return the object itself
if (pathParts.length === 0 || (pathParts.length === 1 && pathParts[0] === '')) {
return obj;
}
let current = obj;
let index = 0;
const length = pathParts.length;
while (current != null && index < length) {
current = current[pathParts[index++]];
}
return index === length ? current : defaultValue;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getValueByPath - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedObj = applyCast(data.pre, data.obj);
return out(getValueByPath(processedObj, data.path, data.def));
// ===============================================================================
// getValueByPath(...) – Apply Mode
// ===============================================================================
/*
return function(value, path, defaultValue) {
path = data.rp1 ? data.path : path;
defaultValue = data.rp2 ? data.def : defaultValue;
return out(getValueByPath(value, path, defaultValue));
};
*/🧪 View Test Scenarios (6 tests)
✅ '[example] Get nested value'
✅ '[example] Missing path returns default'
✅ Test with array path notation
✅ Test path exists but value is undefined - should return undefined not default
✅ Test with null object returns default value
✅ Test with empty path returns the object itself