๐๐๐๐๐๐ Smart Generator - Object โ GTM Variable Template for 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
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
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.
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
โข 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
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:
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.
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.
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:
Default behavior:
Use this for:
โข Deep path assignment (e.g.,
โข Validation before setting
โข Custom assignment logic
(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
๐ Result Handling โ Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
getType()
Related Variables
Same category: GTM
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