Skip to content

hasKeyEqualTo β€” GTM Variable Template for Object

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

Checks if an object has a key with a specific value (strict equality).



Key value matches
INPUT
Object To Check: {name: "John", age: 30, city: "Paris"}
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
OUTPUT
false

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
Property Key
πŸ’Ύ The property name to verify.

Supported formats:
  βœ“ String
Expected Value
πŸ’Ύ The value to compare against.

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


πŸ“œ 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