Google Analytics for Mobile Websites

Overview

All the same data that you've come to expect from your Google Analytics reports is now available for targeting WAP-based phones or other low-end mobile devices that can't execute JavaScript. Simply paste our server-side code snippets (available for PHP, JSP, ASP.NET, and Perl) on each page you wish to track. Once implemented, Google Analytics will then create the same kind of information in the standard Analytics reports including session and traffic source information.

Once the server side snippet is implemented, the language specific code, will generate a URL for an image tag that needs to be placed on each tracked page. The URL for the image will be the same URL on which the server-side snippet is hosted. When a user navigates to the tracked page from their mobile device, their mobile browser will load the page, that will in turn, make a request for the image created by the snippet above. When the server-side snippet receives the request, it will fire off another request to send data to Google Analytics.

Getting Started

To start using the Google Analytics server-side snippet, you must:

  1. Download the server snippet for your server environment.
  2. Upload the code snippet to your server.
  3. Update the pages on the server you wish to track with language specific code

Download the Library

The following package contains server-side snippets and examples for all the supported languages.

Package Size SHA1 Checksum
googleanalyticsformobile.zip 35.7 KiB 9102c9d8f9ddd3f53f7330d41b3b14b73a662646

Once downloaded, you must upload the snippet in the language of your choice, to your web server.

Update Your Web Pages

Once you have uploaded the server side snippet to your web server, you are ready to add the tracking code to each of your pages. Click the tab for the programming language specific instructions.

Note: You must change the prefix on your analytics web property ID from UA- to MO- in the server-side snippets given below. For example, if your web property ID is UA-12345-67, you would use MO-12345-67 in your server-side snippets.


PHP

The server-side snippet for PHP requires PHP 5.

The Google Analytics for Mobile PHP package includes a file named ga.php which, when loaded, writes a small image to the response. To use it, you must copy ga.php to your project such that it can be accessed by your other PHP scripts. Your users will not be accessing it directly, but your own PHP scripts will contain an HTML <img> tag which references ga.php. The ga.php code, in turn, sends a request to the Analytics service and returns the image data which is rendered via HTML.

A sample method, googleAnalyticsGetImageUrl, is included in the package to help you generate the full image URL. Here is the sample script:

<?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 = $_SERVER["HTTP_REFERER"];
    $query = $_SERVER["QUERY_STRING"];
    $path = $_SERVER["REQUEST_URI"];

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

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

    $url .= "&guid=ON";

    return str_replace("&", "&amp;", $url);
  }
?>

Now, it's just a simple matter of calling this method and using the resulting URL as the src attribute of an HTML <img> tag. The method will send a request to ga.php with the relevant tracking parameters. ga.php will send these parameters to the Analytics service and return the image to render:

<?php
  $googleAnalyticsImageUrl = googleAnalyticsGetImageUrl();
  echo '<img src="' . $googleAnalyticsImageUrl . '" />';
?>

JSP

The server-side snippet for JSP has been tested on Jetty 6.

The Google Analytics for Mobile JSP package includes a file named ga.jsp which, when loaded, writes a small image to the response. To use it, you must copy ga.jsp to your project and make sure you include a mapping in web.xml or otherwise such that it can be accessed. Your users will not be accessing it directly, but your own JSPs will contain an HTML <img> tag which references this JSP. The ga.jsp code, in turn, sends a request to the Analytics service and returns the image data which is rendered via HTML.

A sample method, googleAnalyticsGetImageUrl, is included in the download package to help you generate the full image URL. Here is the sample JSP:

<%@ page import="java.io.UnsupportedEncodingException,
                    java.net.URLEncoder" %>
<%!
  // Copyright 2009 Google Inc. All Rights Reserved.
  private static final String GA_ACCOUNT = "ACCOUNT ID GOES HERE";
  private static final String GA_PIXEL = "/ga.jsp";

  private String googleAnalyticsGetImageUrl(
      HttpServletRequest request) throws UnsupportedEncodingException {
    StringBuilder url = new StringBuilder();
    url.append(GA_PIXEL + "?");
    url.append("utmac=").append(GA_ACCOUNT);
    url.append("&utmn=").append(Integer.toString((int) (Math.random() * 0x7fffffff)));

    String referer = request.getHeader("referer");
    String query = request.getQueryString();
    String path = request.getRequestURI();

    if (referer == null || "".equals(referer)) {
      referer = "-";
    }
    url.append("&utmr=").append(URLEncoder.encode(referer, "UTF-8"));

    if (path != null) {
      if (query != null) {
        path += "?" + query;
      }
      url.append("&utmp=").append(URLEncoder.encode(path, "UTF-8"));
    }

    url.append("&guid=ON");

    return url.toString().replace("&", "&amp;");
  }
