extractSearchParams — GTM Variable Template for URL
extractSearchParams CORE URL
Extracts query parameters from a URL as key-value pairs or a specific parameter.
When to Use This
URL Processing
Parse, build, decode, and manipulate URLs and query parameters.
Extraction
Pull specific values, segments, or patterns from complex data structures.
Examples
Extract all parameters
INPUT
URL: https://example.com?foo=bar&baz=qux
Parameter Key: undefined
Parameter Key: undefined
OUTPUT
{foo: 'bar', baz: 'qux'}
Extract specific parameter
INPUT
URL: https://example.com?foo=bar&baz=qux
Parameter Key: foo
Parameter Key: foo
OUTPUT
bar
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
Read-only Preview
extractSearchParams
URL
💾 The URL to extract the search parameters from.
Supported formats:
✓ String
Supported formats:
✓ String
Parameter Key
💾 The specific query parameter key to extract (e.g., 'utm_source', 'id'). If provided, returns only the value of this parameter instead of the entire object.
Supported formats:
✓ String
*** Extract all parameters***
*** Extract specific parameter***
Supported formats:
✓ String
*** Extract all parameters***
*** Extract specific parameter***
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the URL before internal logic (e.g., normalize URL format, add base domain). Internal transformations will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., value => value.toUpperCase() for string values, params => Object.keys(params).length to count parameters). Useful for chaining transformations on the output.
Related Variables
Same category: URL
Under the Hood
📜 View Implementation Code
/**
* Extracts the searchParams from a given URL, optionally filtering for a specific parameter.
*
* @param {string} data.src - The URL to extract the searchParams from.
* @param {string} [data.key] - Optional specific query parameter key to extract.
* @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 extracting.
*
* @returns {Object<string, (string|Array)>|string|Array|undefined} The searchParams object, specific parameter value, or undefined if invalid.
*
* @framework ggLowCodeGTMKit
*/
const parseUrl = require('parseUrl');
const extractSearchParams = function(url, key) {
const parsed = parseUrl(url);
if (!parsed || !parsed.searchParams) {
return undefined;
}
if (key !== undefined && key !== null && key !== '') {
return parsed.searchParams[key];
}
return parsed.searchParams;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// extractSearchParams - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(extractSearchParams(value, data.key));
// ===============================================================================
// extractSearchParams(...) – Apply Mode
// ===============================================================================
/*
return function(value, key) {
key = data.rp1 ? data.key : key;
return out(extractSearchParams(value, key));
};
*/🧪 View Test Scenarios (8 tests)
✅ '[example] Extract all parameters'
✅ '[example] Extract specific parameter'
✅ Extract non-existent parameter
✅ URL without search parameters
✅ Extract parameter with duplicate values (array)
✅ Invalid URL
✅ Empty key parameter (should return all params)
✅ UI-bound mode with static key