extractUserName β GTM Variable Template for URL
extractUserName EXTENDED URL
Extracts the username from a URL with authentication credentials.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβExtract username
INPUT
URL: https://[email protected]/path
OUTPUT
john
No username returns undefined
INPUT
URL: https://example.com/path
OUTPUT
undefined
GTM Configuration
Section titled β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
extractUserName
URL
πΎ The URL to extract the username 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
Section titled βRelated VariablesβSame category: URL
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Extracts the username from a given URL. * * @param {string} data.src - The URL to extract the username 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 username from the URL, or undefined if the input is invalid. * * @framework ggLowCodeGTMKit */
const parseUrl = require('parseUrl');
const extractUserName = function(url) { const parsed = parseUrl(url); return parsed && parsed.username || undefined;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// extractUserName - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(extractUserName(value));
// ===============================================================================// extractUserName() β Apply Mode: runtime parameter// ===============================================================================/*return function(value) { return out(extractUserName(value));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Extract username'β
URL with username and password - extracts only usernameβ
'[example] No username returns undefined'β
Invalid URL - returns undefinedβ
Empty string input - returns undefined