Hacking AIR for Android 2/2: Google Tracking without Javascript

For a test project I was doing I wanted a way to collect data about the device, but since you can't use JavaScript from an Android application created in AS3, because its not hosted within an HTML page, this forced me to use PHP to directly call Google's tracking pixel. The upside of this approach is that you can do only very basic analytics, the downside is that you cannot track how long someone stays on a 'page' or what the bounce rate is.

For more information about calling the tracking pixel directly look at this article by Google.

After reading up on Google' documentation about how to use the tracking pixel I came up with a modified version of this PHP file.

Right now I combined the two (the tracking and the data coming from the parser, as described here) and use this primarily for my own analytics; To see where the user clicks, what screens are viewed, what device and OS the user is on etc. If other people found an other / better way i'd love to hear about it in the comments!

This implementation consists of two parts, AS3 and PHP. Let start off with the AS3.



// Setup tracking. This constant is just a string that holds the application name.
UrchinTracker.setApplicationName(DocumentClass.APPLICATION_NAME);
// The url passed is the location where the tracking.php is located. Include a trailing slash
UrchinTracker.setDomain("http://yourdomain.ext/");

// Do actual tracking on the screens/pages you want
UrchinTracker.trackDevicePage(AnalyticsPageNames.SPLASH_PAGE);


Underwater this then creates an call to tracking.php?appname=YOUR_APPNAME&page=THE_SCREEN_YOU_WANT_TO_TRACK

The PHP then constructs an URL with the parameters defined in the Google spec defined above, calling the tracking GIF directly. Including the user defined parameters, appname and page.



header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL);

$var_utmac='UA-000000-0'; //enter the new urchin code
$var_utmhn='yourdomain.com'; //enter your domain
$var_utmn=rand(1000000000,9999999999);//random request number
$var_cookie=rand(10000000,99999999);//random cookie number
$var_random=rand(1000000000,2147483647); //number under 2147483647
$var_today=time(); //today
$var_referer=$_SERVER['HTTP_REFERER']; //referer url

$var_uservar = $_GET["page"]; //this is the 'page' or 'application screen' we want to track, this comes through a parameter
$var_utmp='/app/'.$_GET["appname"].'/'.$var_uservar; // this creates a fake dir in our analytics called apps to differentiate between the mobile application and the normal pages we are tracking.

$urchinUrl='http://www.google-analytics.com/__utm.gif?utmwv=1&utmn='.$var_utmn.'&utmsr=-&utmsc=-&utmul=-&utmje=0&utmfl=-&utmdt=-&utmhn='.$var_utmhn.'&utmr='.$var_referer.'&utmp='.$var_utmp.'&utmac='.$var_utmac.'&utmcc=__utma%3D'.$var_cookie.'.'.$var_random.'.'.$var_today.'.'.$var_today.'.'.$var_today.'.2%3B%2B__utmb%3D'.$var_cookie.'%3B%2B__utmc%3D'.$var_cookie.'%3B%2B__utmz%3D'.$var_cookie.'.'.$var_today.'.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D'.$var_cookie.'.'.$var_uservar.'%3B';

$handle = fopen($urchinUrl, "r");
$test = fgets($handle);
fclose($handle);

?>


This PHP can of course be extended/modified to track events or other bits of interesting information that is useful to you. I'm still looking at a way to do event tracking. Ideas anyone?

The results after a day was a nice list in Google Analytics showing up like this:
NOJS_Android_Tracking


Again the project can be found on Github for you to play around with, any questions and or comments, improvements etc, place a comment, i'd love to hear some feedback!


About this Entry

This page contains a single entry by Sidney de Koning published on January 5, 2011 9:00 AM.

HTML5 Recipes: Forms Enhancements was the previous entry in this blog.

Thoughts on Variable Scope in As3 is the next entry in this blog.

Find content using the provided navigation or look in the archives to find all content.

Authors

Archives

This content archive is licensed under a Creative Commons License.