Written by Giles Bennett
Following on from this post where I dealt with an initial set of custom push widgets to get information from Magento to Geckoboard, this post will deal with doing the same, but from Zendesk to Gecko.
As with Magento, Geckoboard comes with support for Zendesk out of the box, but also as with Magento, the support is limited to a few widgets when there is scope for so much more. Fortunately it's simple enough to accomplish pretty much whatever you want with a little bit of programming and a custom push widget or two.
Zendesk does have an API which is pretty good, but it's slightly limited in a number of respects. As an example, one of the widgets our client wanted was an overall customer satisfaction figure - but you can't get that one single figure through Zendesk's API, you can only get every single customer satisfaction response. And you can't get them all in one go, but can only query by the 100 - so on the surface of it the only way to pull out a figure would be to loop through every response in batches of 100, count up the good and the bad, then create a percentage at the end of it. Not ideal.
The saving grace, though, is that as well as being able to query the API, you can also query a custom view. So if you were to set up a view in your Zendesk account which showed the overall CSAT figure, then you could query that view, and get the figure that way.
Zendesk to Geckoboard
The first step is to set up the view in Zendesk that you want to query. In the example below I'm actually using two views, one which has the list of tickets closed in the last 24 hours (rolling), and one which has the list of tickets closed in the 24 hours before that (also rolling). Whatever it is you're querying, make a note of your custom Zendesk address and the query number. You'll also need to get the API token (I'm writing from memory here!) associated with the account that you'll be using.
So the first step is to open up a CURL session with the relevant details :
<?php
$curl = curl_init('https://CUSTOMADDRESS.zendesk.com/api/v2/views/VIEW_NUMBER/execute.json');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERPWD, 'USERNAME/token:API_TOKEN');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$responseToday = curl_exec($curl);
$resultsToday = json_decode($responseToday, true);
which then fills $resultsToday with the response from our Zendesk query. If we were to output the value of $resultsToday at this stage with a :
print_r($resultsToday);
then we'd see an array with, amongst other things, the details of the first 20 tickets closed today, with the ID, customer name, and the URL of the specific ticket for each.
Note that I say the first 20 - as with the CSAT statistics, the API doesn't return every value in one go - we would have to loop through the query to get the next 20 and the next 20. But we just want the total number of closed tickets, and this is returned at the bottom of the array like so :
[count] => 297
So that's the first bit of information we need - the total count of tickets closed in the last 24 hours, which is now contained in $resultsToday[count], and which can just sit there for the time being. Next we repeat the same step, but querying the second view (which contains) the number of tickets closed in the 24 hours before that :
$curl2 = curl_init('https://CUSTOMADDRESS.zendesk.com/api/v2/views/VIEW_NUMBER/execute.json');
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl2, CURLOPT_USERPWD, 'USERNAME/token:API_TOKEN');
curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1);
$responseYesterday = curl_exec($curl2);
$resultsYesterday = json_decode($responseYesterday, true);
And the count of closed tickets for the preceding 24 hours is contained in $resultsYesterday[count].
So then we just need to get that information in the format that Geckoboard wants to see it. In this instance we're dealing with a number and secondary stat widget - so if we just pass Geckoboard the number of tickets closed in the last 24 hours, and the number of tickets closed in the 24 hours before that, it will show the former figure as the main statistic, and then a comparison (% up or down) against the preceding 24 hour period.
So armed with $resultsToday[count], $resultsYesterday[count] and our Geckoboard API key, we can create the string that we need to pass to Geckoboard :
$values = '{"api_key":"YOUR_GB_API_KEY","data":{"item":[{"text":"","value":'.$resultsToday[count].'},{"text":"","value":'.$resultsYesterday[count].'}]}}';
And then the final step is to push that string to our custom push widget on Geckoboard, for which all we need is the Widget ID :
curl_setopt_array($ch = curl_init(), array(
CURLOPT_URL => "https://push.geckoboard.com/v1/send/WIDGET_ID_HERE",
CURLOPT_POSTFIELDS => $values,
)
);
curl_exec($ch);
curl_close($ch);
?>
Simply create a file with the code and then call the file through a cron job as regularly as you want (the hard work is being done by Zendesk, in this instance) and you'll have a widget which updates your stats on how many tickets are being closed. By extension, through either Zendesk's API or custom views, you can display pretty much any Zendesk information on your Geckoboard.