Skip to content

Instantly share code, notes, and snippets.

@pengelbrecht
Created November 4, 2013 11:57
Show Gist options
  • Save pengelbrecht/7301523 to your computer and use it in GitHub Desktop.
Save pengelbrecht/7301523 to your computer and use it in GitHub Desktop.
function getIncomingCallStats(from, to, number, userKey) {
var thisCall, open;
var callTime = new Date();
var fromstr = from.toISOString();
var tostr = to.toISOString();
var stats = new Object();
var missed = 0, answered = 0, voicemail = 0, answeredDuration = 0, closed = 0;
var options = {
"method" : "get",
"headers" : {"Accept" : "application/json", "Firmafon-User-Key": userKey}
};
var callLogParams = {
"number" : "45" + number,
"direction" : "incoming",
"from" : fromstr,
"to" : tostr,
"limit" : 999999
}
var URL = BuildURL(BASEURI+"/call_logs", callLogParams);
var response = Utilities.jsonParse(UrlFetchApp.fetch(URL, options).getContentText());
for (var i = 0; i < response.call_logs.length; i++) {
thisCall = response.call_logs[i];
if (thisCall.direction != "incoming") continue;
callTime = getDateFromISOString(thisCall.started_at);
open = isOpen(callTime, OPENTIME, CLOSETIME)
if (!open) {
closed++;
continue;
}
if (thisCall.status == "answered") {
answered++;
answeredDuration += thisCall.duration;
}
if (thisCall.status == "missed") missed++;
if (thisCall.status == "voicemail") voicemail++;
}
stats.missed = missed; stats.answered = answered; stats.voicemail = voicemail; stats.answeredDurationMins = answeredDuration/60; stats.closed = closed, stats.open = answered+missed+voicemail;
stats.avgDurationMins = (answeredDuration/60)/answered; stats.answerRate = answered/stats.open;
if (answered == 0) stats.avgDurationMins = 0;
if (stats.open == 0) stats.answerRate = 0;
return(stats);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment