hasFragment — GTM Variable Template for URL
Examples
Has fragment returns true
INPUT
URL To Check: https://example.com/page#section
OUTPUT
true
No fragment 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
hasFragment
URL To Check
💾 The full URL to check for fragment identifier.
Supported formats:
✓ 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.
Related Variables
Same category: URL
Under the Hood
📜 View Implementation Code
/**
* Checks whether a given URL contains a fragment identifier.
*
* @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 contains a fragment, otherwise false.
*
* @framework ggLowCodeGTMKit
*/
const parseUrl = require('parseUrl');
const hasFragment = function(url) {
return !!parseUrl(url) && parseUrl(url).hash !== "";
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// hasFragment - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(hasFragment(value));
// ===============================================================================
// hasFragment() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(hasFragment(value));
};
*/🧪 View Test Scenarios (6 tests)
✅ '[example] Has fragment returns true'
✅ '[example] No fragment returns false'
✅ URL with empty fragment
✅ URL with fragment and query params
✅ Invalid URL
✅ Undefined input