Skip to content

right — GTM Variable Template for String

VARIABLES › STRING
right CORE String

Returns a substring from the end of a specified string, extracting the specified number of characters from the right.


When to Use This

String Manipulation

Transform, clean, and normalize text data for consistent downstream processing.

Extraction

Pull specific values, segments, or patterns from complex data structures.


Examples

Extract last 5 characters
INPUT
Source String: Hello World
Number of Characters: 5
OUTPUT
World
Count exceeds string length
INPUT
Source String: Hello
Number of Characters: 10
OUTPUT
Hello
Non-string input
INPUT
Source String: 12345
Number of Characters: 3
OUTPUT
""

GTM Configuration

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

right
Source String
💾 The string from which to extract the right portion.

Supported formats:
  ✓ String
Number of Characters
🎯 The number of characters to extract from the right side of the string (default is 1).

Supported formats:
  ✓ Number
  ✓ String
Input Setup
Input Function (optional)
⚙️ Optional pre-processing function applied to the input before internal logic (e.g., normalize case, trim whitespace). Internal transformations such as string validation will still apply afterward.
Result Handling
Output Function (optional)
⚙️ Optional function to apply to the result before returning it (e.g., str => str.toUpperCase(), val => val + ' extracted' for string modifications). Useful for chaining transformations on the output.
Source String string
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Number of Characters number
right()


Under the Hood

📜 View Implementation Code
/**
 * Returns a substring from the end of a specified string.
 * 
 * @param {string} data.src - The string from which to extract the right portion.
 * @param {number|string} data.num - The number of characters to extract from the right side of the string (default is 1).
 * @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 extraction.
 * 
 * @returns {string} The right portion of the string, or an empty string if num is 0.
 *
 * @framework ggLowCodeGTMKit
 */
const makeNumber = require('makeNumber');
const right = function(searchData, numCharacters) {
   const numChars = numCharacters !== undefined ? makeNumber(numCharacters) : 1;
   if (typeof searchData !== 'string' || typeof numChars !== 'number' || numChars <= 0) {
       return '';
   }
   return searchData.slice(-numChars);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// right - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(right(value, data.num));
// ===============================================================================
// right(...) – Apply Mode
// ===============================================================================
/*
return function(value, numCharacters) {
  numCharacters = data.rp1 ? data.num : numCharacters;
  return out(right(value, numCharacters));
};
*/
🧪 View Test Scenarios (9 tests)
✅ '[example] Extract last 5 characters'
✅ Default behavior (1 character when num is undefined)
✅ '[example] Count exceeds string length'
✅ Extract 0 characters (returns empty string)
✅ String number parameter
✅ Empty string input
✅ '[example] Non-string input'
✅ Negative number (returns empty string)
✅ Single character extraction