Skip to content

toBase64 — GTM Variable Template for String

VARIABLES › STRING
toBase64 EXTENDED String

Converts a string to a Base64-encoded string, making it suitable for transmission or storage in contexts that require Base64 encoding.



Examples

Encode simple string
INPUT
String To Encode: Hello
OUTPUT
SGVsbG8=
Encode special characters
INPUT
String To Encode: Hello@2024!
OUTPUT
SGVsbG9AMjAyNCE=

GTM Configuration

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

toBase64
String To Encode
💾 The string to encode.

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.
String To Encode string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
toBase64()


Under the Hood

📜 View Implementation Code
/**
 * Encodes a string into Base64 format.
 * 
 * @param {string} data.src - The string to be encoded into Base64 format.
 * @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 Base64-encoded string.
 *
 * @framework ggLowCodeGTMKit
 */
const toBase64 = require('toBase64');
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// toBase64 - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(toBase64(value));
// ===============================================================================
// toBase64() – Apply Mode
// ===============================================================================
/*
return function(value) {
   return out(toBase64(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Encode simple string'
✅ String with spaces - should encode to Base64
✅ '[example] Encode special characters'
✅ Empty string - should return Base64 representation of empty string
✅ Numeric string - should encode to Base64