Adding Google Analytics in your mobile site

Google Analytics is well known for having great reports and tracking and for being a free product, therefore using Javascript in a mobile site is almost impossible, but there is one good and tested alternative.

This is what you got to do:

$utmUrl           = ‘’?’
. ‘utmwv=4.4sh’
. ‘&utmn=’ . $randomNumber
. ‘&utmhn=’ . urlencode($domainName)
. ‘&utmr=’ . urlencode($documentReferer)
. ‘&utmp=’ . urlencode($path)
. ‘&utmac=’ . $gaAccount
. ‘&utmcc=__utma%3D999.999.999.999.999.1%3B’
. ‘&utmvid=’ . $visitorId
. ‘&utmip=’ . $ip
. ‘&guid=ON’;

Your $gaAccount will be your Google account for the Analytics with the following difference. Instead of using UA-XXXXX-XX you will be using MO-XXXXX-XX. The prefix MO will indicate to Google that it will hold data for a mobile website. About that is also good to point out that you will need a separate account from your website to track your mobile website, otherwise one data might override the other (known Google issue).

With the exception of the $ip and $visitorId, the rest of the parameters are straight forward.

$referer         = $_SERVER(‘HTTP_REFERER’);
$documentReferer = (!empty($referer)) ? $referer : ‘-‘;
$path            = $_SERVER(‘REQUEST_URI’);
$userAgent        = $_SERVER(‘HTTP_USER_AGENT’, ”);
$dcmGuid          = $_SERVER(‘HTTP_X_DCMGUID’);
$randomNumber     = rand(0, 0x7fffffff);
$timestamp        = time();
$domainName       = $_SERVER(‘SERVER_NAME’, ‘’); // or your mobile url

To get the IP, Google reserves, to keep the user privacy, to remove the last 3 octets from the IP. It’s a simple task.

// captures the 1st 3 octets of the ip and replace last with 0
$regex = “/^([^.]+\.[^.]+\.[^.]+\.).*/”;
$ip = (preg_match($regex, $remoteAddress, $matches)) ? $matches[1] . ‘0’ : ”;

The visitor id, it’s a more complex process, but Google provides you the function, here is a copy from it:

function getUniqueVisitorId($guid, $account, $userAgent) {

$message      = ”;
$randomNumber = rand(0, 0x7fffffff);
$cookie       = $_COOKIE[‘__utmmobile’];

if (isset($cookie))
$result = $cookie;
if (!empty($guid))
$message = $guid . $account;
$message = $userAgent . uniqid($randomNumber, true);

$md5String = md5($message);
$result    = “0x” . substr($md5String, 0, 16);

return $result;

Known issues that I got with data so far:

  • If you have this script inside another script from your own site, make sure that it dont’ load itself to avoid double visits
  • Since the number of phones that support cookies are very small, most likely every visit will be a unique visit.
  • You can’t use the same Google Analytics Account from your site to your Mobile site
  • Google Analytics support is all user based -> forums

For more info about how to work with Google Analytics for Mobile Sites and / or Applications, go to:


About mcloide

Making things simpler, just check: View all posts by mcloide

4 responses to “Adding Google Analytics in your mobile site

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: