Skip to content

has β€” GTM Variable Template for Object

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

Checks if an object has a specific key.



Key exists returns true
INPUT
Object to Check: {name: "John", age: 30}
Property Key: name
OUTPUT
true
Key missing returns false
INPUT
Object to Check: {name: "John", age: 30}
Property Key: email
OUTPUT
false

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

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

Supported formats:
  βœ“ Object variable: {{myObject}}
  βœ“ Object literal
Property Key
πŸ’Ύ The name of the property to check for. Checks own properties only (not inherited from prototype).

Supported formats:
  βœ“ String: "name", "userId"
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 Key string
has()


πŸ“œ View Implementation Code
/**
* Checks if an object contains a top-level own property with the given key.
*
* @param {Object} data.src - The object to check.
* @param {string} data.key - The name of the property to check for.
* @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 object has the specified key (regardless of value).
*
* @framework ggLowCodeGTMKit
*/
const has = function(obj, key) {
if (obj == null || typeof obj !== 'object' || typeof key !== 'string') {
return false;
}
return obj.hasOwnProperty(key);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// has - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(has(value, data.key));
// ===============================================================================
// has(...) – Apply Mode
// ===============================================================================
/*
return function(obj, key) {
key = data.rp1 ? key : data.key;
return out(has(obj, key));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Key exists returns true'
βœ… '[example] Key missing returns false'
βœ… Null object returns false
βœ… Non-string key returns false
βœ… Object has key with undefined value