Skip to content

omitParamsFromString β€” GTM Variable Template for URL

VARIABLES β€Ί URL
omitParamsFromString CORE URL
Direct (.tpl) Apply (.tpl)

Removes specified parameters from a query string or fragment. Useful for cleaning URLs by removing tracking parameters like fbclid, gclid, or PII.


String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.

Filtering

Select or exclude items from collections based on criteria or predicates.

URL Processing

Parse, build, decode, and manipulate URLs and query parameters.


Remove tracking parameters
INPUT
Query String or Fragment: ?utm_source=google&fbclid=123&gclid=456&utm_medium=cpc
keys: ['fbclid', 'gclid']
Parameters to Omit: [{key: 'fbclid'}, {key: 'gclid'}]
Case insensitive matching: false
OUTPUT
?utm_source=google&utm_medium=cpc
Clean hash fragment
INPUT
Query String or Fragment: #section=intro&debug=true&id=123
keys: ['debug']
Parameters to Omit: [{key: 'debug'}]
Case insensitive matching: false
OUTPUT
#section=intro&id=123

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

omitParamsFromString
Query String or Fragment
πŸ’Ύ The query string or fragment to filter.

Supported formats:
  βœ“ Query string: "?utm_source=google&fbclid=123"
  βœ“ Fragment: "#section=intro&debug=true"
  βœ“ Without prefix: "utm_source=google&fbclid=123"
Parameters to Omit
πŸ’Ύ List of parameter names to remove from the string.

*** Remove tracking parameters***

*** Clean hash fragment***
βŠ–
βš™οΈ Enable to match parameter names regardless of case (e.g., "FBCLID" matches "fbclid").
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input string before filtering.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it.
Query String or Fragment string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Parameters to Omit list
omitParamsFromString()


πŸ“œ View Implementation Code
/**
* Filters a query string or fragment to exclude specified parameters.
*
* @param {string} data.src - A string starting with "?" or "#" to filter (e.g., "?a=1&b=2").
* @param {Array} data.kys - Array of objects with parameter names to exclude.
* @param {boolean} [data.ci] - Optional flag to enable case-insensitive matching.
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function.
*
* @returns {string} A filtered parameter string excluding the matching key-value pairs.
*
* @framework ggLowCodeGTMKit
*/
const decodeUriComponent = require('decodeUriComponent');
const encodeUriComponent = require('encodeUriComponent');
const getType = require('getType');
const Object = require('Object');
const createFlatArrayFromValues = function(list, property) {
const result = [];
if (!list) return result;
for (let i = 0; i < list.length; i++) {
const val = list[i][property];
if (getType(val) === 'array') {
for (let j = 0; j < val.length; j++) {
result.push(val[j]);
}
} else if (val) {
result.push(val);
}
}
return result;
};
const omitParamsFromString = function(input, keys, caseInsensitive) {
if (!input || !keys || keys.length === 0) {
return input || '';
}
const omitMap = {};
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (typeof key === 'string') {
omitMap[caseInsensitive ? key.toLowerCase() : key] = true;
}
}
if (Object.keys(omitMap).length === 0) {
return input;
}
const hasPrefix = input.charAt(0) === '?' || input.charAt(0) === '#';
const prefix = hasPrefix ? input.charAt(0) : '';
const raw = hasPrefix ? input.slice(1) : input;
if (!raw) {
return prefix;
}
const filteredPairs = [];
const pairs = raw.split('&');
for (let i = 0; i < pairs.length; i++) {
const pair = pairs[i];
if (!pair) continue;
const eqIndex = pair.indexOf('=');
const key = eqIndex >= 0 ? pair.slice(0, eqIndex) : pair;
const lookupKey = caseInsensitive ? key.toLowerCase() : key;
if (!omitMap[lookupKey]) {
if (eqIndex >= 0) {
const value = pair.slice(eqIndex + 1);
const decodedValue = decodeUriComponent(value);
filteredPairs.push(key + '=' + encodeUriComponent(decodedValue));
} else {
filteredPairs.push(key);
}
}
}
const result = filteredPairs.join('&');
return result ? (prefix + result) : prefix;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// omitParamsFromString - Dire
πŸ§ͺ View Test Scenarios (6 tests)
βœ… Test omit single parameter
βœ… '[example] Remove tracking parameters'
βœ… Test case insensitive matching
βœ… '[example] Clean hash fragment'
βœ… Test empty input returns empty string
βœ… Test no keys returns original input