π§© WEIGHTED (Predefined) β GTM Variable Template for Number
π§© WEIGHTED (Predefined) EXTENDED 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βSingle condition score
INPUT
Conditions: [
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "hello",
"Points": "10"
}
]
Threshold Score: undefined
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "hello",
"Points": "10"
}
]
Threshold Score: undefined
OUTPUT
10
Below threshold returns 0
INPUT
Conditions: [
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "goodbye",
"Points": "10"
},
{
"Input Value": "world",
"Condition": "eq",
"Reference Value": "world",
"Points": "5"
}
]
Threshold Score: 10
{
"Input Value": "hello",
"Condition": "eq",
"Reference Value": "goodbye",
"Points": "10"
},
{
"Input Value": "world",
"Condition": "eq",
"Reference Value": "world",
"Points": "5"
}
]
Threshold Score: 10
OUTPUT
false
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
π§© WEIGHTED (Predefined)
SUM OUT β¬
π‘ Tips:
β’ All conditions are evaluated (unlike IF-ELSE-IF)
β’ Use positive points for desired matches, negative for penalties
β’ Combine different operator types for sophisticated scoring
β’ All conditions are evaluated (unlike IF-ELSE-IF)
β’ Use positive points for desired matches, negative for penalties
β’ Combine different operator types for sophisticated scoring
Input ValueConditionReference 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 falseResult 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 table
Input ValueConditionReference ValuePoints
Threshold Score (Optional) string
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
weighted()
Related 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 system with predefined operators. * Each condition has a point value, returns true if total score meets threshold. * * @param {Array<Object>} ls1 - List of condition objects with points. * @param {number} thr - Minimum score required to return true. * @param {boolean} eth - Whether threshold is enabled. * * @returns {boolean|number} True/false if threshold provided, or total score if no threshold. * * @framework ggLowCodeGTMKit */const makeNumber = require('makeNumber');
const weighted = function(ls1, thr, eth) { const toStr = function(val) { if (val === null) return 'null'; if (val === undefined) return 'undefined'; return typeof val === 'string' ? val : val.toString(); };
const toLower = function(str) { return str.toLowerCase(); };
const convertToNumber = function(value) { if (typeof value === 'number') return value; if (typeof value !== 'string') return value; const num = makeNumber(value); return (typeof num === 'number' && num === num) ? num : value; };
const ruleMethods = { eq: function(val, ref) { return val == ref; }, ct: function(val, ref) { return val.indexOf(ref) > -1; }, sw: function(val, ref) { return val.substring(0, ref.length) === ref; }, ew: function(val, ref) { return val.substring(val.length - ref.length) === ref; }, re: function(val, ref) { return val.search(ref) !== -1; }, xlt: function(val, ref) { return val < ref; }, xlte: function(val, ref) { return val <= ref; } };
const evaluateCondition = function(cond) { if (!cond) return false;
const condKey = cond.con || ''; if (condKey.length === 0) return false;
const firstChar = condKey[0]; const lastChar = condKey[condKey.length - 1];
const isNegated = firstChar === 'n' && condKey.length > 1; const isInsensitive = lastChar === 'i' && condKey.length > 1;
const baseStart = isNegated ? 1 : 0; const baseEnd = isInsensitive ? condKey.length - 1 : condKey.length; const baseCondition = condKey.substring(baseStart, baseEnd);
const ruleFn = ruleMethods[baseCondition]; if (!ruleFn) return false;
var val = cond.val; var ref = cond.ref;
const isNumericRule = baseCondition[0] === 'x';
if (isNumericRule) { val = convertToNumber(val); ref = convertToNumber(ref); } else { val = toStr(val); ref = toStr(ref);
if (isInsensitive) { val = toLower(val); ref = toLower(ref); } }
const result = ruleFn(val, ref); return isNegated ? !result : result; };
// Weighted scoring evaluation const conditions = ls1 || []; const threshold = makeNumber(thr); var totalScore = 0;
for (var i = 0; i < conditions.lπ§ͺ View Test Scenarios (28 tests)
β
'[example] Single condition score'β
With threshold - score meets thresholdβ
'[example] Below threshold returns 0'β
Points as stringsβ
Negative pointsβ
Contains operatorβ
Case insensitive equalsβ
Negation - not containsβ
Starts with case insensitiveβ
Ends withβ
Regex matchβ
Numeric less thanβ
Numeric not less than (greater or equal)β
Numeric less than or equalβ
All conditions fail - zero scoreβ
null becomes string null for string comparisonβ
undefined becomes string undefinedβ
boolean true becomes string trueβ
boolean false becomes string falseβ
number becomes stringβ
Invalid points (non-numeric string) defaults to 0β
Invalid condition (empty operator) skippedβ
With output function (double the score)β
With output function and thresholdβ
Threshold exactly equals scoreβ
Complex scenario - mix of operators and conversionsβ
Negative total score with thresholdβ
Reference value stringification