๐งฉ WEIGHTED (Predefined) โ GTM Variable Template for Logic
๐งฉ WEIGHTED (Predefined) EXTENDED 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
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
GTM Configuration
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
Same category: Logic
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