extractOrigin — GTM Variable Template for URL
extractOrigin EXTENDED URL
Extracts the origin (protocol + hostname + port) from a URL.
Examples
HTTPS origin
INPUT
URL: https://example.com/path/to/page
OUTPUT
https://example.com
Custom port in origin
INPUT
URL: https://example.com:8080/path
OUTPUT
https://example.com:8080
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
extractOrigin
URL
💾 The URL to extract the origin from.
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
/**
* Extracts the origin (protocol + hostname + optional port) from a given URL.
*
* @param {string} data.src - The URL to extract the origin from.
* @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 {string|undefined} The origin of the URL, or undefined if the input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const parseUrl = require('parseUrl');
const extractOrigin = function(url) {
const parsed = parseUrl(url);
return parsed && parsed.origin || undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// extractOrigin - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(extractOrigin(value));
// ===============================================================================
// extractOrigin() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(extractOrigin(value));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] HTTPS origin'
✅ '[example] Custom port in origin'
✅ URL with subdomain - should extract full origin
✅ HTTP URL with query and hash - should extract origin only
✅ Invalid URL - should return undefined