Tracking usage of your WordPress plugins.

Analytics data are an important success factor for any plugin. Understanding how people are using your plugin is essential. Unfortunately WordPress does not provide an easy way to track usage statistics of plugins, this is where my library comes to the rescue. Thanks to the integration with Keen.io you can now start tracking data from your own plugins.

This is a tutorial aimed at plugin developers.

There are many different services out there providing a developers friendly api to track analytics. I've picked Keen.io because it has an SDK to build my own dashboard for displaying collected data. This tutorial will only show you how to track data, you'll then need to build the dashboard yourself.

How does it work?

The WP Plugin Usage Tracker library, will display an admin notice asking permission to start tracking data. If the user approves, collected data will be sent to your own Keen.io account once a month via WP Cron.

I personally hate bloating the admin panel with too many admin notices, so the library will display the notice after a certain amount of days from the plugin's installation date.

Requirements:

Usage:

Open up a terminal and run composer install to install the required libraries.

Initialize the class
$tracker = new My_Tracker(
  'plugin-prefix',
  'Name of the plugin',
  '16 July 2016',
  '10',
  'project id goes here',
  'long write key goes here'
);

$tracker->init();

You're done. The above configuration is saying to display the notice 10 days after the installation date being "16 July 2016". Your own plugin must somehow store the installation date somewhere (perhaps an option on plugin activation).

All parameters are described here.

Data customization:

It is recommended that you extend the WP_Plugin_Usage_Tracker class so you can customize the admin message and the data collected. By default the class doesn't collect any sensitive data.

Customization example
<?php
class My_Tracker extends WP_Plugin_Usage_Tracker {

  public function __construct( $plugin_prefix, $plugin_name, $installation_date, $days_passed, $project_id, $write_key ) {

    parent::__construct( $plugin_prefix, $plugin_name, $installation_date, $days_passed, $project_id, $write_key );

  }

  public function get_message() {

        $message = esc_html__( 'My own message ;) ' );
        $message .= ' <a href="'. esc_url( $this->get_tracking_approval_url() ) .'" class="button-primary">'. esc_html( 'Allow tracking' ) .'</a>';

        return $message;

    }

  public function get_data() {

        $data = array();

        $data['php_version']    = phpversion();
        $data['wp_version']     = get_bloginfo( 'version' );
        $data['server']         = isset( $_SERVER['SERVER_SOFTWARE'] ) ? $_SERVER['SERVER_SOFTWARE']: '';

        return $data;

    }

}

The class already provides methods to track:

If needed you can also change the event schedule from once a month to what you prefer.

If you've done everything correctly, you'll see your data into your Keen.io account ready to be manipulated.

Hope you'll find this library useful, if you have any suggestions, feel free to post them on Github.