Skip to content

⚡ OBJECT › Smart Generator — GTM Variable Template for Object

VARIABLES › OBJECT
⚡ OBJECT › Smart EXTENDED Object
Direct (.tpl)

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


GTM Utilities

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

Type Conversion

Safely convert between data types — strings, numbers, booleans, arrays, objects.


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}

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

⚡ OBJECT › Smart
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
buildSmartObject()


📜 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 buildSmartObject = function(src, rul, afp, ffp, acp, con, fcp, arb, rbp, set) {
const sourceObj = getType(src) === 'object' && src || {};
const overwrite = !rul;
const setShallow = (obj, key, value, allowOverwrite) => {
if (allowOverwrite || !obj.hasOwnProperty(key)) {
obj[key] = value;
}
};
const setter = typeof set === 'function' ? set : setShallow;
const assignFromPairs = (targetObj, keyValuePairs, setterFn, allowOverwrite) => {
keyValuePairs.forEach(pair => {
setterFn(targetObj, pair.key, pair.val, allowOverwrite);
});
return targetObj;
};
const fromFixed = afp ? (ffp || []) : [];
const fromCondition = acp && con ? (fcp || []) : [];
const fromRules = arb ? (rbp || []).filter(item => item.con) : [];
const finalPairs = fromFixed.concat(fromCondition, fromRules);
return assignFromPairs(sourceObj, finalPairs, setter, overwrite);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
return out(buildSmartObject(data.src, data.rul, data.afp, data.ffp, data.acp, data.con, data.fcp, data.arb, data.rbp, data.set));
🧪 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