Tracking Word Count in WordPress and GA4 to Unlock Better Engagement Insights

Marketers talk a great deal about engagement, but rarely do they tie it back to one of the most fundamental elements of content performance: length. Word count influences how deeply readers consume an article, how likely they are to scroll, how much time they spend on the page, and whether they convert. Long-form content often supports search visibility and authority, while shorter formats can encourage faster consumption and broader shareability. Without storing a consistent, accurate word count for every article, though, those patterns are impossible to measure, especially inside GA4.
WordPress itself displays word count in the block editor, but that value is calculated on demand. It disappears the moment you close the editor and is never stored in the database. This means you can’t run reports on which content lengths contribute to engagement time, scroll depth, or conversions. The only way to analyze the relationship between length and performance is to calculate word count server-side, save it to postmeta, and pass it to GA4 as a custom event parameter.
The following plugin does exactly that. It cleans and normalizes every post’s content on save, strips non-visible elements such as HTML comments and shortcodes, derives an accurate reader-facing word count, stores it in postmeta, and pushes the value to GA4 on single-post views. Once configured in GA4, you can finally examine how length impacts user engagement across your content library.
Word Count Tracking Plugin for WordPress (Full Code)
<?php
/**
* Plugin Name: WP Clean Word Count + GA4 Integration
* Description: Stores a clean word count for posts on save and sends it to GA4 as an event parameter.
* Version: 1.0.0
* Author: Douglas Karr
* Author URl: https://dknewmedia.com
*/
/**
* Generate a cleaned, human-visible word count.
*/
function wpccwc_get_clean_word_count( $content ) {
// Remove HTML comments
$content = preg_replace( '/<!--(.|\s)*?-->/', ' ', $content );
// Remove shortcodes
$content = strip_shortcodes( $content );
// Remove all HTML tags (anchor tags included)
$content = wp_strip_all_tags( $content );
// Normalize whitespace
$content = trim( preg_replace( '/\s+/', ' ', $content ) );
if ( empty( $content ) ) {
return 0;
}
// Count visible words
return str_word_count( $content );
}
/**
* Store the cleaned word count whenever a post is saved or updated.
*/
add_action( 'save_post', 'wpccwc_store_word_count', 20, 3 );
function wpccwc_store_word_count( $post_id, $post, $update ) {
// Prevent autosave or revisions
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}
if ( $post->post_type !== 'post' ) {
return;
}
$word_count = wpccwc_get_clean_word_count( $post->post_content );
update_post_meta( $post_id, '_clean_word_count', $word_count );
}
/**
* Send the stored word count to GA4 on single post views.
*/
add_action( 'wp_footer', 'wpccwc_send_wordcount_to_ga4' );
function wpccwc_send_wordcount_to_ga4() {
if ( ! is_singular( 'post' ) ) {
return;
}
global $post;
$word_count = get_post_meta( $post->ID, '_clean_word_count', true );
$word_count = $word_count ? intval( $word_count ) : 0;
?>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){ dataLayer.push(arguments); }
gtag('event', 'post_view', {
post_id: '<?php echo intval( $post->ID ); ?>',
post_title: '<?php echo esc_js( get_the_title( $post ) ); ?>',
word_count: <?php echo $word_count; ?>
});
</script>
<?php
} How the Word Count Plugin Works
The plugin handles three responsibilities: cleaning the content, storing the word count, and sending it to GA4.
Cleaning and Counting Words
WordPress content often contains shortcodes, HTML structure, comments, and formatting that normal readers never see. Counting these elements would inflate the true length of an article and distort engagement metrics. The plugin solves this by removing non-visible elements before counting words. It strips HTML comments, removes all shortcodes, eliminates anchor tags and other HTML, normalizes whitespace, and returns only the words visible to a reader. This produces the standard, widely accepted definition of word count used in writing and SEO tools.
Storing the Word Count
The cleanup function runs every time a post is saved or updated. WordPress does not store word counts natively, so the plugin adds a _clean_word_count postmeta entry. This ensures the value can be reused without recalculating it and becomes available for reporting, querying, or use within templates.
Sending Word Count to GA4
On single-post views, the plugin retrieves the stored word count and outputs a GA4 event in the footer. The event is named post_view and includes three parameters: the post ID, the post title, and the cleaned word count. GA4 can then ingest this metadata alongside engagement time, scroll depth, conversions, and other user behaviors.
Setting Up Google Analytics to Capture Word Count
Once the plugin is active and your posts have been saved, so each has a stored count, you can configure GA4 to recognize and report on the parameter.
- Verify the Event in DebugView: Open your site, navigate to any single post, and inspect GA4’s DebugView. You should see the post_view event appear with word_count included. This confirms that the parameter is being pushed correctly.
- Create the Custom Dimension: GA4 requires a registered custom dimension for event parameters to appear in reports.
- Open Admin > Custom Definitions.
- Choose Create custom dimension.
- Name it Word Count.
- Set Scope to Event.
- Enter the parameter name exactly as
word_count.
- Save the definition.
GA4 will begin populating this dimension in your reports within about a day.
Build Reports and Comparisons
With the dimension active, you can start measuring how length affects performance. You can group posts by word count ranges, compare long-form vs. short-form pieces, or examine correlations between length and engagement time. You can also combine the dimension with scroll-depth events or conversion data to understand whether longer content drives more meaningful actions.
MySQL Query for Word Count
An older version of this post provided a MySQL query to get your word counts by post. If you want to get a quick query of your WordPress Posts and their Word Count, you could also just run a query using PHPMyAdmin or your favorite query engine.
SELECT
ID,
post_title,
(
LENGTH(
TRIM(
REGEXP_REPLACE(
REGEXP_REPLACE(
REGEXP_REPLACE(post_content, '<!--(.|\n)*?-->', ' '),
'<[^>]+>', ' '),
'[[:space:]]+', ' ')
)
)
- LENGTH(
REPLACE(
TRIM(
REGEXP_REPLACE(
REGEXP_REPLACE(
REGEXP_REPLACE(post_content, '<!--(.|\n)*?-->', ' '),
'<[^>]+>', ' '),
'[[:space:]]+', ' ')
),
' ', '')
)
+ 1
) AS word_count
FROM wp_posts
WHERE post_type = 'post'
AND post_status = 'publish'; 






