Wednesday, January 30, 2013

Reporting Subscription Revenue with Pirate Metrics

If you’re like us, you use a service like Braintree, Stripe, or Chargify to handle your subscription billing (and Pirate Metrics to track your customer activity).  Your application may not be aware when someone is billed or, in some cases, exactly how much they have been charged.  If your application is not manually initiating billing, how can you accurately report revenue data to Pirate Metrics?

For us, the answer is with Braintree Webhooks.  Braintree will call back to your application any time a user’s account is successfully charged, and your application can pass that information along to Pirate Metrics.

PirateMetrics on PirateMetrics, it’s a pretty simple setup.  First of all, if you’re using Ruby, you should be using the Pirate Metrics Agent gem.  If you’re not, you’ll need to replace the PM calls in the example with whatever you use to communicate with the Pirate Metrics API.

Let’s look at some code:

In your production application, you will likely want to get the charge amount information from Braintree, but for testing purposes, we simply sent along the plan price.

Before it begins sending charge information to your site, Braintree will send a verification request, which is easy enough to deal with:

That’s all there is to it.  Now add a little bit of routing magic to tie it all together:

And you’re ready to report revenue using Braintree webhooks.  The process for other services like Stripe, is very similar.

Happy Pirating!