Google Mobile Analytics and Opera Mini
Google Analytics is a popular web statistics site with lots of useful features, I’ve been a big fan since the start. Google recently added a bunch of new features, and an important one out of those for me is mobile web tracking.
Typically, Google’s way of tracking site usage is to ask you to put a small snippet of HTML onto your site which is essentially a javascript call to their servers. Great – except a lot of mobile phones don’t actually support javascript. So the only good solution is to run some code on the server. I don’t know if it’s related to Google’s purchase of AdMob, but they’ve done exactly that and provided server side code in various programming languages (C#, PHP, etc) to achieve this. See ga.php as a real example.
Great, now I’ve got beautiful graphs showing me how people use my site. But what’s that? 25% of my users live in the country (not set)? I’ve not heard of the place! It turns out those users more or less all used one browser: Opera Mini.
Oh Opera Mini, what a strange beast you are. While the Opera browser is a full normal browser, Opera Mini is a service that installs a thin client on your phone and Norwegian proxy servers visit the site for you. Norway? Well that’s a little better than living in (not set).
Luckily, this whole problem is fixed by a teensy change in their server side code. Rather than always fetching the browser’s current IP address, I tweaked Google’s PHP code so that it detects Opera Mini is in use and sends the real user’s IP address. To be honest, I’m a little confused why this isn’t in Google’s code in the first place.
So here it is. A minor fix to Google’s ga.php file (line 162) to improve Opera Mini handling:
Original line: "&utmip=" . getIP($_SERVER["REMOTE_ADDR"]);
and my version: "&utmip=" . getIP((stristr($_SERVER['HTTP_USER_AGENT'], 'opera mini') && array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) ? $_SERVER['HTTP_X_FORWARDED_FOR']: $_SERVER["REMOTE_ADDR"]);
That’s all. Now I get real country stats :)