Plugin for tracking site traffic without Cookies, localStorage or sessionStorage.
Download here -> naswp-visitors.zip and upload in Plugin installations.
Plugin doesn't use cookies, session or local storage to identify user. It uses the browser cache, specifically $_SERVER['HTTP_IF_MODIFIED_SINCE']
, where it stores the generated value, so this plugin does not store any personal information. If the value isn't store in this server variable, the plugin increase visit counts and stores the information to the cache. So if the same user visits the same page again, the plugin will not increase the number of visits due to the information stored in the browser cache, of course until the browser cache is cleared.
Visits are tracked using AJAX script to prevent any problems caused by caching plugins. Additionaly, visit isn't tracked immediately, user has to either spend 6 seconds on the page or perform any interaction, such as:
- move with mouse
- scroll the page
- touch the screen on mobile devices
- hit any key on keyboard
This plugin is not a replacement for Google Analytics and similar tools. The measurements are not completely accurate, but from our perspective it is the best way to measure traffic without storing personal data and having to use a cookie bar.
By default, this plugin tracks visits of page
and post
post types and category
and post_tag
taxonomy terms. Visits are tracked only for anonymous users or users logged in with subscriber
role.
You can modify default configuration using filters. To do so, place this code to functions.php:
// Change tracked post types
add_filter( 'naswp_visitors_cpt', function( array $defaultTypes ) {
// Return an array of post type slugs
return [ 'page', 'post', 'movie' ];
} );
// Change tracked taxonomies
add_filter( 'naswp_visitors_tax', function( array $defaultTaxonomies ) {
// Return an array of taxonomy slugs
return [ 'category', 'post_tag', 'movie_genre' ];
} );
// Change tracked user roles (anonymous users are always tracked)
add_filter( 'naswp_visitors_roles', function( array $defaultRoles ) {
// Return an array of roles
return [ 'subscriber', 'admin' ];
} );
Visitor counts are stored in post meta / term meta named by default as follows:
naswp_visitors_total
for total count of visitsnaswp_visitors_yearly
for visits in the past 12 monthsnaswp_visitors_monthly
for visits in the past 30 daysnaswp_visitors_daily
for visits in the past 24 hours
Meta values are recalculated whenever a visitor comes to a page or term and either stays for at least 6 seconds or perform any interaction (mouse move, click, touch, scroll). That's why the values in post meta might not be accurate - they are only relevant at the time of the last visit. For example, if there isn't any visit in current month, meta value will still hold counts relevant for previous month, because there wasn't any chance to recalculate it.
For this reason, use following code to get appropriate visit counts. Following code get the post meta value and additionaly checks it's relevance.
$visitors = new NasWP_Visitors_Post( 1 ); // For post with ID = 1
$visitors = new NasWP_Visitors_Term( 1 ); // For term with term_id = 1
// For past 24 hours
echo $visitors->get_daily();
// For past 30 days
echo $visitors->get_monthly();
// For past 12 months
echo $visitors->get_yearly();
// Total count
echo $visitors->get_total();
To get sum of all visits for all posts or terms, use static methods as follows:
// Total sum of all posts
echo NasWP_Visitors_Post::get_total_sum();
// Total sum of all pages
echo NasWP_Visitors_Post::get_total_sum( [ 'page' ] );
// Total sum of all terms
echo NasWP_Visitors_Term::get_total_sum();
// Total sum of all tags
echo NasWP_Visitors_Term::get_total_sum( [ 'post_tag' ] );
Plugin adds the ability to order WP query and Term query by number of visits in required time interval. Use one of the following constants as orderby value in query arguments:
NASWP_VISITORS_TOTAL
for order by total number of visitsNASWP_VISITORS_YEARLY
for order by visits in the past 12 monthsNASWP_VISITORS_MONTHLY
for order by visits in the past 30 daysNASWP_VISITORS_DAILY
for order by visits in the past 24 hours
For example:
// For posts, same for query_posts()
$posts = get_posts( [
'orderby' => NASWP_VISITORS_TOTAL,
'order' => 'DESC',
// ...
] );
// For taxonomy terms
$terms = get_terms( [
'orderby' => NASWP_VISITORS_MONTHLY,
'order' => 'DESC',
// ...
] );
Order takes into account the time-validation of visitor count as mentioned above.
There are two limitations:
- You can only use one orderby argument. Passing orderby as array won't work.
- You can't suppress filters in the query using
'suppress_filters'
argument.
- Best practise update: replacing our AJAX entry with WP admin-ajax.php
- Security fix: Full Path Disclosure
- First plugin release with filters for change CPT, TAX and Roles.