countWordsInString — GTM Variable Template for String
countWordsInString CORE String
Counts the number of non-empty words in a given string. Note: text should be sanitized first to remove HTML tags and normalize whitespace for accurate word counting.
When to Use This
String Manipulation
Transform, clean, and normalize text data for consistent downstream processing.
Filtering
Select or exclude items from collections based on criteria or predicates.
Examples
Simple sentence
INPUT
Sanitized Text: hello world test
OUTPUT
3
Multiple spaces between words
INPUT
Sanitized Text: hello world test
OUTPUT
3
Non-string input returns undefined
INPUT
Sanitized Text: 12345
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
countWordsInString
Sanitized Text
💾 The sanitized text string to analyze for word counting.
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.
Sanitized Text 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.
countWordsInString()
Related Variables
Same category: String
Under the Hood
📜 View Implementation Code
/**
* Counts the number of non-empty words in a given string. Note: Text should be sanitized first to remove HTML tags and normalize whitespace for accurate word counting.
*
* @param {string} data.src - The sanitized text string to analyze.
* @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 counting.
*
* @returns {number|undefined} The number of words in the string, or undefined if input is not a string.
*
* @framework ggLowCodeGTMKit
*/
const countWordsInString = function(sanitizedText) {
if (typeof sanitizedText !== 'string') { return undefined; }
return sanitizedText.split(' ').filter(word => word.length > 0).length;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// countWordsInString - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(countWordsInString(value));
// ===============================================================================
// countWordsInString() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(countWordsInString(value));
};
*/🧪 View Test Scenarios (8 tests)
✅ '[example] Simple sentence'
✅ String with single word - should return 1
✅ Empty string - should return 0
✅ String with only spaces - should return 0
✅ '[example] Multiple spaces between words'
✅ String with leading and trailing spaces - should return 2
✅ '[example] Non-string input returns undefined'
✅ Non-string input (array) - should return undefined