Skip to content

๐Ž๐๐‰๐„๐‚๐“ Transformer Generator (Advanced) - Object โ€” GTM Variable Template for GTM

VARIABLES โ€บ 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: [
OUTPUT
{first_name: 'John', lastName: 'Doe', age: 30}
Flatten nested values
INPUT
Keep unmapped properties: true
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.
โœ“ 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 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)
setPath()


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