Skip to content

hasNested β€” GTM Variable Template for Object

VARIABLES β€Ί OBJECT
hasNested EXTENDED Object
Direct (.tpl) Apply (.tpl)

Checks if a nested property exists in an object using a path.



Nested property exists
INPUT
Object to Check: {a: {b: {c: 'value'}}}
Property Path: a.b.c
OUTPUT
true
Nested property missing
INPUT
Object to Check: {a: {b: {}}}
Property Path: a.b.c
OUTPUT
false

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

hasNested
Object to Check
πŸ’Ύ The object to check for the nested property.

Supported formats:
  βœ“ Object variable: {{myObject}}
  βœ“ Object literal
Property Path
πŸ’Ύ The path to the nested property. Can use dot notation or array format.

Supported formats:
  βœ“ String (dot notation): "user.profile.name"
  βœ“ Array: ["user", "profile", "name"]
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the object before checking (e.g., normalize object structure, parse JSON).
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the boolean result before returning it (e.g., val => !val to invert, val => val ? 'exists' : 'missing'). Useful for chaining transformations on the output.
Object to Check object
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Property Path array
hasNested()


πŸ“œ View Implementation Code
/**
* Checks if a nested property exists in an object using a path string or array.
*
* @param {Object} data.src - The object to check.
* @param {string|string[]} data.pth - The path to the property (e.g., "a.b.c" or ["a", "b", "c"]).
* @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 src before checking.
*
* @returns {boolean} Returns true if the nested property exists, false otherwise.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const hasNested = function(obj, path) {
if (getType(obj) !== 'object' || obj === null) {
return false;
}
const pathParts = getType(path) === 'array'
? path.map(function (part) { return part.toString(); })
: path.split('.');
let current = obj;
let index = 0;
const length = pathParts.length;
while (current != null && index < length) {
if (!current.hasOwnProperty(pathParts[index])) {
return false;
}
current = current[pathParts[index++]];
}
return index === length;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// hasNested - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(hasNested(value, data.pth));
// ===============================================================================
// hasNested(...) – Apply Mode
// ===============================================================================
/*
return function(obj, path) {
path = data.rp1 ? path : data.pth;
return out(hasNested(obj, path));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Nested property exists'
βœ… '[example] Nested property missing'
βœ… Test with array path format
βœ… Test property exists with undefined value
βœ… Test with non-object input returns false