encodeUri β GTM Variable Template for URL
encodeUri EXTENDED URL
Encodes a URI, preserving characters that have special meaning.
When to Use This
Section titled βWhen to Use ThisβExamples
Section titled βExamplesβEncode spaces in URI
INPUT
URI To Encode: https://example.com/path with spaces
OUTPUT
https://example.com/path%20with%20spaces
Clean URI unchanged
INPUT
URI To Encode: https://example.com/page
OUTPUT
https://example.com/page
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
encodeUri
URI To Encode
πΎ The URI to encode with percent-encoding for special characters.
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.
URI To Encode string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
encodeUriFunction()
Related Variables
Section titled βRelated VariablesβSame category: URL
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Encodes an entire URI (Uniform Resource Identifier), converting special characters into percent-encoded equivalents. * * @param {string} data.src - The URI to encode. * @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 encoding. * * @returns {string} The encoded URI with special characters converted to percent-encoded equivalents. * * @framework ggLowCodeGTMKit */const encodeUri = require('encodeUri');
const encodeUriFunction = function(uri) { return encodeUri(uri);};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// encodeUriFunction - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(encodeUriFunction(value));// ===============================================================================// encodeUriFunction() β Apply Mode// ===============================================================================/*return function(value) { return out(encodeUriFunction(value));};*/π§ͺ View Test Scenarios (7 tests)
β
'[example] Encode spaces in URI'β
URI with special characters - should encode special charsβ
URI with non-ASCII characters - should encode Unicodeβ
'[example] Clean URI unchanged'β
URI with hash and query parameters - should preserve structureβ
Non-string input - should handle gracefullyβ
Object input - should convert to string and encode