Skip to content

extractSearchParams β€” GTM Variable Template for URL

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

Extracts query parameters from a URL as key-value pairs or a specific parameter.


URL Processing

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

Extraction

Pull specific values, segments, or patterns from complex data structures.


Extract all parameters
INPUT
URL: https://example.com?foo=bar&baz=qux
Parameter Key: undefined
OUTPUT
{foo: 'bar', baz: 'qux'}
Extract specific parameter
INPUT
URL: https://example.com?foo=bar&baz=qux
Parameter Key: foo
OUTPUT
bar

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
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***
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.


πŸ“œ 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