π§© WEIGHTED (Advanced) β GTM Variable Template for Number
π§© WEIGHTED (Advanced) CORE Number
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.
When to Use This
Section titled βWhen to Use ThisβNumber Operations
Arithmetic, rounding, clamping, and mathematical transformations on numeric values.
GA4 Ecommerce
Build and transform ecommerce data structures for GA4 event tracking.
Examples
Section titled β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
{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
{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
Section titled β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
β’ 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 functionsInput 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
Example:
β’ Threshold:
β’ Total Score:
β’ Total Score:
true. If total score is below this value, returns false.Example:
β’ Threshold:
50β’ Total Score:
65 β Returns trueβ’ Total Score:
30 β Returns falseInput 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:
Examples:
β’
β’
β’
β’
β’
π‘ If not provided, values are passed to custom functions unchanged.
Applied consistently to all conditions for normalization.
Function signature:
(value) => transformedValueExamples:
β’
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:
β’
β’
β’
β’
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 labelRelated Variables
Section titled βRelated VariablesβSame category: Number
Under the Hood
Section titled β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