๐๐๐๐๐๐ Transformer Generator (Advanced) - Object โ GTM Variable Template for GTM
๐๐๐๐๐๐ Transformer Generator (Advanced) - Object CORE GTM
Transforms object structure using key renaming, value transformation, and nesting rules.
Examples
Rename object keys
INPUT
Keep unmapped properties: true
Keep original keys when renaming: false
tkm: [
Keep original keys when renaming: false
tkm: [
OUTPUT
{first_name: 'John', lastName: 'Doe', age: 30}
Flatten nested values
INPUT
Keep unmapped properties: true
Keep original keys when renaming: false
tkm: [
Keep original keys when renaming: false
tkm: [
OUTPUT
{name: 'John', user: {firstName: 'John', age: 30}, status: 'active'}
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
๐๐๐๐๐๐ Transformer Generator (Advanced) - Object
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.
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 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.
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:
โข Nested:
โข Complex:
Target Key: Path to write to result object
โข Leave empty to keep same key name
โข Simple:
โข Nested:
Transform Function: Optional function to modify the value
โข Leave empty to copy value as-is
โข Examples:
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:
Use this to support custom path formats:
โข Bracket notation:
โข Array notation:
Example parser:
path => normalizedPathUse 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:
Default behavior: Handles dot notation and array paths.
Use this for:
โข Case-insensitive lookups
โข Special property access logic
โข Custom path resolution
(obj, path) => valueDefault 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:
Default behavior: Handles dot notation, auto-creates nested objects.
Use this for:
โข Validation before setting
โข Custom nested structure creation
โข Special assignment logic
(obj, path, value) => objDefault 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 string
๐ก 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)
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
setPath()
Related Variables
Same category: GTM
Under the Hood
๐ 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 sourceObj = getType(data.src) === 'object' && data.src || {};
const keepUnmapped = data.kup;
const keepOriginal = data.kor;
const keyMappings = data.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] = {};
}
current = current[key];
}
}
return object;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const pathParser = safeFunction(data.prs);
const getter = typeof data.get === 'function' ? data.get : getPath;
const s๐งช 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