Mobile Analytics With Python, Django, ASP.NET, Java, and node.js
A few weeks ago, the PercentMobile team came to me to see if I could help write some new libraries for them. Contemporary web and mobile web sites are written on a vast array of different platforms… and obviously the more that PercentMobile supports, the better.
Something I love about programming is that there are so many languages to choose from - why restrict yourself to learning or becoming an expert at one when the same problems are also being solved in other, sometimes better, ways? Each language or platform has strengths and weaknesses of course. But I believe that understanding the differences - and more often, the similarities - makes one a better programmer.
So, generous polyglot that I am, I took the challenge, and plunged in.
Python & Django
I love Python - its readability, its libraries, and its overall philosophies. In a web server environment, Python can be used in a fairly raw way, behind a generic Web Service Gateway Interface (WSGI), but there are also a number of powerful web application frameworks using the language - most notably Django. PercentMobile want to provide painless Django support, but also to make sure that other Python server environments were not precluded.
percentmobile.py is the single file that provides everything you need.
If you are running code in a WSGI environment, you will have a handle to the WSGI environment. This is normally called environ by convention, and is passed in to your application via your top-level WSGI callable. To make the PercentMobile tracking code work, you need only make one call to the percentmobile.tracker_cookie_insert function. It returns two values: the cookie that you’ll need to set in the HTTP response headers, and the HTML that you should insert into your page.
Say, for example, you had a very simple WSGI app. This responds to requests with an HTTP 200 status code, a single header and some simple HTML:
def my_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type','text/html')]
start_response(status, response_headers)
return ["Hello world"]
To add PercentMobile tracking to this code, you need to firstly import the tracker library of course, and then call the tracker_cookie_insert function:
import percentmobile
cookie, insert = percentmobile.tracker_cookie_insert(environ, '1234555')
(Of course you should replace the final string with your own site ID!)
The cookie return value is actually a dictionary of the different parts needed to construct its string serialization. This will allow you to splice together multiple cookies, or alter the expiry time, path scope and so on. To convert the dictionary to a string, and to get the Set-Cookie header sent back in the request headers, the following code will suffice:
cookie = "%s=%s; expires=%s; path=%s" % (
cookie['name'],
cookie['value'],
cookie['expires'],
cookie['path']
)
response_headers = [('Content-type','text/html'), ('Set-Cookie', cookie)]
Finally, you need to make sure the HTML fragment, in the insert variable, gets placed in the HTML response:
return ["<html><body>Hello world %s</body></html>" % insert]
And that’s a wrap. The whole, PercentMobile-tracked WSGI application looks like this:
def my_app(environ, start_response):
cookie, insert = percentmobile.tracker_cookie_insert(environ, '1234555')
cookie = "%s=%s; expires=%s; path=%s" % (
cookie['name'],
cookie['value'],
cookie['expires'],
cookie['path']
)
status = '200 OK'
response_headers = [('Content-type','text/html'), ('Set-Cookie', cookie)]
start_response(status, response_headers)
return ["<html><body>Hello world %s</body></html>" % insert]
Pretty easy, huh? Well, not as easy as tracking an app if you’re using the amazing Django framework! Contained in the same percentmobile.py file is a class that can be used as Django middleware. It uses the same underlying function as the WSGI implementation, but also takes care of the headers and insertion for you.
Assuming you’ve placed the library file in your Python or Django path, you simply need to add two lines of code in the settings file. Firstly add the percentmobile.PercentMobileDjangoMiddleware class to your list of middleware:
MIDDLEWARE_CLASSES = (
...
'percentmobile.PercentMobileDjangoMiddleware'
)
And secondly, add your PercentMobile site ID to the settings file too:
PERCENTMOBILE_SITE_ID = '1234555'
Um… that’s it. The middleware will intercept the request, figure out the cookie that will need to be sent in the response, create the insertion code, and place it just before </body> in the response. You’re golden. I love Django.
ASP.NET: C# & VB.NET
Switching over to another world altogether, let’s take a quick look at the ASP.NET implementation of the tracking code. One of the great things about .NET is that you can choose between all sorts of different languages to write your applications and pages in. I decided to write the tracking library in C#, but you can use it declaratively in your page code, or programmatically from C#, VB.NET, or any other supported language.
The library is implemented as a User Control - that is, as a .ascx file. You need to download and add PercentMobile.ascx to your web application project.
To embed the tracking logic into a page (or probably preferably, a master page), you simply register the user control as residing in that file:
<%@ Register TagPrefix="pm" TagName="Tracker" Src="~/PercentMobile.ascx" %>
And then embed the control straight into the .aspx file contents, wherever you want it to be:
<pm:Tracker runat="server" SiteId="1234555" />
So a simple, tracked .aspx file might look something like this:
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register TagPrefix="pm" TagName="Tracker" Src="~/PercentMobile.ascx" %>
<!DOCTYPE html>
<html>
<head><title>Hello World</title></head>
<body>
<form id="form1" runat="server">
<div>Hello World</div>
<pm:Tracker runat="server" SiteId="1234555" />
</form>
</body>
</html>
To be honest, this is so simple that I would expect most people to use the control declaratively. But if, for some reason, the control needs to be inserted programmatically, that’s pretty easy too. You need to use the Reference directive, instead of Register, at the top of the file, but otherwise it’s not much harder. Here, we’re using VB.NET to programmatically achieve exactly the same result as above:
<%@ Page Language="VB" AutoEventWireup="true" %><%@ Reference Control="~/PercentMobile.ascx" %>
<script runat="server">
Private tracker As PercentMobile.Tracker
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
tracker = CType(LoadControl("~/PercentMobile.ascx"), PercentMobile.Tracker)
tracker.siteId = "1234555"
form1.Controls.Add(tracker)
End Sub
</script>
<!DOCTYPE html>
<html>
<head><title>Hello World</title></head>
<body>
<form id="form1" runat="server">
<div>Hello World</div>
</form>
</body>
</html>
(It should be fairly straightforward to see how to do this in other .NET languages - PercentMobile includes some examples in their install instructions.)
Java
Onwards. Let’s take a look at the tracking code for Java. There are numerous ways in which Java can be used for web or application server environments. To keep things simple, I decided that supporting JSP was more or less the most familiar and reusable approach.
JSP doesn’t enjoy the rich page event model that I was able to use in ASP.NET to intercept headers, write cookies, and insert HTML, all with one include. <jsp:include> is OK for adding the HTML snippets, but wouldn’t let me access the HTTP headers. <jsp:forward> does, but would only work if you weren’t planning to emit any HTML of your own after the tracking code - something of a radical assumption.
So I settled for an approach where an include directive creates an instance of an inner PercentMobile class defined within the JSP class. To cut a long story short, this means you merely add a reference to the library file at the top of the JSP file:
<%@ include file="percentmobile.jsp" %>
And then call the track method on that instance, somewhere within the page:
<%percentMobile.track("1234555");%>
The included JSP file takes care of instantiating the percentMobile object and giving it references to the request and response stream. This means it can read and write cookies, and later emit HTML. Your tracked Hello World in Java then? It’s as simple as this:
<%@ include file="percentmobile.jsp" %>
<html>
<body>
Hello World
<%percentMobile.track("1234555");%>
</body>
</html>
node.js
OK, OK. Python, ASP.NET, Java. No big deal, right? That’s all so 2003 or so, right?
Well, there’s an alternative future for web server technologies. It’s one that’s blazingly fast, lightweight, event-driven, and… where you write your server logic in Javascript.
node.js is one of the most exciting things I’ve seen for a long time, and I know I’m not alone. Fresh, fashionable, and somewhat unproven, admittedly, it’s had a lot of gushing coverage. But since it turns the web server model (almost literally) inside out, I do believe there’s something important going on.
Would it be possible to write a web app with node.js and still have it tracked by PercentMobile? My challenge.
I decided to rely on Connect, a middleware framework for node.js, which, if you are writing web applications, provides a whole host of other helpful web logic. Using Connect to create a simple node.js app is extremely easy:
var Connect = require('connect');
Connect.createServer(
function (req, res, next) {
res.simpleBody(200,
"<html><body>Hello World" +
"</body></html>",
{
"Content-Type": "text/html"
}
);
}
).listen(88);
To add PercentMobile tracking to this application firstly requires you to pull in the PercentMobile module:
var PercentMobile = require('./percentmobile');
(Where the percentmobile.js file has been placed in your node.js environment or in the common modules location.)
The module needs to be initialized as a piece of Connect middleware in the createServer function:
PercentMobile.init('1234555')
There are then two module functions, cookie and html, which both take a reference to the response object, and which return the cookie string and HTML to insert, respectively. These can be used in Connect’s simpleBody function, for example, meaning that our tracked application is as simple as this:
var Connect = require('connect');
var PercentMobile = require('./percentmobile');
Connect.createServer(
PercentMobile.init('1234555'),
function (req, res, next) {
res.simpleBody(200,
"<html><body>Hello World" +
PercentMobile.html(res) +
"</body></html>",
{
"Content-Type": "text/html",
"Set-Cookie": PercentMobile.cookie(res)
}
);
}
).listen(88);
That’s a wrap
So that’s it. A whistle-stop tour of the new languages and frameworks supported by PercentMobile. I’d love to hear your feedback on how easy (or hard!) these new APIs are to use.
And, oh… who will be the first to build a mobile web app on node.js? :-)
James Pearce is the former CTO of dotMobi and Argogroup, and has evangelized, coded, written and spoken about the mobile web and mobile development for over a decade. Find him online at http://tripleodeon.com/about
PercentMobile Takes a Look at Which Countries are the Most “QR Code Ready.”
The map below depicts the percentage of mobile Web traffic from devices that have the ability to install a QR Code Reader.

