estimateReadingTime — GTM Variable Template for GTM
estimateReadingTime CORE GTM
Estimates the reading time of content based on word count.
When to Use This
Date & Time
Calculate durations, differences, and time-based operations on date values.
Examples
Default reading time
INPUT
Word Count: 700
Words Per Minute: undefined
Minimum Seconds: undefined
Words Per Minute: undefined
Minimum Seconds: undefined
OUTPUT
120
Invalid input returns undefined
INPUT
Word Count: not a number
Words Per Minute: undefined
Minimum Seconds: undefined
Words Per Minute: undefined
Minimum Seconds: undefined
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
estimateReadingTime
Word Count
💾 The total number of words to base the estimate on.
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ String
Words Per Minute
💾 Average reading speed in words per minute (default: 350).
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ String
Minimum Seconds
💾 Minimum number of seconds to return (default: 30).
Supported formats:
✓ Number
✓ String
Supported formats:
✓ Number
✓ 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.
Word Count number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
Words Per Minute number
Minimum Seconds number
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
estimateReadingTime()
Related Variables
Same category: GTM
Under the Hood
📜 View Implementation Code
/**
* Estimates reading time in seconds based on word count.
*
* @param {number} data.src - The total number of words to base the estimate on.
* @param {number|string} data.wpm - Average reading speed in words per minute (default: 350).
* @param {number|string} data.min - Minimum number of seconds to return (default: 30).
* @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 calculation.
*
* @returns {number|undefined} Estimated reading time in seconds, or undefined if word count is invalid.
* @author Markus Baersch
* @see https://bit.ly/4katPuD
* @modified Adaptation to the library framework
*
* @framework ggLowCodeGTMKit
*/
const Math = require('Math');
const makeNumber = require('makeNumber');
const estimateReadingTime = function(wordCount, wordsPerMinute, minSecs) {
const words = makeNumber(wordCount);
if (words !== words) { return undefined; }
const wpmNum = makeNumber(wordsPerMinute);
const wpm = (wpmNum === wpmNum && wordsPerMinute !== undefined) ? wpmNum : 350;
const minSecsNum = makeNumber(minSecs);
const minSeconds = (minSecsNum === minSecsNum && minSecs !== undefined) ? minSecsNum : 30;
const timeInSeconds = Math.round((words / wpm) * 60);
return Math.max(timeInSeconds, minSeconds);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// estimateReadingTime - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(estimateReadingTime(value, data.wpm, data.min));
// ===============================================================================
// estimateReadingTime(...) – Apply Mode
// ===============================================================================
/*
return function(value, wordsPerMinute, minSecs) {
return out(estimateReadingTime(value, data.wpm , data.min));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Default reading time'
✅ Valid word count with custom words per minute - calculates correctly
✅ Low word count below minimum - returns minimum seconds
✅ Valid word count with custom minimum - respects custom minimum
✅ '[example] Invalid input returns undefined'