uniq — GTM Variable Template for Array
uniq EXTENDED Array
Returns a new array with only unique values, removing all duplicates.
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]
GTM Configuration
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
Same category: Array
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