has β GTM Variable Template for Object
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβKey exists returns true
INPUT
Object to Check: {name: "John", age: 30}
Property Key: name
Property Key: name
OUTPUT
true
Key missing returns false
INPUT
Object to Check: {name: "John", age: 30}
Property Key: email
Property Key: email
OUTPUT
false
Live Sandbox
Section titled βLive Sandboxβ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
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"
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
has()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled β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 ? 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