Skip to content

hasParameterWithValue — GTM Variable Template for URL

VARIABLES › URL
hasParameterWithValue EXTENDED URL

Checks if a URL contains a specific parameter with a non-empty value.



Examples

Parameter with value
INPUT
Query/Fragment String: ?utm_source=google&utm_medium=cpc
Parameter Name: utm_source
OUTPUT
true
Missing parameter returns false
INPUT
Query/Fragment String: ?param1=value¶m2=test
Parameter Name: param3
OUTPUT
false

GTM Configuration

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

hasParameterWithValue
Query/Fragment String
💾 A string starting with "?" or "#" containing parameters.

Supported formats:
  ✓ String
Parameter Name
🔍 The parameter name to check for existence and non-empty value.

Supported formats:
  ✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Query/Fragment String string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Parameter Name string
hasParameterWithValue()


Under the Hood

📜 View Implementation Code
/**
* Checks if a specific parameter exists and has a non-empty value in a query or fragment string.
* 
* @param {string} data.src - A string starting with "?" or "#" (e.g. "?a=1&b=2").
* @param {string} data.prm - The parameter name to check.
* @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 checking.
* 
* @returns {boolean} True if the parameter exists and has a non-empty value.
*
* @framework ggLowCodeGTMKit
*/
const hasParameterWithValue = function(input, paramName) {
   if (typeof input !== 'string' || typeof paramName !== 'string' || !input || !paramName) {
       return false;
   }
   const raw = input.charAt(0) === '?' || input.charAt(0) === '#' ? input.slice(1) : input;
   if (!raw) return false;
   
   const pattern = '(^|&)' + paramName + '=([^&]+)';
   const match = raw.match(pattern);
   
   return match !== null;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// hasParameterWithValue - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(hasParameterWithValue(value, data.prm));
// ===============================================================================
// hasParameterWithValue(...) – Apply Mode
// ===============================================================================
/*
return function(value, paramName) {
   paramName = data.rp1 ? data.prm : paramName;
   return out(hasParameterWithValue(value, paramName));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Parameter with value'
✅ Parameter exists but with empty value - returns false
✅ '[example] Missing parameter returns false'
✅ Fragment string with parameter - returns true
✅ Empty or invalid input - returns false