Skip to content

encodeUri — GTM Variable Template for URL

VARIABLES › URL
encodeUri EXTENDED URL

Encodes a URI, preserving characters that have special meaning.



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

GTM Configuration

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
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
encodeUriFunction()


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