Move the Smiley Face of WordPress Stats

Why would you want to move the smiley face of WordPress stats from the footer to somewhere else? Good question with equally good answers.

Someone in a Facebook group that I’m a member of asked about the accuracy of WordPress stats. He said site visitor stats measured by Google Analytics and his web server show much higher visitor numbers than shown by WordPress stats.

Someone else suggested that a big discrepancy between WP Stats or Jetpack Stats and other analytics software might be caused by the beaming beacon of WP Stats — known as the smiley face — not loading on some pages. I thought that too. This made me wonder whether the WP Stats beacon could be moved. And it can.

So why might WP Stats not load?

The function that adds the stats beacon to web pages is attached to the site’s footer with <?php add_action( ‘wp_footer’, ‘stats_footer’, 101 ) ?> . If wp_footer() is not called by a theme then the tracking beacon will not be loaded.

Plugins installed into WordPress might prevent the WP Stats beacon from loading in the footer.

Web browsers might not be able to load some pages completely. This could be caused by a slow connection, DNS problems, browser plugins that are badly coded or by browser plugins that deliberately block trackers.

We can’t stop browser plugins from blocking WP Stats but we can move the stats beacon out of the page footer to somewhere the beacon will load even if the site footer doesn’t load.

How to move the WP Stats beacon

Add this little code snippet to your theme’s functions.php file:

<?php
if (function_exists('stats_footer')) {

    function vr_move_wp_stats($content) {
            $stats=stats_footer();
            $content = $stats.$content;
            return $content;
    }

    remove_action( 'wp_footer', 'stats_footer', 101 );
    add_filter('the_content','vr_move_wp_stats');
    
}
?>

The smiley face icon of WP Stats is loaded by the function stats_footer(). The code shown above unhooks stats_footer() from loading in wp_footer() then re-hooks the stats_footer() function into the page content filter known as “the_content()”.

I chose to hook into the_content() because it is available in all themes that display content. If you want to place the smiley somewhere else you could look for hooks in your theme and latch the stats_footer() function onto one of those theme specific hooks.

For example, in Genesis themes, we can hook directly into the genesis_header() hook to place the smiley icon into the header, like this:

<?php
if (function_exists('stats_footer')) {

    remove_action( 'wp_footer', 'stats_footer', 101 );
    add_action( 'genesis_header', 'stats_footer' );
    
}
?>

Want to know what WP Stats measures?

A little detour…

WordPress Stats records both visits and visitors but shows only visitor stats in the dashboard of self-hosted WordPress sites.

  • Visitor stats keep record of the first visits from unique identities logged in a day
  • Visits stats keep record of all visits no matter whether they are return visits or not. The number of visits will never be smaller than the number of visitors a site receives (unless working backwards, I guess)
  • Page view stats keep record of the number of pages viewed by all visitors. Page views will never be smaller than the number of visits.
  • Hits stats keep record of the number of individual requests for individual files. When a visitor views a page on your website, that visitor downloads the HTML doc, the CSS files, the scripts, the images and other media files. Each downloaded component is counted as one hit on your server. This is always the biggest stats number and is a totally useless statistic for most people.

So now you know 1, how to move the Jetpack Stats icon from the footer to someone else in your website, and, 2, what type of visit WP Stats actually records.

Reference

The raw code for the Jetpack Stats module can be read on GitHub at

https://github.com/Automattic/jetpack/blob/master/modules/stats.php

Sharing is caring!

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x