Create a Dashboard for API Usage and Error Counts

October 22, 2015 | by

As mentioned in an earlier post, you can view API usage data for the prior week by going to the Admin > Web Services panel in Marketo.  This gives you a daily rollup of the number of calls made by user.  As a Marketo API consumer, this is useful information that you should keep an eye on.

What if you could get historical usage data to help detect trends over time?

What if you could get a summary of API error codes to help measure the health of your integration?

As a Marketo Technology Partner, what if you could get usage and error data across all of your customer accounts in one dashboard?

This post will provide an approach to answering the questions above.

Buckle up, here we go!

Diagram

Here is a high level diagram of the dashboard system.

Scheduled Job for Stats Retrieval

Let’s create an app that retrieves usage and error data using Get Daily Usage and Get Daily Errors endpoints.  The app is designed to be scheduled to run once per day.  Each time the app runs, it appends a day’s worth of usage data to one file, and a day’s worth of error data to another file.  At the beginning of each month, a new pair of files are created.  These files will serve as a historical record that we can access at any time.

Here is the app logic …

  • Read Marketo account information (Munchkin id and Client credentials) from an external source .  Note: This source must be secure to keep others from accessing account data.
  • Iterate through each account and…
    • Call Get Daily Usage to retrieve usage data for one day
    • Append daily usage data to a montly “usage” file
    • Call Get Daily Errors to retrieve errors data for one day
    • Append daily errors data to a monthly “errors” file

Output File Format

The format for the output files is JSON which matches up with the “result” array returned from the respective API calls (Usage and Error).  Each element of the “result” array is a JSON object that contains data for one day.

Output File Naming

The output files are named as follows:

<type>_<yyyy>_<mm>_<account>.json

where,

<type>  –  The type of data (“usage” or “errors”)
<yyyy> – The year (4-digits)
<mm> – The month (2-digits)
<account> – The account id (Munchkin id)

Output File Examples

usage_2015_10_111-AAA-222.json

errors_2015_10_111-AAA-222.json

Code for this app is presented at the end of this post (Stats.java).

Stats Web Service

So now we need a way to get this data into our browser.  The proposal is to create a web service to deliver the data.  The web service will consume the app’s output files and then return data to the browser in a form that can be readily presented.  For simplicity’s sake, and to get around same-origin policy restrictions, we will leverage the JSONP pattern.

Here is the proposed REST endpoint specification for the external web service:

URI: /stats

Method: GET

Parameter Description Example
month Retrieve data for this month.  Comma seperated list of months to include (2-digit representation).   Default to all months. 10,11
year Retrieve data for this year.  Comma seperated list of years to include  (4-digit representation).  Default to all years. 2015
account Retrieve data for this account (Munchkin id). 111-AAA-222
callback Name of function to wrap JSON content with. processStats

Example Request

GET //localhost:8080/stats?month=10&year=2015&account=111-AAA-222&callback=processStats

The web service reads “usage” and “error” files, and combines them together and returns them in this format:

<Name of Callback here>(

<Contents of Usage file here>,

<Contents of Error file here>

);

This is a JSONP callback with 2 arguments.  First argument is usage “result” array.  Second argument is errors “result” array.

Example Response

As you can see, the web service has simply wrapped the contents of the two output files from our app.  We have created a mock web service response using Mocky.  An example of the web service the mock is here.

Creation of this web service is left as an exercise for the reader 🙂

Dashboard Web Page

So now all we need is a web page that calls our web service and formats the data.

To use the JSONP pattern we just need to add a <script> tag that invokes the web service:

<script src=”http: //<hostname>/stats?month=10&year=2015&account=284-RPR-133&callback=processStats”></script>

This will inject the web service response body directly into the HTML page.

We then  add the JSONP callback function :

This function is automatically called after the web service call.  In this example, we call a simple JavaScript “variable dumper” called prettyPrint.js on each array.  The prettyPrint function simply produces an HTML table using the contents of the array.

Here is a screen shot of the HTML tables:

Voilà, that is our our dashboard!

Granted this isn’t very elegant, but it should give you an idea of what is possible.  There is nothing stopping you from transforming the data any way you like to make your own eye catching visualizations.

The HTML page is below (Index.html)

You can view the tables above live in your browser using the following steps:

  1. Save a local copy of Index.html
  2. Save a local copy of prettyPrint.js
  3. Open Index.html in your browser

So that’s it.  Hopefully this post has given you some ideas about monitoring your Marketo API stats.  Happy coding!

Stats.java

Index.html