How to Track Post Page Views, User IDs, and Date Time in WordPress

Home » Snippets » How to Track Post Page Views, User IDs, and Date Time in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Ever wondered which posts are the most popular on your WordPress website? Tracking page views, visitor ID, date, and time can give you invaluable insights into your audience’s behavior and preferences. Understanding these aspects can help you create more engaging content tailored to audience needs. In this article, we’ll guide you through the process of tracking post page views, user IDs, dates, and time on your WordPress site.

					function wpturbo_track_post_views($post_id, $user_id) {
    $count_key = 'wpturbo_post_views_count';
    $count = get_post_meta($post_id, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    }else{
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }

    $user_views = array(
        'user_id' => $user_id,
        'view_count' => $count,
        'view_date' => current_time( 'mysql' )
    );

    update_post_meta($post_id, 'wpturbo_user_views', $user_views);
}
add_action( 'wp_head', 'wpturbo_track_post_views');
				

This PHP script defines a function named wpturbo_track_post_views(), which tracks and records the views of a post with the associated user’s id and index them by the date and time of the access.

function wpturbo_track_post_views($post_id, $user_id)

It takes two parameters: $post_id and $user_id. These parameters are used to access the specific post viewed by a specific logged-in user.

The $count_key variable is declared with a string ‘wpturbo_post_views_count’. This string acts as a meta-key for the post’s meta-data that tallies the post’s view count.

$count_key = 'wpturbo_post_views_count';

Using WordPress’s get_post_meta() function, the $count variable fetches the current count of the post’s views from the meta-data using the $count_key.

$count = get_post_meta($post_id, $count_key, true);

An IF condition checks if there’s no existing count for views in the post’s meta-data i.e. $count is an empty string. If true, it sets the count to ‘0’, deletes the previous meta-data (if any) for the view count, and adds a new one with ‘0’ views.

if($count==''){
    $count = 0;
    delete_post_meta($post_id, $count_key);
    add_post_meta($post_id, $count_key, '0');
} 

However, if the $count isn’t an empty string (i.e., views exist), the script increments the $count and updates the post’s meta-data with the new count.

else{
   $count++;
   update_post_meta($post_id, $count_key, $count);
}

Next, an associative array $user_views is created which holds the ID of the user who accessed the post, the count of views, and the date and time of this access.

$user_views = array(
    'user_id' => $user_id,
    'view_count' => $count,
    'view_date' => current_time( 'mysql' )
);

This array is then stored as a custom post meta-data with the meta-key ‘wpturbo_user_views’.

update_post_meta($post_id, 'wpturbo_user_views', $user_views);

The function is then hooked into WordPress’s wp_head action, which ensures this function gets called every time a post page’s header gets loaded, thereby initiating the tracking.

add_action( 'wp_head', 'wpturbo_track_post_views');

This way, every single post view gets systematically tracked and stored with the viewer’s ID and the exact date and time of the view.

Register an account to save your snippets or go Pro to get more features.