Tracking call details with Bandwidth


https://gist.github.com/dtolb/d2b5268ed0554ce893bf
Keeping call analytics with Bandwidth is incredibly easy.
The business of tracking where an incoming sales call originated historically been an incredibly pricey (but necessary) add on. In fact, if you have used Google on your smart phone to call any business in the past years, your call was probably tracked.
Call tracking helps businesses know what ads and searches are effective, pretty straightforward stuff.

Bandwidth XML (BXML) Basics
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<SpeakSentence gender="male" locale="en_US" voice="paul">Thank you for calling Tom's Tire Shop</SpeakSentence>
<Transfer transferCallerId="+19198281234">
<PhoneNumber>+19198281235</PhoneNumber>
<PhoneNumber>+19198281236</PhoneNumber>
<PhoneNumber>sip:[email protected]</PhoneNumber>
</Transfer>
</Response>
Bandwidth XML (BXML for short) makes call control incredibly simple. The above example does a deceivingly large amount of work for us.
- Speaks the sentence specified to the original caller.
- Creates an outbound call with the specified callerId to each phone number specified in the <PhoneNumber> tag as well as the SIP address.
- When the first phone number or SIP Phone (can be softphone or SIP Phone) answers it will connect that endpoint to the original caller.
NodeJS SDK Basics
var xml = require('node-bandwidth').xml;
/// Start the XML response
var response = new xml.Response();
// Create the sentence
var speakSentence = new xml.SpeakSentence({sentence: "Thank you for calling, please wait while we connect you.",
voice: "paul", gender: "male", locale: "en_US"});
var transfer = new xml.Transfer({
transferTo: "+19198281234"
});
//Push all the XML to the response
response.push(speakSentence);
response.push(transfer);
// Create the xml to send
var bxml = response.toXml();
Bandwidth provides a SDK for NodeJS to build up XML to send back when an event happens.
Putting it all together
Putting all this together with a small Express app with SocketIO gives us a very basic demo that will keep track of how many times a number was called along with the latest incoming call number and call duration.
Get the code
Download the source code from the github link.
Set up ngrok
Download ngrok if you have not already. Ngrok is a handy tool that provides an external URL to map to localhost:port.

The application defaults to port 5000, so launch ngrok http mapping to port 5000:
ngrok http 5000
Be sure to copy the forwarding ngrok url, we’ll need that later to configure the application
Modify bandwidth.json
User creds and baseURL and specified in a json file <bandwidth.json>. Be sure to update the values provided by Bandwidth and ngrok.
- userId — UserID from Bandwidth
- apiToken — API Token from Bandwidth
- apiSecret — API Secret from Bandwidth
- baseUrl — ngrok url (from above)
{
"userId": "u-yourUserID",
"apiToken": "t-yourToken",
"apiSecret": "yourSecret",
"baseUrl": "http://yourURL.ngrok.io"
}
Install and Run
The app is written in node, so following normal conventions just run:
npm install
npm start

Then visit ‘localhost:5000’ and call the number on the screen to see some basic call tracking!
Live Demo

Check out a live demo at calltracking.bwdemos.com
Subscribe to Bandwidth
If for some reason you don’t already have an account, get started with the Bandwidth Platform by filling out the form here. One of our awesome support staff will get you setup in no time!