Skip to content

getKeys β€” GTM Variable Template for Object

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

Returns an array of an object's own keys.



Get object keys
INPUT
Object Input: {name: 'John', age: 30, city: 'Paris'}
OUTPUT
['name', 'age', 'city']
Empty object returns empty
INPUT
Object Input: {}
OUTPUT
[]

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

getKeys
Object Input
πŸ’Ύ The object whose keys are to be retrieved.

Supported formats:
  βœ“ Object
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the object before internal logic (e.g., filter properties, normalize structure). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., arr => arr.sort(), arr => arr.filter(key => key.length > 3) for filtering). Useful for chaining transformations on the output.
Object Input object
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
getKeys()


πŸ“œ View Implementation Code
/**
* Retrieves the keys of an object.
*
* @param {Object} data.obj - The object whose keys are to be retrieved.
* @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 extracting keys.
*
* @returns {Array} An array of the keys of the provided object.
*
* @framework ggLowCodeGTMKit
*/
const Object = require('Object');
const getKeys = function(objectInput) {
return Object.keys(objectInput);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getKeys - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const processedObject = applyCast(data.pre, data.obj);
return out(getKeys(processedObject));
// ===============================================================================
// getKeys() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(getKeys(value));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Get object keys'
βœ… Object with single key - should return array with one key
βœ… '[example] Empty object returns empty'
βœ… Object with numeric keys - should return array of string keys
βœ… Nested object - should return only top-level keys