It’s Apple in the Air
In their most lucid dreams the Wright Brothers may have thought of flying to the Moon or having an entire city floating in the sky, but the thought of having instant access to all of the world’s knowledge probably never crossed their minds. This implausible power belonged in the realm of gods. Now with the Hertzian space expanding upwards we find ourselves with this power. What mobile devices are used for these flights of networked fancy?
It appears Apple has done an even better job in the air than it has done on the ground. Over 90% of in-flight mobile web usage that PercentMobile has measured in the last 3 months has come from Apple Devices. The newly released iPad ranked first with a solid 41% — no surprise considering its extra long battery life. The iPhone ranked second with 38%, with the iPod adding an additional 15%. It also hasn’t hurt that Apple has made it drop dead simple to toggle on Airplane Mode and WiFi from each of these devices.

PercentMobile Maps - 2nd Quarter 2010
Welcome to our first round of PercentMobile Maps. Our goal is to introduce you to the wonderfully diverse global mobile ecosystem. All maps are based on second quarter 2010 visits to sites that are tracked with our mobile analytics system.
Nokia Devices Web Usage
Lets start with the CEO searching and share dropping Finnish giant. Once globally perceived as the mobile industry leader, Nokia is now struggling with its quickly aging operating systems and number pad phones. While it currently has a stronghold in the largest and most populous countries, most of those phones are quite old and many will soon need to be replaced. The large majority are inexpensive phones with less profit margin and no attractive digital media store.
Apple Devices Web Usage
A phenomenon, Apple rose from nothing in the summer of 2007 to a mobile superpower with amazing interface concepts, fantastic web experience, attractive applications and its iTunes digital media store. It speaks to their success that nearly everyone is trying to copy them.
BlackBerry Devices Web Usage
It used to be that the most recognizable feature of a businessman or politician was their suit. Now it is the symbiotic relationship with their BlackBerry device. Joining their ranks are teenagers who pump out text messages quickly thanks to BlackBerry’s built-in QWERTY keyboard, optimized for “thumbing.” An indication of BlackBerry’s success is that several companies such as Nexian, ZTE, Tianyu, Videocon and ti-phone are attempting to clone these iconic devices.
Samsung Devices Web Usage
The South Korean Conglomerate brand is found on everything from Chips, LCD Displays, Refrigerator, and Ships to the shirts of the English Premier League Team the Chelsea Football Club. Their mobile phones run Bada OS, Windows Mobile, Symbian, Android and their own proprietary Operating System. It is perhaps the most diverse cell phone company, which is not surprising given its mothership nature.
WiFi Usage
Mobile phones with WiFi connectivity began to appear in 2006 with the Nokia N93. Now, most Smart and Experience Phones come equipped standard with WiFi. Their promise is faster speed and cost-free mobile browsing. Downsides include heavy battery usage, and not all user interfaces make it easy to select a WiFi network. The map below shows how actual users of WiFi-enabled mobile phones take advantage of the WiFi feature. Please note that we have excluded Non-Phones such as iPad, iPod, Sony PSP, etc. from this map.
Device Age
Last but not least, let’s have a look at the average release date of mobile devices used by country. Most countries in the Americas and Western Europe have newer devices, while most countries in Asia and the Middle East have slightly older devices, and many African countries have the oldest devices.
“
But all these “big boys” [Google Analytics and WebTrends and others] have simply “added on” mobile analytics to their tools. The result is that they suffer from both a lack of imagination and, this is important, truly great databases when it comes to devices and carriers and other unique mobile information.
Not Percent Mobile.
They have two incredible benefits:
1. A really expansive and accurate database and detection mechanism when it comes to mobile platforms.
2. A really simple UI and reporting layer, even your mom will understand the data.
”Trendspotting with PercentMobile
Just as with the world at large, the mobile ecosystem is in constant motion. It grows and changes, sometimes significantly, sometimes subtly, every day. We want you to easily learn and understand those changes happening right now on your Site using Mobile Analytics by PercentMobile.
With this post the team is excited to introduce to you a new feature called ‘Trending’ which, after being used extensively internally, is now available for free on every Mobile Analytics Report for every user.
Using the feature ourselves has quickly and easily given us insight we couldn’t have gotten before on the mobile ecosystem at large. The results can be viewed in our recently published post “Second Quarter 2010 Mobile Ecosystem Highlights” where we gathered trending highlights on Operating Systems, Phone Types and WiFi Usage over the past 6 months.
For example, using the Trending feature we were able to observe a 95% increase in Android OS usage in the US (and a 94% increase in Europe respectively) across our network of tracked sites, which otherwise would not have been easily possible. Being able to perceive changes like this yields almost immediately a call to action in terms of consideration of the Android OS platform overall. We find this very exciting to know. It’s beautiful.
Click here or image above for full view.
With Trending you are now able to observe changes in most of your Reporting-dimensions.
To get you started, we would like to ask you:
- Have WiFi-capable Devices been growing on your Site?
- And what about iPads and other Non-Phone mobile devices? Can you find out if they’re becoming a growing audience of yours?
- How are older Feature-Phones doing compared to highly interactive Experience-Phones?
- Is traffic from iPhone OS Devices such as the iPhone, iPods and iPads growing or slowing?
- Has your Site become more popular with owners of RIM OS Blackberry Devices?
- Can you find a former unknown-to-you Network Operator climbing up hundred-fold within the last month?
- Which country has dropped interest in your offering and which country is showing growing interest?
- Have you noticed a drop or rise in WiFi usage and can you imagine why that is?
If you are already using PercentMobile, we highly recommend you sign in to your Reports right now and check out what insights your new Trending feature can offer you. If you are new to PercentMobile, get started to track your Sites by registering today.
We hope you will enjoy this new level of comprehension of your mobile ecosystem. Our objective always has been to make things simple and understandable so you can concentrate on what really matters to you.
With best wishes,
Your PercentMobile team.
11% of Web Traffic Worldwide is Now Mobile
Mobile traffic to sites designed for the desktop Web have increased over the last 6 months from 8.3% to 11%, a 32% increase.







