Skip to content

๐Ž๐๐‰๐„๐‚๐“ Smart Generator - Object โ€” GTM Variable Template for GTM

VARIABLES โ€บ GTM
๐Ž๐๐‰๐„๐‚๐“ Smart Generator - Object CORE GTM

Generates an object with fixed and conditional properties based on conditions.


When to Use This

GTM Utilities

Access GTM-specific APIs: dataLayer, debug mode, container settings.

Type Conversion

Safely convert between data types โ€” strings, numbers, booleans, arrays, objects.


Examples

Overwrite fixed properties
INPUT
Source Object: {name: 'John', age: 30}
Source Properties Handling: false
Add fixed property (or from variable): true
ffp: [
Add conditional property (or from variable): false
Add Rule-Based Properties: false
OUTPUT
{name: 'John', age: 25, type: 'user'}
Conditional properties added
INPUT
Source Object: {name: 'Product'}
Source Properties Handling: false
Add fixed property (or from variable): false
Add conditional property (or from variable): true
con: true
fcp: [
Add Rule-Based Properties: false
OUTPUT
{name: 'Product', category: 'checkout', tracked: true}

GTM Configuration

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

๐Ž๐๐‰๐„๐‚๐“ Smart Generator - Object
OBJECT โฌ‡
Source Object
๐Ÿ’พ The source object to start from.

Use the checkboxes below to add additional properties.
Source Properties Handling
๐Ÿ”ง Choose whether to keep properties from the source object.

โ€ข Keep: Start with source object, add/overwrite with new properties
โ€ข Remove: Start with empty object, only include new properties added below
โœ“ Include properties that are always added to the object, regardless of conditions.
โœ“ Include properties that are only added when a global condition is true.
โœ“ Include properties where each property has its own individual condition.
newProp
------------------------------------------------------- Add Fixed Property โฌ‡ --------------------------------------------------------
๐Ÿ’พ Define properties with static values that will always be added to the resulting object.

Supported formats:
  โœ“ Property name: Any valid object key
  โœ“ Value: Static value or GTM variable
Property (Target Object)Value
โŠ–
โŠ–
conProp
--------------------------------------------------- Add Conditional Property โฌ‡---------------------------------------------------
Condition
๐Ÿ” Enter a condition expression. If this evaluates to true, all properties below will be added to the object.

Example: {{Page Type}} === "checkout"
๐Ÿ’พ Properties that will be added only if the global condition above evaluates to true.

All properties in this table share the same condition.
Value
โŠ–
โŠ–
rulProp
-------------------------------------------------- Add Rule-Based Properties โฌ‡-------------------------------------------------
๐Ÿ’พ Each property has its own condition. A property is added only if its specific condition evaluates to true.

Properties are evaluated independently - multiple properties can be added if their conditions are met.
Property (Target Object)ValueCondition
โŠ–
โŠ–
Property Access
Setter Function (optional)
โš™๏ธ Optional function to customize how values are assigned: (obj, key, value) => {...}

Default behavior: obj[key] = value (shallow assignment).

Use this for:
  โ€ข Deep path assignment (e.g., obj.user.name = value)
  โ€ข Validation before setting
  โ€ข Custom 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
getType()


Under the Hood

๐Ÿ“œ View Implementation Code
/**
 * Transforms an object by adding fixed values, conditional properties, and rule-based properties.
 * 
 * @param {Object} data.src - Source object to transform.
 * @param {boolean} data.rul - Whether to preserve existing properties (true = don't overwrite, false = allow overwrite).
 * @param {boolean} data.afp - Whether to include fixed properties.
 * @param {Array<{key: string, val: any}>} data.ffp - Fixed properties to add.
 * @param {boolean} data.acp - Whether to include conditional properties.
 * @param {boolean} data.con - Global condition result.
 * @param {Array<{key: string, val: any}>} data.fcp - Conditional properties (added if condition is true).
 * @param {boolean} data.arb - Whether to include rule-based properties.
 * @param {Array<{key: string, val: any, con: boolean}>} data.rbp - Rule-based properties (each added if its condition is true).
 * @param {Function} [data.set] - Optional custom setter (defaults to shallow assignment).
 * @param {Function|string} [data.out] - Optional output handler.
 * 
 * @returns {Object} Transformed object.
 *
 * @framework ggLowCodeGTMKit
 */
const getType = require('getType');
const sourceObj = getType(data.src) === 'object' && data.src || {};
const overwrite = !data.rul; 

const setShallow = (obj, key, value, allowOverwrite) => {
    if (allowOverwrite || !obj.hasOwnProperty(key)) {
        obj[key] = value;
    }
};
const setter = typeof data.set === 'function' ? data.set : setShallow;

const assignFromPairs = (targetObj, keyValuePairs, setterFn, allowOverwrite) => {
    keyValuePairs.forEach(pair => {
        setterFn(targetObj, pair.key, pair.val, allowOverwrite);
    });
    return targetObj;
};

const fromFixed = data.afp ? (data.ffp || []) : [];
const fromCondition = data.acp && data.con ? (data.fcp || []) : [];
const fromRules = data.arb ? (data.rbp || []).filter(item => item.con) : [];
const finalPairs = fromFixed.concat(fromCondition, fromRules);

const result = assignFromPairs(sourceObj, finalPairs, setter, overwrite);

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

return out(result);
๐Ÿงช View Test Scenarios (9 tests)
โœ… '[example] Overwrite fixed properties'
โœ… Fixed properties with keep source - should not overwrite existing properties
โœ… '[example] Conditional properties added'
โœ… Conditional properties with false condition - should not add conditional properties
โœ… Rule-based properties with mixed conditions - should only add where condition is true
โœ… Combination of all property types with overwrite - should merge all applicable properties
โœ… Empty source object - should only contain added properties
โœ… No properties added - should return source object unchanged
โœ… Custom setter function - should use custom logic for assignment