Skip to content

๐Ÿงฉ WEIGHTED (Advanced) โ€” GTM Variable Template for Logic

VARIABLES โ€บ LOGIC
๐Ÿงฉ WEIGHTED (Advanced) CORE Logic

Each condition contributes a score when its rule evaluates to true. The final result is true if the total score meets or exceeds the threshold.


Examples

Score without threshold
INPUT
Conditions: [

{condition:
{
Input Value: 'hello',
Custom Function: (v, r) => v === r,
Reference: 'hello'
},

Points:
10
},

{condition:
{
Input Value: 'world',
Custom Function: (v, r) => v === r,
Reference: 'world'
},

Points:
5
},

{condition:
{
Input Value: 'test',
Custom Function: (v, r) => v === r,
Reference: 'fail'
},

Points:
3
}
]

Threshold Score:
undefined
OUTPUT
15
Below threshold returns 0
INPUT
Conditions: [

{condition:
{
Input Value: 'test',
Custom Function: (v, r) => v === r,
Reference: 'fail'
},

Points:
10
},

{condition:
{
Input Value: 'hello',
Custom Function: (v, r) => v === r,
Reference: 'hello'
},

Points:
5
}
]

Threshold Score:
10
OUTPUT
false

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
๐Ÿงฉ WEIGHTED (Advanced)
SUM OUT โฌ‡
๐Ÿ’ก Tips:
โ€ข All conditions are evaluated (unlike IF-ELSE-IF)
โ€ข Your custom functions can contain any JavaScript logic
โ€ข Use positive points for desired matches, negative for penalties
โ€ข Access GTM APIs like require('makeNumber') inside functions
Input ValueCustom FunctionReference ValuePoints
โŠ–
โŠ–
Threshold Settings
Enable to return true/false based on whether the total score meets the threshold. If disabled, returns the raw total score as a number.
Threshold Score
Minimum total score required to return true. If total score is below this value, returns false.

Example:
โ€ข Threshold: 50
โ€ข Total Score: 65 โ†’ Returns true
โ€ข Total Score: 30 โ†’ Returns false
Input Setup
Cast Function (optional)
โš™๏ธ Optional function to transform both val and ref before passing them to your custom functions.

Applied consistently to all conditions for normalization.

Function signature: (value) => transformedValue

Examples:
โ€ข v => String(v).toLowerCase() โ†’ Convert to lowercase
โ€ข v => String(v).trim() โ†’ Remove whitespace
โ€ข v => { const makeNumber = require('makeNumber'); return makeNumber(v) || v; } โ†’ Convert to number
โ€ข v => v && v.price ? v.price : 0 โ†’ Extract object property
โ€ข v => JSON.parse(v) โ†’ Parse JSON string

๐Ÿ’ก If not provided, values are passed to custom functions unchanged.
Result Handling
Output Function (optional)
โš™๏ธ Optional function to transform the final result before returning it.

Applied to the boolean (if threshold set) or numeric score (if no threshold).

Examples:
โ€ข score => score * 2 โ†’ Double the score
โ€ข bool => bool ? 'Pass' : 'Fail' โ†’ Convert to text
โ€ข val => Math.round(val) โ†’ Round score
โ€ข score => score + ' points' โ†’ Add label


Under the Hood

๐Ÿ“œ View Implementation Code
/**
 * Evaluates conditions using weighted point scoring with custom functions.
 * Each condition has a point value, returns true if total score meets threshold.
 *
 * Direct-mode only
 * @param {Array<Object>} data.ls1 - List of condition objects with points
 * @param {Object} data.ls1[].condition - Condition object for this item
 * @param {*} data.ls1[].condition.val - Value to evaluate
 * @param {Function} data.ls1[].condition.fn - Custom function that receives (val, ref) and returns boolean
 * @param {*} data.ls1[].condition.ref - Reference value to compare against
 * @param {number} data.ls1[].pts - Points awarded if this condition passes
 * @param {number} data.thr - Minimum score required to return true
 * @param {Function} [data.cast] - Optional cast function applied to both val and ref before passing to custom function
 * @param {Function} [data.out] - Optional function to transform the final result
 *
 * @returns {boolean|number} True/false if threshold provided, or total score if no threshold.
 *
 * @note Custom Functions Version - Use for complex scoring logic not covered by predefined operators
 *
 * @framework ggLowCodeGTMKit
 */
const makeNumber = require('makeNumber');

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

const conditions = data.ls1 || [];
const threshold = makeNumber(data.thr);

const evaluateCondition = (cond) => {
    if (!cond) return false;
    
    const fn = cond.fn;
    if (typeof fn !== 'function') return false;
    
    const val = castFn(cond.val);
    const ref = castFn(cond.ref);
    
    return fn(val, ref);
};

let totalScore = 0;

for (let i = 0; i < conditions.length; i++) {
    const item = conditions[i];
    if (!item || !item.condition) {
        continue;
    }
    
    const pointsValue = makeNumber(item.pts);
    const points = (typeof pointsValue === 'number' && pointsValue === pointsValue) ? pointsValue : 0;
    
    if (evaluateCondition(item.condition)) {
        totalScore += points;
    }
}

const result = (typeof threshold === 'number' && threshold === threshold) ? totalScore >= threshold : totalScore;

return out(result);
๐Ÿงช View Test Scenarios (20 tests)
โœ… '[example] Score without threshold'
โœ… With threshold - score meets threshold
โœ… '[example] Below threshold returns 0'
โœ… Points as strings
โœ… Negative points
โœ… With cast function - lowercase normalization
โœ… With cast function - numeric conversion
โœ… Custom function with object property comparison
โœ… Complex custom logic
โœ… With output function (double the score)
โœ… With output function and threshold
โœ… All conditions fail - zero score
โœ… Invalid function (not a function) - skipped
โœ… Missing condition object - skipped
โœ… Invalid points (non-numeric string) defaults to 0
โœ… Empty conditions array
โœ… Threshold exactly equals score
โœ… Negative total score with threshold
โœ… Cast with property extraction
โœ… omplex scenario - mix of conditions and cast