Word Count as a data layer variable
How can I use the word count feature of Admin Columns as a data layer variable to measure my website engagement through Google Analytics and Google Tag Manager? What variable (name) should I use?
That won’t be possible. Admin Columns is an admin tool, it won’t work on the frontend. The best option would be to write a little code snippet yourself.
The way we calculate the word count can be found in this method ac_helper()->string->word_count()
.
Thanks Tobias. I’m not really good with php. I found this solution but the word count is different from Admin Columns (correct) and the solution below. (my default language is pt-BR)
function word_range() {
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
if ($word_count < 200) { $word_range = '0-200';}
elseif($word_count < 400) { $word_range = '200-400'; }
elseif($word_count < 600) { $word_range = '400-600'; }
elseif($word_count < 800) { $word_range = '600-800'; }
elseif($word_count < 1000) { $word_range = '800-1000'; }
elseif($word_count < 1200) { $word_range = '1000-1200'; }
elseif($word_count < 1400) { $word_range = '1200-1400'; }
elseif($word_count < 1600) { $word_range = '1400-1600'; }
elseif($word_count < 1800) { $word_range = '1600-1800'; }
elseif($word_count < 2000) { $word_range = '1800-2000'; }
elseif($word_count < 2200) { $word_range = '2000-2200'; }
elseif($word_count < 2400) { $word_range = '2200-2400'; }
elseif($word_count < 2600) { $word_range = '2400-2600'; }
elseif($word_count < 2800) { $word_range = '2600-2800'; }
elseif($word_count < 3000) { $word_range = '2800-3000'; }
elseif($word_count < 3200) { $word_range = '3000-3200'; }
elseif($word_count < 3400) { $word_range = '3200-3400'; }
elseif($word_count < 3600) { $word_range = '3400-3600'; }
elseif($word_count < 3800) { $word_range = '3600-3800'; }
elseif($word_count < 4000) { $word_range = '3800-4000'; }
elseif($word_count >= 4000) { $word_range = '4000+'; }
return $word_range;
}
function word_count() {
$content = get_post_field( 'post_content', $post->ID );
$word_count = str_word_count( strip_tags( $content ) );
return $word_count;
}
function tag_manager_variables() {
$url = 'http://'. $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
echo "
<script>
dataLayer = [{
'wordCount': '". word_count() ."',
'wordRange': '". word_range() ."'
}];
</script>
";
}
add_action ( 'wp_head', 'tag_manager_variables' );
For example. In text “Qualquer um que tenha navegado no Google Analytics já se deparou com um apanhado de variáveis nos relatórios.” WordPress (and Admin Columns) tell me that are 18 words (correct). But the script above shows me 20. If I use “Qualquer um que tenha navegado no Google Analytics ja se deparou com um apanhado de variaveis nos relatorios.) then the script tells me 18 words too.
Wordcount outside the scope of our plugin is not something we can support really, but this is the code we are using:
<?php
class Strings {
/**
* @since 1.3
*
* @param $string
*
* @return string
*/
public function strip_trim( $string ) {
return trim( strip_tags( $string ) );
}
/**
* Count the number of words in a string (multibyte-compatible)
* @since 3.0
*
* @param $string
*
* @return int Number of words
*/
public function word_count( $string ) {
if ( empty( $string ) ) {
return false;
}
$string = $this->strip_trim( $string );
if ( empty( $string ) ) {
return false;
}
$patterns = array(
'strip' => '/<[a-zA-Z\/][^<>]*>/',
'clean' => '/[0-9.(),;:!?%#$¿\'"_+=\\/-]+/',
'w' => '/\S\s+/',
'c' => '/\S/',
);
$string = preg_replace( $patterns['strip'], ' ', $string );
$string = preg_replace( '/ | /i', ' ', $string );
$string = preg_replace( $patterns['clean'], '', $string );
if ( ! strlen( preg_replace( '/\s/', '', $string ) ) ) {
return 0;
}
return preg_match_all( $patterns['w'], $string, $matches ) + 1;
}
}
This is a snippet from our string class. I think the fact that we support multibyte characters (things with like á and ē) is what differs from using str_word_count() function in PHP.
With some PHP language, you should be able to get that snippet working.
Hope this helps you, as this is about as far as we can go in support as it is not really related to our product…
Cheers and good luck!
You must be logged in to reply to this topic.