Skip to content

⚡ OBJECT › Transformer (Advanced) Generator — GTM Variable Template for Object

VARIABLES › OBJECT
⚡ OBJECT › Transformer (Advanced) EXTENDED Object
Direct (.tpl)

Transforms object structure using key renaming, value transformation, and nesting rules.


Rename object keys
INPUT
Source Object: {firstName: 'John', lastName: 'Doe', age: 30}
Keep unmapped properties: true
Keep original keys when renaming: false
Key Mappings: [{key: 'firstName', tgt: 'first_name'}]
OUTPUT
{first_name: 'John', lastName: 'Doe', age: 30}
Flatten nested values
INPUT
Source Object: {user: {firstName: 'John', age: 30}, status: 'active'}
Keep unmapped properties: true
Keep original keys when renaming: false
Key Mappings: [{key: 'user.firstName', tgt: 'name'}]
OUTPUT
{name: 'John', user: {firstName: 'John', age: 30}, status: 'active'}

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

⚡ OBJECT › Transformer (Advanced)
OBJECT ⬇
Source Object
💾 The source object to transform.

Properties from this object will be read, optionally renamed, and/or transformed based on the mappings below.
✓ When enabled, properties NOT in the mapping table are preserved in the result.

When disabled, only mapped properties appear in the result.
✓ When enabled, the original key is kept alongside the renamed key (both appear in result).

When disabled, the original key is replaced by the new key.
Key Mappings ⬇
💾 Define how to transform object properties.

Source Key: Path to read from source object
  • Simple: "firstName"
  • Nested: "user.firstName"
  • Complex: "['user']['firstName']" (requires path parser)

Target Key: Path to write to result object
  • Leave empty to keep same key name
  • Simple: "first_name"
  • Nested: "profile.name" (auto-creates nested structure)

Transform Function: Optional function to modify the value
  • Leave empty to copy value as-is
  • Examples: val => val.toUpperCase(), parseInt, val => val.trim()
Source KeyTarget Key (optional)Transform Function (optional)
Path Processing
Path Parser (optional)
⚙️ Optional function to normalize complex path syntax before processing: path => normalizedPath

Use this to support custom path formats:
  • Bracket notation: "['user']['name']"["user", "name"]
  • Array notation: "items[0].name"["items", "0", "name"]

Example parser:
path => path.replace(/[['"]/g, '.').replace(/['"]]/g, '').split('.').filter(x => x)
Custom Getter (optional)
⚙️ Optional function to customize property retrieval: (obj, path) => value

Default behavior: Handles dot notation and array paths.

Use this for:
  • Case-insensitive lookups
  • Special property access logic
  • Custom path resolution
Custom Setter (optional)
⚙️ Optional function to customize property assignment: (obj, path, value) => obj

Default behavior: Handles dot notation, auto-creates nested objects.

Use this for:
  • Validation before setting
  • Custom nested structure creation
  • Special assignment logic
Result Handling
Output Function (optional)
⚙️ Optional function to transform the final object before returning it (e.g., obj => JSON.stringify(obj), obj => Object.freeze(obj)). Useful for chaining transformations on the output.
Source Object object
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
table
Source KeyTarget Key (optional)Transform Function (optional)
setPath()


📜 View Implementation Code
/**
* Transforms an object by renaming keys and/or applying functions to their values. Supports nested paths.
*
* @param {Object} data.src - Source object to transform.
* @param {boolean} data.kup - Whether to keep unmapped properties (properties not in mapping table).
* @param {boolean} data.kor - Whether to keep original keys when renaming.
* @param {Array<{key: string, tgt?: string, fnc?: Function}>} data.tkm - Key transformation mappings.
* @param {Function} [data.prs] - Optional path parser to normalize complex paths (e.g., ["JJ"]["kko"] → ["JJ", "kko"]).
* @param {Function} [data.get] - Optional custom getter (defaults to path-aware getter).
* @param {Function} [data.set] - Optional custom setter (defaults to path-aware setter).
* @param {Function|string} [data.out] - Optional output handler.
*
* @returns {Object} Transformed object with renamed keys and transformed values.
*
* @framework ggLowCodeGTMKit
*/
const getType = require('getType');
const transformerGenerator = function(src, kup, kor, tkm, prs, get, set) {
const sourceObj = getType(src) === 'object' && src || {};
const keepUnmapped = kup;
const keepOriginal = kor;
const keyMappings = tkm || [];
const parsePath = function(path) {
return getType(path) === 'array'
? path.map(part => part.toString())
: path.split('.');
};
const getPath = function(object, path) {
if (getType(object) !== 'object' || object === null) {
return undefined;
}
const pathParts = parsePath(path);
let current = object;
for (let i = 0; i < pathParts.length; i++) {
const key = pathParts[i];
if (current === null || current === undefined) {
return undefined;
}
current = current[key];
}
return current;
};
const pathExists = function(object, path) {
if (getType(object) !== 'object' || object === null) {
return false;
}
const pathParts = parsePath(path);
let current = object;
for (let i = 0; i < pathParts.length; i++) {
const key = pathParts[i];
if (current === null || current === undefined || !current.hasOwnProperty(key)) {
return false;
}
current = current[key];
}
return true;
};
const setPath = function(object, path, value) {
if (getType(object) !== 'object' || object === null) {
return object;
}
const pathParts = parsePath(path);
let current = object;
for (let i = 0; i < pathParts.length; i++) {
const key = pathParts[i];
if (i === pathParts.length - 1) {
current[key] = value;
} else {
if (getType(current[key]) !== 'object' || current[key] === null) {
current[key] = {}
🧪 View Test Scenarios (12 tests)
✅ '[example] Rename object keys'
✅ Key rename with transform - should rename and transform value
✅ '[example] Flatten nested values'
✅ Flat source to nested target - should create nested structure
✅ Don't keep unmapped properties - should only include mapped properties
✅ Keep original keys when renaming - should have both old and new keys
✅ Empty target key - should use source key as target
✅ Keep both flags enabled - should keep unmapped and original renamed keys
✅ Empty source object - should return empty object
✅ Nested to nested rename - should transform nested structure
✅ Multiple nested transformations - should handle complex mappings
✅ Transform without rename - should transform value but keep same key