Skip to content

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

VARIABLES β€Ί NUMBER
🧩 WEIGHTED (Predefined) EXTENDED 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.


Single condition score
INPUT
Conditions: [
{
"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
OUTPUT
false

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
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 true. If total score is below this value, returns false.

Example:
β€’ Threshold: 50
β€’ Total Score: 65 β†’ Returns true
β€’ Total Score: 30 β†’ Returns false
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
table
Input ValueConditionReference ValuePoints
Threshold Score (Optional) string
weighted()


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