Track Your Application Usage

Tracking your application usage

When you release either commercial or free software its useful to be able to track your application usage by your customers to advise of updates or changes.

I wrote this WordPress plugin to log an entry each time the software is opened.

When you save the plugin to your wordpress plugin folder and active it you can create a page as shown below and use the shortcode [insert-data]. This will take the data from the url and save it to the database

When the url is called it saved the data to the DB. From your admin Panel you can see the activity under Tracker Entries.

Tracking your application usage
Access VBA Function

From Access I use this function to silently call the url and track your application usage

Function LogUsage()
    On Error Resume Next ' IF THE USER ISNT CONNECTED TO THE WEB JUST IGNORE THIS
    'GPDR - dont collect data if use opts out
    'Track your application usage
    If GetPref("Allow Usage Tracking") <> "Yes" Then
        Exit Function
    End If
    Dim http As Object
    Dim url As String
    Dim response As String

    ' Specify the URL you want to call
    url = "https://anythingaccess.com/dbtrack/?user=Lotto : " & GetPref("Club Name")

    ' Create the XMLHTTP object
    Set http = CreateObject("MSXML2.XMLHTTP")

    ' Open a GET request
    http.Open "GET", url, False

    ' Send the request
    http.Send

    ' Capture the response (optional)
    response = http.responseText

    ' Log the response to the Immediate Window (Debug Window)
    Debug.Print response

    ' Clean up
    Set http = Nothing
End Function

WordPress Plugin to Track your Application Usage
<?php
/**
 * Plugin Name: Access DB Usage Tracking
 * Description: A plugin to track your application usage.
 * Version: 1.0
 * Author: AnythingAccess.com
 */

// Function to create the `accdb_tracker` table
function create_accdb_tracker_table() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'accdb_tracker';
    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        DB_User varchar(255) NOT NULL,
        DB_Date datetime NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
}

// Hook to create the table during plugin activation
register_activation_hook(__FILE__, 'create_accdb_tracker_table');

// Function to insert data into the `accdb_tracker` table
function insert_into_accdb_tracker($user_name) {
    global $wpdb;

    $table_name = $wpdb->prefix . 'accdb_tracker';
    $data = [
        'DB_User' => $user_name,
        'DB_Date' => current_time('mysql')
    ];

    $result = $wpdb->insert($table_name, $data);

    if ($result === false) {
        error_log('Database insert failed: ' . $wpdb->last_error);
        return 'Database insert failed!';
    } else {
        return 'Data inserted successfully!';
    }
}

// Shortcode to insert data via URL query string
function insert_data_from_url() {
    // Get the user data from the URL query string
    if (isset($_GET['user'])) {
        $user_name = sanitize_text_field($_GET['user']);
        return insert_into_accdb_tracker($user_name);
    }
    return 'No user provided!';
}
add_shortcode('insert_data', 'insert_data_from_url');

// Add Admin Menu Page
function accdb_tracker_admin_menu() {
    add_menu_page(
        'Tracker Entries',            // Page title
        'Tracker Entries',            // Menu title
        'manage_options',           // Capability
        'Tracker-entries',            // Menu slug
        'accdb_tracker_display_page', // Callback function
        'dashicons-database',       // Icon
        20                          // Position
    );
}
add_action('admin_menu', 'accdb_tracker_admin_menu');

// Display Entries on the Admin Page
function accdb_tracker_display_page() {
    global $wpdb;

    $table_name = $wpdb->prefix . 'accdb_tracker';
    $results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY id DESC");

    echo '<div class="wrap">';
    echo '<h1>Tracker Entries</h1>';

    if (!empty($results)) {
        echo '<table class="widefat fixed striped">';
        echo '<thead><tr><th>ID</th><th>User</th><th>Date</th></tr></thead>';
        echo '<tbody>';
        foreach ($results as $row) {
            echo '<tr>';
            echo '<td>' . esc_html($row->id) . '</td>';
            echo '<td>' . esc_html($row->DB_User) . '</td>';
            echo '<td>' . esc_html($row->DB_Date) . '</td>';
            echo '</tr>';
        }
        echo '</tbody>';
        echo '</table>';
    } else {
        echo '<p>No entries found.</p>';
    }

    echo '</div>';
}

Check out our other wordpress posts

Tracking Desktop Analytics

, ,

Leave a Reply

Your email address will not be published. Required fields are marked *