Finally – statistics for your mobile site in Google Analytics

Google analytics finally ready for mobile xhtmlTracking traffic on mobile site has so far been quite difficult. The majority of the web analytics programs are based on JavaScript tracking, which is problematic with mobile sites, since many mobile devices still have bad support of JavaScript.

The good news is that Google Analytics now has been upgraded with additional tracking features, that allows tracking of mobile sites, even for mobile phones without JavaScript support. In this post you’ll learn how to add Google Analytics tracking to your mobile site.

Getting started

Before getting started you need to have a Google Analytics account, and create a new profile.
You create a new website profile by clicking at the link “Add website profile” at the end of the start page.
Create a new google analytics profile

After the profile has been set up, the standard tracking code is displayed.

Ignore this code and go to this page instead. At this page you’ll find Google Analytics code for special mobile tracking.  Google Analytics supports Mobile Server Side Tracking for the following operating systems:

  • PHP
  • JSP
  • ASP.NET
  • Perl

In this post we’ll look at the tracking code for a mobile site, coded in PHP.

PHP installation

Installing the code at your php based mobile sites includes 4 steps:

  1. insert a general tracking function at the start of your PHP code
  2. change the account id in the tracking function
  3. insert a code that calls the tracking function at the end of the html/xhtml output
  4. copy the file ga.php to your site

The general function for server side tracking looks like this:

<?php
//  Copyright 2009 Google Inc. All Rights Reserved.
$GA_ACCOUNT = "ACCOUNT ID GOES HERE";
$GA_PIXEL = "ga.php";

function googleAnalyticsGetImageUrl() {
global $GA_ACCOUNT, $GA_PIXEL;
$url = "";
$url .= $GA_PIXEL . "?";
$url .= "utmac=" . $GA_ACCOUNT;
$url .= "&utmn=" . rand(0, 0x7fffffff);

$referer = $_SERVERHTTP_REFERER;
$query = $_SERVERQUERY_STRING;
$path = $_SERVERREQUEST_URI;

if (empty($referer)) {
$referer = "-";
}
$url .= "&utmr=" . urlencode($referer);

if (!empty($path)) {
$url .= "&utmp=" . urlencode($path);
}

$url .= "&guid=ON";

return $url;
}
?>

Remenber to insert your Google Analytics Account id in the line: ”

$GA_ACCOUNT = "ACCOUNT ID GOES HERE";"

The insert this code in your mobile page, just before </body>:

<pre><?php
$googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
?>
<img src="<?= $googleAnalyticsImageUrl ?>" /></pre>

Last you need to copy the ga.php file from the Google Analytics for Mobile server-side snippets SDK to your mobile site, and adjust the line “$GA_PIXEL = “ga.php”;” to reflect the location of the local ga.js file.

And then you’re up and running.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
fold-left fold-right
About the author
Jørgen Nicolaisen has been passionately interested in everything online since 1995. His experience is based on working with small hobby projects as well as high volume websites. Jørgen is currently focused on the PHP based programming framework - Codeigniter, and WordPress naturally

Comments are closed.