Skip to content

hasQueryString — GTM Variable Template for URL

VARIABLES › URL
hasQueryString EXTENDED URL

Checks if a URL contains a query string.



Examples

Has query string
INPUT
URL To Check: https://example.com/page?param=value
OUTPUT
true
No query string returns false
INPUT
URL To Check: https://example.com/page
OUTPUT
false

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
hasQueryString
URL To Check
💾 The full URL to check for query string.

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.


Under the Hood

📜 View Implementation Code
/**
* Checks whether a given URL contains a query string.
* 
* @param {string} data.src - The full URL 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 URL has a query string, otherwise false.
*
* @framework ggLowCodeGTMKit
*/
const parseUrl = require('parseUrl');

const hasQueryString = function(url) {
   return !!parseUrl(url) && parseUrl(url).search !== "";
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// hasQueryString - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(hasQueryString(value));
// ===============================================================================
// hasQueryString() – Apply Mode
// ===============================================================================
/*
return function(value) {
  return out(hasQueryString(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Has query string'
✅ URL with multiple query parameters - returns true
✅ '[example] No query string returns false'
✅ URL with only question mark but no parameters - returns false
✅ URL with hash but no query string - returns false