estimateReadingTime β GTM Variable Template for GTM
estimateReadingTime CORE GTM
Estimates the reading time of content based on word count.
When to Use This
Section titled βWhen to Use ThisβDate & Time
Calculate durations, differences, and time-based operations on date values.
Examples
Section titled β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
Live Sandbox
Section titled βLive Sandboxβ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
Section titled βRelated VariablesβSame category: GTM
Under the Hood
Section titled β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'