Skip to content

๐‚๐Ž๐Œ๐Œ๐€๐๐ƒ ๐“๐€๐๐‹๐„ Lazy Generator (Advanced) - Object โ€” GTM Variable Template for GTM

VARIABLES โ€บ GTM
๐‚๐Ž๐Œ๐Œ๐€๐๐ƒ ๐“๐€๐๐‹๐„ Lazy Generator (Advanced) - Object CORE GTM

Generates an object from global and event-specific parameters using a command table pattern.


Examples

Global parameters
INPUT
Add Global Parameter (not Lazy and shared): true
gpm: [
Add Event Parameter (Lazy): false
OUTPUT
{tracker_id: 'UA-12345', send_page_view: 'true'}
Event parameters match
INPUT
Add Global Parameter (not Lazy and shared): false
Add Event Parameter (Lazy): true
epm: [
inp: purchase
urm: false
OUTPUT
{transaction_id: 'TX-123'}

GTM Configuration

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

Read-only Preview
๐‚๐Ž๐Œ๐Œ๐€๐๐ƒ ๐“๐€๐๐‹๐„ Lazy Generator (Advanced) - Object
OBJECT โฌ‡
โœ“ Include global parameters that are always added to the output object, regardless of event matching.
โœ“ Include event-specific parameters that are only evaluated and added when the input variable matches.
globalParam
-------------------------------------------------------- Global Parameters โฌ‡--------------------------------------------------------
๐Ÿ’พ Global parameters that are always included in the output object.

Process as:
  โ€ข Value: Use as-is
  โ€ข Data Layer Variable: Read from dataLayer using the value as path
  โ€ข Lazy Variable: Evaluate function on execution
Global ParameterValueProcess as
โŠ–
โŠ–
eventParam
--------------------------------------------------------- Event Parameters โฌ‡--------------------------------------------------------
Input Variable
๐Ÿ” The variable to match against event parameter input patterns.

Example: {{Event Name}} or {{Page Type}}
Enable regex pattern matching for event parameter inputs. When unchecked, uses exact string matching.
๐Ÿ’พ Event-specific parameters that are only included when the Input Pattern matches the Input Variable above.

Input Pattern:
  โ€ข Exact match (default): "page_view"
  โ€ข Regex match (if enabled): "^purchase.*"

Process as:
  โ€ข Value: Use as-is
  โ€ข Data Layer Variable: Read from dataLayer
  โ€ข Lazy Variable: Evaluate function on execution
Input PatternEvent ParameterValueProcess as
โŠ–
โŠ–
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
  โ€ข 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.


Under the Hood

๐Ÿ“œ View Implementation Code
/**
 * Creates a parameter object for tracker commands with global and event-specific parameters.
 * Event parameters are evaluated only when input matches the condition.
 * 
 * @param {boolean} data.agp - Whether to include global parameters.
 * @param {Array<{key: string, val: any, typ?: string}>} data.gpm - Global parameters (always included).
 * @param {boolean} data.aep - Whether to include event parameters.
 * @param {Array<{key: string, val: any, typ?: string, inp: string}>} data.epm - Event parameters with input matching.
 * @param {string} data.inp - Input variable to match against event parameters.
 * @param {boolean} data.urm - Whether to use regex matching (true) or exact matching (false).
 * @param {Function} [data.set] - Optional custom setter function (defaults to shallow assignment).
 * @param {Function|string} [data.out] - Optional output handler.
 * 
 * @returns {Object} Parameter object with applied transformations.
 *
 * @framework ggLowCodeGTMKit
 */
const copyFromDataLayer = require('copyFromDataLayer');

const globalParameters = data.agp ? (data.gpm || []) : [];
const eventParameters = data.aep ? (data.epm || []) : [];
const inputVariable = data.inp;
const useRegex = data.urm;

const matchRule = function(pattern, value, isRegex) {
    if (isRegex) {
        return value && value.toString().match(pattern);
    }
    return pattern === value;
};

const applyTypToVal = function(arr) {
    return arr.map(item => {
        let val = item.val;
        if (item.typ === 'dlv') {
            val = copyFromDataLayer(val);
        } else if (item.typ === 'lzv' && typeof val === 'function') {
            val = val();
        }
        return {
            key: item.key,
            val: val
        };
    });
};

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

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

const eventParametersToKeep = eventParameters.filter(x => matchRule(x.inp, inputVariable, useRegex));
let finalTable = globalParameters.concat(eventParametersToKeep);
finalTable = applyTypToVal(finalTable);

const result = assignFromPairs({}, finalTable, setter);

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

return out(result);


___WEB_PERMISSIONS___

[
  {
    "instance": {
      "key": {
        "publicId": "read_data_layer",
        "versionId": "1"
      },
      "param": [
        {
          "key": "allowedKeys",
          "value": {
            "type": 1,
            "string": "any"
          }
        }
      ]
    },
    "clientAnnotations": {
      "isEditedByUser": true
    },
    "isRequired": true
  }
]
๐Ÿงช View Test Scenarios (10 tests)
โœ… '[example] Global parameters'
โœ… '[example] Event parameters match'
โœ… Event parameters with no match - should return empty object
โœ… Event parameters with regex match - should include parameters matching regex pattern
โœ… Combination of global and event parameters - should merge both
โœ… Lazy Variable processing - should evaluate function
โœ… Multiple event parameter matches with regex - should include all matching patterns
โœ… No parameters enabled - should return empty object
โœ… Custom setter and output function - should apply custom logic
โœ… Data Layer Variable processing - should read from dataLayer