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
Section titled β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
Section titled β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
Section titled β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
Section titled βRelated VariablesβSame category: URL
Under the Hood
Section titled β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 ? key : data.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