Using Google Analytics for Mobile and fixing a lang not set problem

After building my own stats logging system, I decided to try the non-javascript version of GA, aka Google Analytics for Mobile.

It uses no Javascript to do the tracking pixel call, relying instead on you to implement that call the way you want. Perfect for my needs of "no Javascript if possible".

Documentation is excellent and there are code samples for the majority of web building languages, but I noticed two issues:

First, the code samples, at least PHP and ASP.NET ones, look as if written by someone who didn't cared much about code quality.

Huge string concatenations in C# instead of using StringBuilder, unused library references, unused variables... And the PHP version, probably to support PHP 4 was also quite crappy and with globals.

Overall I could reduce PHP code to 1/3 and ASP.NET code to 1/2 of their original size (also optimized some things to my specific needs). So i t is worth to spend a bit of time in cleaning the samples if you plan to use them.

And second, once I had everything in place and working, I noticed that the language was not being sent to my stats. All of them were reflected in Google Analytics as "(unset)". While not critical, I like to know from where my visits come from, so I digged a bit to see how this works in the normal GA. The solution is quite easy, just append this to your google analytics tracking pixel call:

'&utmul=' . $_SERVER["HTTP_ACCEPT_LANGUAGE"];

utmUrl.Append("&utmul=").Append(Request.Headers.Get("Accept-Language"));

For PHP and C# respectively.

Tip: most headers will have more stuff in that header (for example "es-ES;q=0.7"), so you might want to do some cleaning before, as Google Analytics won't do it.

In the end this is the solution I've built for most of my sites. Even with the tracking call on the same domain as the site and without any asynchronous Javascript, the speed is greater. And I now have one less Javascript to force uses to download while keeping the nice GA interface and detailed reporting system (which is really good indeed).

Tags: Development

Using Google Analytics for Mobile and fixing a lang not set problem article, written by Kartones. Published on