Skip to content

has — GTM Variable Template for Object

VARIABLES › OBJECT
has EXTENDED Object

Checks if an object has a specific key.



Examples

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

GTM Configuration

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


Under the Hood

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