Here is the PHP code for the WordPress Function.php file to capture the dataLayer information such as Date, Author, Category, and Word Count. This script only works for Single.php (Post Page).
//Google Universal Analytics data layer
function google_analytics() { ?>
<?php if (is_single()) : ?>
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'gtm.publishedDate': '<?php the_date('M j, Y'); ?>',
'gtm.modifiedDate': '<?php the_modified_date('M j, Y'); ?>',
'gtm.postAuthor': '<?php the_author(); ?>',
'gtm.postCategory': '<?php foreach((get_the_category()) as $category) { $catname =$category->cat_name; echo $catname;} ?>',
'gtm.postWordCount': '<?php $data = get_post_field( 'post_content', $post->ID );
$wordCount = str_word_count(strip_tags($data)); echo $wordCount; ?>'
});
</script>
<?php endif; ?>
<?php }
add_action ('wp_head', 'google_analytics');
Add this script in function.php
file in WordPress active theme.