Collect Usage Data With A Simple Hook

As a plugin developer, one of your biggest hurdles with the WordPress ecosystem is that you don’t have a standard way of gathering metrics on how your plugin is used.

You can see how many people have downloaded your plugin, and how that download metric is trending.

But what does that get you? Not much.

You can see that people downloaded the plugin, but you don’t actually know if they installed it or if they installed and then immediately uninstalled it.

Recently, there’s been some debate around better plugin metrics.

Don’t wait for WordPress.org – Gather this information yourself

When you let your users opt-in to share their plugin usage with you:

  • You can get a sense of the environments your plugin is running in
  • You can gather information on why a user chooses to deactivate your plugin
  • You can analyze what features are used the most (or least)
  • You can provide your users a better overall experience

Stop guessing how you can improve your plugin, ask your users to show you.

Step 1: Create a setting for users to opt in

The first step in gathering metrics is creating a way for your users to opt in.

​Per the plugin handbook​:

In the interest of protecting user privacy, plugins may not contact external servers without explicit and authorized consent. This is commonly done via an ‘opt in’ method, requiring registration with a service or a checkbox within the plugin settings.

I’m not going to walk through this step because there are ​more detailed guides on creating settings for settings pages​.

Basically, you’ll want a section in your plugin settings where the user can control whether they are willing to share their usage with you. A checkbox is really all you need.

Here’s something you might say:

Hey {$user_display_name} 👋🏼

I’m so glad you are using my plugin!

If you opt-in to share your usage data, I’ll only gather some information on how you use my plugin so I can focus my efforts in the best areas.

{privacy policy link} and {terms of service link}

Then give them the option to “Allow” or deny with something like “skip” or “no thanks”.

Don’t skip this part or you could get your plugin kicked off the repository.

Step 2: Create an action

For this, the easiest method I’ve found is to create an action that triggers a request to a specific endpoint.

I like to think of this as the reverse of how actions are typically done. Instead of adding

do_action('your/namespace/event', $arg1, $arg2);

for other developers to extend functionality, you’ll handle the add_action() functionality first.

Here’s what that might look like in your plugin:

<?php
/**
 * Your Plugin.
 */

add_action( 'some/namespace/event', 'send_event', 10, 2 );

/**
 * Sends events to our data collection endpoint.
 *
 * @param string $name The name for the event.
 * @param array $data Additional info to include with the event.
 */
function send_event( $name, $data ) {

  // Don't send the event if the site administrator hasn't opted in.
  if ( false === get_option('opted_in') ) {
    return;
  }

  // Send post request to our data collection endpoint.
	wp_remote_post( $url, array(
    'blocking' => false, // Don't let this request halt execution.
    'body' => array(
      'name' => $name,
      'data' => $data
    ),
  ) );
}

Now, you place the do action anywhere in your plugin:

do_action( 'some/namespace/event', 'custom-event', array('some' => 'data') );

And your event will be sent off to wherever you need it sent.

This could be a service like Zapier, your own custom server, or any number of analytic services.

Step 3: Gather Data And Make Decisions

Once you implement something like this into your own plugin, all you have to do is prompt the user to opt-in to sharing their usage data.

Then you can trigger events for anything you might want information on.

Here are some ideas on things you might want:

  • Immediately after a user opts in, log an event with environment info (WP version, PHP version, etc.)
  • Log an event when a user adds a plugin block to a post or page
  • If a user doesn’t use a plugin feature for X days, log an event

In Summary

If you want more information on how your plugin is being used (or not being used), you can have complete control.

Ask your users if you can collect some usage information, and for those that opt-in you’ll have whatever information you have decided to capture.