%>

Now, it's just a simple matter of calling this method and using the resulting URL as the src attribute of an HTML <img> tag. The method will send a request to ga.jsp with the relevant tracking parameters. ga.jsp will send these parameters to the Analytics service and return the image to render:

<% String googleAnalyticsImageUrl = googleAnalyticsGetImageUrl(request); %>
<img src="<%= googleAnalyticsImageUrl %>" />

ASP.net

The server-side snippet for ASP.NET has been tested on IIS 6.0.

The Google Analytics for Mobile ASP.NET package includes a file named ga.aspx which, when loaded, writes a small image to the response. To use it, you must copy ga.aspx to your project such that it can be accessed by your other ASPs. Your users will not be accessing it directly, but your own ASPs will contain an HTML <img> tag which references ga.aspx. The ga.aspx code, in turn, sends a request to the Analytics service and returns the image data which is rendered via HTML.

A sample method, GoogleAnalyticsGetImageUrl, is included in the download package to help you generate the full image URL. Here is the sample ASP:

<%@ Page Language="C#" %>
<script language="C#" runat="server">
  // Copyright 2009 Google Inc. All Rights Reserved.
  private const string GaAccount = "ACCOUNT ID GOES HERE";
  private const string GaPixel = "/ga.aspx";

  private string GoogleAnalyticsGetImageUrl() {
    System.Text.StringBuilder url = new System.Text.StringBuilder();
    url.Append(GaPixel + "?");
    url.Append("utmac=").Append(GaAccount);

    Random RandomClass = new Random();
    url.Append("&utmn=").Append(RandomClass.Next(0x7fffffff));

    string referer = "-";
    if (Request.UrlReferrer != null
        && "" != Request.UrlReferrer.ToString()) {
      referer = Request.UrlReferrer.ToString();
    }
    url.Append("&utmr=").Append(HttpUtility.UrlEncode(referer));

    if (HttpContext.Current.Request.Url != null) {
      url.Append("&utmp=").Append(HttpUtility.UrlEncode(Request.Url.PathAndQuery));
    }

    url.Append("&guid=ON");

    return url.ToString().Replace("&", "&amp;");
  }
</script>

Now, it's just a simple matter of calling this method and using the resulting URL as the src attribute of an HTML <img> tag. The method will send a request to ga.aspx with the relevant tracking parameters. ga.aspx will send these parameters to the Analytics service and return the image to render:

<% string googleAnalyticsImageUrl = GoogleAnalyticsGetImageUrl(); %>
<img src="<%= googleAnalyticsImageUrl %>" />

Perl

The server-side snippet for Perl requires Perl 5.10.

The Google Analytics for Mobile Perl package includes a file named ga.pl which, when loaded, writes a small image to the response. To use it, you must copy ga.pl to your project such that it can be accessed by your other Perl scripts. Your users will not be accessing it directly, but your own Perl scripts will contain an HTML <img> tag which references ga.pl. The ga.pl code, in turn, sends a request to the Analytics service and returns the image data which is rendered via HTML.

A sample subroutine, google_analytics_get_image_url, is included in the download package to help you generate the full image URL. Here is the sample script:

# Copyright 2009 Google Inc. All Rights Reserved.
use URI::Escape;

use constant GA_ACCOUNT => 'ACCOUNT ID GOES HERE';
use constant GA_PIXEL => '/ga.pl';

sub google_analytics_get_image_url {
  my $url = '';
  $url .= GA_PIXEL . '?';
  $url .= 'utmac=' . GA_ACCOUNT;
  $url .= '&utmn=' . int(rand(0x7fffffff));

  my $referer = $ENV{'HTTP_REFERER'};
  my $query = $ENV{'QUERY_STRING'};
  my $path = $ENV{'REQUEST_URI'};

  if ($referer eq "") {
    $referer = '-';
  }

  $url .= '&utmr=' . uri_escape($referer);
  $url .= '&utmp=' . uri_escape($path);
  $url .= '&guid=ON';

  $url =~ s/&/&amp\;/g;
  $url;
}

Now, it's just a simple matter of calling this subroutine and using the resulting URL as the src attribute of an HTML <img> tag. The subroutine will send a request to ga.pl with the relevant tracking parameters. ga.pl will send these parameters to the Analytics service and return the image to render:

print '<img src="' . google_analytics_get_image_url() . '" />';