AfricasTalking— Call tracking with Google analytics

This is a simple flask/ python project to help with google analytics call tracking. The aim of this project is to provide an easier way to integrate google analytics to Voice applications to ease call tracking tasks. This enables us to track call conversions over time.

To achieve call tracking, we need to know what view the caller’s number is mapped to, a web-app would render a view with a randomized number off a pool of virtual numbers; we can at that point track the view to a number. Below is a sample view showing listings.

sample views
Cheki mapped a virtual number to a view id

This could also be achieved with an ajax event being called when the call button is clicked, then at that a point a number would be picked randomly off the pool. In this case the number is rendered on the template when a view is called.

The big challenge here is to expire the number on a view but this could be achieved with a TTL on a data-store such as Redis. This is done to free numbers and make sure the pool is replenished.

When a call’s state is incoming, we first check what the user’s view is currently mapped to; in this case it’s a view number 18746. We can then get the users location with HTML5. Now we know what the user is looking at (view) and where they are. We then render this to a separate dashboard for incoming calls; this page polls continuously so it refreshes the view to get the current ongoing calls.

View showing users location and view they are mapped to

The dependencies to this project are minimal — (check


Using pyga — you can publish events to google analytics from the back-end when the call cost (for non-zero rated)/ duration is more than 0 units, before we publish the event; we need to create a valid user session.

visitor = Visitor()
visitor.ip_address = request.remote_addr
session = Session()
event = Event(‘conversion’, ‘call-initiated’) # category — action
# or if the call was dropped
event = Event(‘conversion’, ‘call-dropped’)
tracker.track_event(event, session, visitor)

So, at this point, we are able to track call conversions for dropped and completed/ initiated calls.

These events are then published to GA, on the analytics real time events page, you can visualize the stream of events being registered. But we only publish these events when isActive is false.

A stream of events (call-conversions) being published.

A route is set registering an endpoint where we get the ongoing call events from the AfricasTalking API’s. See gist for code sample. This sample project is available at this link.

Like what you read? Give Ian Juma a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.