hasKeyEqualTo — GTM Variable Template for Object
hasKeyEqualTo EXTENDED Object
Checks if an object has a key with a specific value (strict equality).
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
GTM Configuration
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
Same category: Object
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