omitParamsFromString β GTM Variable Template for URL
omitParamsFromString CORE URL
Removes specified parameters from a query string or fragment. Useful for cleaning URLs by removing tracking parameters like fbclid, gclid, or PII.
When to Use This
Section titled βWhen to Use Thisβ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.
Examples
Section titled βExamplesβ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
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
keys: ['debug']
Parameters to Omit: [{key: 'debug'}]
Case insensitive matching: false
OUTPUT
#section=intro&id=123
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.
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"
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***
*** 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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
omitParamsFromString()
Related Variables
Section titled βRelated VariablesβSame category: URL
Under the Hood
Section titled βUnder the Hoodβπ 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