hasKeyEqualTo β GTM Variable Template for Object
hasKeyEqualTo EXTENDED Object
Checks if an object has a key with a specific value (strict equality).
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβKey value matches
INPUT
Object To Check: {name: "John", age: 30, city: "Paris"}
Property Key: name
Expected Value: John
Property Key: name
Expected Value: John
OUTPUT
true
Key value mismatch
INPUT
Object To Check: {name: "John", age: 30}
Property Key: age
Expected Value: 25
Property Key: age
Expected Value: 25
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.
hasKeyEqualTo
Object To Check
πΎ The object to check for the property.
Supported formats:
β Object
Supported formats:
β Object
Property Key
πΎ The property name to verify.
Supported formats:
β String
Supported formats:
β String
Expected Value
πΎ The value to compare against.
Supported formats:
β Any
Supported formats:
β Any
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the object before internal logic (e.g., parse JSON string, normalize object). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., val => val ? 'Found' : 'Not found', val => !val for negation). 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
Expected Value any
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
hasKeyEqualTo()
Related Variables
Section titled βRelated VariablesβSame category: Object
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Checks if an object has a top-level own property with the given key and that its value strictly equals the expected value. * * @param {Object} data.obj - The object to check. * @param {string} data.key - The property name to verify. * @param {*} data.val - The value to compare against. * @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 checking. * * @returns {boolean} True if the key exists and its value matches the expected value, false otherwise. * * @framework ggLowCodeGTMKit */const hasKeyEqualTo = function(obj, key, expectedValue) { return obj != null && typeof obj === 'object' && typeof key === 'string' && obj.hasOwnProperty(key) && obj[key] === expectedValue;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// hasKeyEqualTo - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const processedObj = applyCast(data.pre, data.obj);return out(hasKeyEqualTo(processedObj, data.key, data.val));// ===============================================================================// hasKeyEqualTo(...) β Apply Mode// ===============================================================================/*return function(value, key, expectedValue) { return out(hasKeyEqualTo(value, data.key, data.val));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Key value matches'β
'[example] Key value mismatch'β
Object doesn't have the key - returns falseβ
Strict equality check - number vs string returns falseβ
Non-object input - returns false