Monday 2 November 2009

Google Analytics: Expanded Mobile Tracking for Mobile Rails sites

Expanded Mobile Reporting - Introduction
Google recently announced expanded tracking for mobile apps using server side code. PHP, Perl, ASPX and JSP are supported as standard. As a dedicated 'Rails house' Moneyspyder has published a sample application for Mobile sites using Rails to broaden the usage of the product.

The need for this sample app is driven by the current use of javascript for tracking clickstream data. There are still a fair number of mobile devices out there that do not support client side javascript which can leave a sizable hole in your clickstream data. Using serverside code to reproduce the behaviour caters for these devices.

Preamble and cavets
The sample app is available for download. This is just a skeletal sample app and requires more thorough testing if deployed in the wild. We'd be keen to hear feedback of course as this is just a starting point....it ain't production ready!

Moneyspyder accepts no responsibility for loss or harm to data caused by using this sample app yadda yadda...

With that out of the way, some details.

Details

Download the app

Files

  • routes.rb

  • environment.rb

  • ga_helper.rb

  • ga_controller.rb

  • index.html



Two routes are used, only one is really required. The utm_gif action (served by the ga_controller.rb controller) is responsible for sending basic request info to the action and the action builds the remainder of the request details and sends them to ga, returning a 1px transparent gif.

The GA Account idientifer is saved in environment.rb. You might want to store it in a configuration yml file, the db or whatever your favourite place is...

The utm_gif_url helper method builds the utm_gif request. This is used in index.html for demonstration purposes.

ga_controller.rb rquires cgi, digest and open-uri. CGI is used for urlencoding strings. digest is used for the MD5 stuff. open-uri is used to send the request to Google analytics.

utm_gif is the only public method needed. response headers are set appropriately and send_data is used to return the transparent gif image. Requesting the gif through the private track_pageview method is where the magic happens.

In summary, all the usual name value pairs that are appended to the _utm.gif request to Google Analytics by ga.js are collected and then sent to Google Analytics using the private send_request_to_ga method. open-uri allows us to send this request programmatically. This might well be a nice plave to build in some error handling...

Once the request is done, the gif data is returned and the page is rendered - sweet.

You should be able to spin upthis little rails app easily enough using webrick on your local machine i the standard rails way and hit http://localhost:3000. (no support is offered by Moneyspyder for this setup - it's pretty standard stuff)

You'll see something like:



Gotcha and thoughts
If bots are hitting your mobile site or you use this tracking code technique insteaqd of ga.js (Hmmm?!) then you'll likely get a lot of traffic from bots - better filter the requests that get tracked.

2 comments:

jp said...

This is great, thanks for posting it.

Jayne said...

Thanks for bring it to our attention !