uniq β GTM Variable Template for Array
uniq EXTENDED Array
Returns a new array with only unique values, removing all duplicates.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβRemove duplicates
INPUT
Array: [1, 2, 2, 3, 4, 3, 5]
OUTPUT
[1, 2, 3, 4, 5]
No duplicates unchanged
INPUT
Array: [1, 2, 3, 4, 5]
OUTPUT
[1, 2, 3, 4, 5]
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.
uniq
Array
πΎ The array to process for unique values.
Supported formats:
β Array
Supported formats:
β Array
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., convert string to array, filter values). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the result before returning it (e.g., arr => arr.length, arr => arr.join(',')). Useful for chaining transformations on the output.
Array array
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
uniq()
Related Variables
Section titled βRelated VariablesβSame category: Array
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Returns a new array with only unique values, removing all duplicates. * * @param {Array} data.src - The array to process for unique values. * @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format. * * Direct-mode specific parameters: * @param {Function} [data.pre] - Optional pre-processor function to transform src before getting unique values. * * @returns {Array} A new array containing only unique values from the input array. * * @framework ggLowCodeGTMKit */const getType = require('getType');
const uniq = function(value) { if (getType(value) !== 'array') { return []; } const result = []; for (let i = 0; i < value.length; i++) { const val = value[i]; let found = false; for (let j = 0; j < result.length; j++) { if (result[j] === val || (val !== val && result[j] !== result[j])) { // Handle NaN found = true; break; } } if (!found) { result.push(val); } } return result;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// uniq - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(uniq(value));
// ===============================================================================// uniq() β Apply Mode: runtime parameter// ===============================================================================/*return function(value) { return out(uniq(value));};*/π§ͺ View Test Scenarios (8 tests)
β
'[example] Remove duplicates'β
Array with string duplicates - should return unique stringsβ
'[example] No duplicates unchanged'β
Empty array - should return empty arrayβ
Non-array input - should return empty arrayβ
Array with mixed types - should handle different typesβ
Single element array - should return same arrayβ
Array with undefined and null - should handle special values