How do I track unique visitors in WordPress Website ?

To track unique visitors on a WordPress site is easy and various method like using Google Analytics, and some other plugins. In this tutorial we learn about plugin creation to track unique visitors for a WordPress site. We will discuss about plugin creation steps and also cover all coding steps and their detailed explanation.

Configure Plugin folder/directory

To create a plugin navigate to the ‘wp-content/plugins/’ folder and proceed for below steps.

  • Create a folder name called unique-visitor or any name.
  • Create a readme file and add complete description about the plugin and its procedure.
  • Create a php file with any name.
plugin directory configuration steps.

Define Plugin Header information

Defining plugin header information is a key role for plugin development. It contains information about plugin mainly name, description, Author, Author URI. To add header information navigate to the unique-visitor.php and paste below code into file.

<?php
/*
Plugin Name: Unique Visitor
Description: Tracks unique visitors when website is load.
Version: 1.0
Author: Abhishek
*/
?>

Hooking into WordPress

To keep an eye out for your one-of-a-kind visitors, you need to deploy the code on every page load. The wp action hook is definitely a good choice because it is run before the content is sent to the browser, thus it is ideal for tracking purposes.

Here’s how you can hook into WordPress:

register_activation_hook(FILE, 'uvc_create_table');

We are using above hook to activate plugin in WordPress dashboard. Using above hook we can activate the plugin.

plugin activate

In next step we will create SQL database query when to store the records of all visitors details to monitor the data.

Here is the SQL Query to with plugin function name. It will create column into database once plugin is activate.

function uvc_create_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'unique_visitors';
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table_name (
        id INT NOT NULL AUTO_INCREMENT,
        ip_address VARCHAR(45) NOT NULL,
        visit_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        UNIQUE KEY ip_address (ip_address)
    ) $charset_collate;";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

In next step we will use add_action() hook and $_SERVER[‘REMOTE_ADDR’] to get ip address of a visitor also we will write SQL query to check whether data is exists in our record or not. After that we will perform conditional check with SQL to insert records into database.

add_action('wp_footer', 'uvc_track_visitor');

function uvc_track_visitor() {

    if (!is_admin()) {

        global $wpdb;

        $table_name = $wpdb->prefix . 'unique_visitors';

        $ip_address = $_SERVER['REMOTE_ADDR'];

        // Check if the IP address already exists

        $existing_ip = $wpdb->get_var($wpdb->prepare("SELECT ip_address FROM $table_name WHERE ip_address = %s", $ip_address));

        if ($existing_ip === null) {
            // Insert new IP address
            $wpdb->insert($table_name, array('ip_address' => $ip_address));
        }

    }

}

In the final steps we will use add_shortcode() hook to display unique visitor data into our wordpress website. Using shortcode we can add anywhere in the wordpress editor. Below code will return count with $count variable.

add_shortcode('unique_visitor_count', 'uvc_display_count');
function uvc_display_count() {

    global $wpdb;

    $table_name = $wpdb->prefix . 'unique_visitors';

    $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");

    return $count;

}

Use the given shortcode [‘unique_visitor_count’] into wp editor anywhere to display data on WordPress website. We can also use shortcode into footer file with HTML code for better look and customize it accordingly.

<div class="uvisitor"><?php echo do_shortcode('3182'); ?></div>

We can customize plugin as per requirement and check footer section for working plugin.

<?php
/*
Plugin Name: Unique Visitor
Description: Tracks unique visitors by IP address.
Version: 1.0
Author: Abhishek
*/

// Hook to activate plugin and create table

register_activation_hook(__FILE__, 'uvc_create_table');

function uvc_create_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'unique_visitors';
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table_name (
        id INT NOT NULL AUTO_INCREMENT,
        ip_address VARCHAR(45) NOT NULL,
        visit_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY (id),
        UNIQUE KEY ip_address (ip_address)
    ) $charset_collate;";
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}


// Hook to track unique visitors

add_action('wp_footer', 'uvc_track_visitor');

function uvc_track_visitor() {

    if (!is_admin()) {

        global $wpdb;

        $table_name = $wpdb->prefix . 'unique_visitors';

        $ip_address = $_SERVER['REMOTE_ADDR'];

        // Check if the IP address already exists

        $existing_ip = $wpdb->get_var($wpdb->prepare("SELECT ip_address FROM $table_name WHERE ip_address = %s", $ip_address));

        if ($existing_ip === null) {
            // Insert new IP address
            $wpdb->insert($table_name, array('ip_address' => $ip_address));
        }

    }

}

// Shortcode to display unique visitor count

add_shortcode('unique_visitor_count', 'uvc_display_count');
function uvc_display_count() {

    global $wpdb;

    $table_name = $wpdb->prefix . 'unique_visitors';

    $count = $wpdb->get_var("SELECT COUNT(*) FROM $table_name");

    return $count;

}

We are sharing complete code just follow above steps and paste into php file save it and activate the plugin.