Skip to content

uniq β€” GTM Variable Template for Array

VARIABLES β€Ί ARRAY
uniq EXTENDED Array
Direct (.tpl) Apply (.tpl)

Returns a new array with only unique values, removing all duplicates.



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]

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


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