Skip to content

๐Ÿงฉ WEIGHTED (Predefined) โ€” GTM Variable Template for Logic

VARIABLES โ€บ 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
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

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
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()


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