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