Let me clear this straight, this post isn’t about to create WordPress plugin, but how to track your plugin users (blogs).
I have recently published my first plugin on WordPress’ repository at http://wordpress.org/extend/plugins/tweet-my-post/ and I felt really happy to see the downloads. However, what I was missing was, who are using my plugin. I am not sure if tracking the plugin users prohibited or not by WordPress but I am just tracking the url of blogs, that uses my plugin.
Let’s get back to the point – how to track our plugin’s users. This is a simple thing, but main thing is, this should strike your mind. I am posting this for those who haven’t thought (or not able make this happen). Useful also if you’re planning to make one too.
Plugin has a plugin_activation and plugin_deactivation hook, that means, you can register your functions that are executed every time, your plugin is activated and deactivated.
You need to add following code in your plugin file:
As I mentioned earlier, I have added log_operation($op) in the functions that are called while activation and deactivation. $op is operation, either activate or deactivate. Use appropriate operation in functions. Then whenever plugin is activated or deactivated, log_operation will be called. That function will create a cURL session and post the data of blog url and activation status to the page on our server. Now, what you need to do is to put following code in a page, let’s call it service.php, which is hosted at your server. What it will do is, add the status in our database. You need to put link to this page in above code as a value of $url variable.
The code is relatively simple to understand so I’m not explaining it at all. Just replace the database name and user details with yours. You will now need the database. Use following table:
Well, you are not ready to see, who are using your plugin. You can look into your database table and enjoy!
If you’re lazy to open PhpMyAdmin and browse the database, simply make a file which fetches the data and display it.
I have not added this in my initial release of plugin, so I can only track the users who are using it after Tweet My Post 1.0. Actually, I should be able to track old users too, because WordPress first deactivate plugin, update it and the reactivate it, so I should have the data of old users who have update the plugin but WordPress doesn’t execute activation and deactivation hook while updating plugin. I’m not sure if this is a bug or intentional, but I can’t know my old users unless they deactivate and reactivate the plugin.
Anyways, if you have any problem or query, please let me know in comments below or ping me @ksg91 on Twitter.