Skip to content

🧩 WEIGHTED (Advanced) β€” GTM Variable Template for Number

VARIABLES β€Ί NUMBER
🧩 WEIGHTED (Advanced) CORE Number
Direct (.tpl)

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.


Number Operations

Arithmetic, rounding, clamping, and mathematical transformations on numeric values.

GA4 Ecommerce

Build and transform ecommerce data structures for GA4 event tracking.


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

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


πŸ“œ 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