Google Analytics logoWhen running SEO for a business website, information on web traffic is key.
Google analytics is a useful tool to help keep track of your web properties. In the ever advancing world of connectivity one has to consider what traffic to include and what to exclude or differentiate.

As an example, we have a client with multiple brick and mortar locations who we run online PPC advertising campaigns for. When we present our reports we need to exclude the traffic that the online payment system is generating and report it separately. That can be done with Google analytics.

This same client has had a mobile version of its website since 2009, but getting Google’s mobile analytics package to work has presented a problem. To date, we have been using the server webalizer traffic package to supply that information. However, in the past few months we noticed that some of the locations have been getting as many mobile page views (in affluent areas) as other locations get normal page views, so it was time to get Google analytics for mobile to work.

You can find many articles about how to trouble shoot the Google mobile analytics package on the web, but we found that most of them hint at the issues without supplying a full set of solutions. So what follows is a guide to uncovering many of the known problems (in this case we had several) so you can get the package installed.

1). The Google mobile analytics package has several components and comes in multiple flavors (java, perl and php). We used the PHP package, because it is installed on all servers and is the most SEO friendly.

There are three parts to implement the package, something that is not always clear from the documentation. You need to download the analytics package from Google not just cut and paste the code from their website, it has the main files included.

First there is a PHP program called “ga.php” in the downloaded package and it needs to be installed in your mobile directory.

Second, there is a PHP function snipit that need to be put into your HTML mobile page above the “<html>“ tag.

Third, there is a smaller PHP snipit for a blank tracking pixel that needs to be inserted into the same mobile web page at the bottom just above the “</body>“ tag.

2). Test the main code for the Google mobile analytics package (ga.php). First upload the new mobile webpage and the “ga.php” files to your mobile directory. Then use your browser to test the “ga.php” file by calling it directly e.g. insert mobile.mydomain.com/ga.php into the address bar. What you should get is a 1×1 blank pixel, but what you might get is a PHP error code.

If you get a PHP error code it is most likely due to your PHP version. The file “ga.php” is written for PHP5, but many servers still default to PHP4. If this is the case then you have two options, first you can use your server panel to default to PHP5 (if it is installed) or second, you can edit the “ga.php” file.

The only function in “ga.php” that requires PHP5 is called “setrawcookie” you can replace that with “setcookie” and make the function PHP4 compatible.

Retest the “ga.php” function and if it now works move on to the next step.

3). Now it is time to test your mobile webpage. First make sure you made the correct changes o the code when you inserted it into the webpage. The part above the “<html>“ tag needs to have your Google id (note it is best to change the prefix of your number from “UA-” to “MO-” to let Google know it is a mobile page). You also have to let it know where you put the “ga.php“ file (if it is in the root directory you will have “/ga.php” or if it is in the mobile directory with the mobile pages then “ga.php” will work).

Running the webpage on your server and waiting to see if the analytics page sees it can take some time. A better test is see if the 1×1 blank pixel is in the webpage. There are a few ways you can accomplish this, one way is to open the developer window in your browser (in Firefox it is called “web console”) and try your mobile test page. If the function is not working you will see that 1×1 pixel is a broken image or a “404 not found” link. Another method is to look at the source of your served webpage in the browser (right click and choose view source). Scroll down to the bottom and look for the “img” tag, if it says something like “$googleAnalyticsImageUrl” it is not working. If it has a “ga.php?……” with all your data in it…then it is working. Still have problems? Then move to the next step.

4). A quick test is to use the “sample.php” file that came with the Google analytics package. Make the same changes to the function code in this file (your Google account number and the location of the “ga.php” file) and load it to the server to test it out e.g. “mobile.my domain.com/sample.php”. It will display your 1×1 pixel img code for you if it works. The most likely reason for the “sample.php” file not to work will be the setting for your “ga.php” file. Try “/ga.php” or “/mymobiledirectory/ga.php” until you get it to work. Another reason this may not work is that the Google snipits use some PHP “short code”, although I have not seen this issue on servers for years, PHP “short code” may be disabled. In that case you will need to change the snipit for the 1×1 pixel img call from “<?=“ to “?php”.

Once you have this sample file working, make the changes to your mobile webpage and test it again. If your mobile webpage is still not working move to the next step.

5). At this point you have verified that the “ga.php” function is working and that it can be successfully called from a different page (sample.php), but you may still be having issues with it working in you mobile webpage. The most likely reason for this is that your server is not parsing your HTML pages for PHP code. The easiest solution for that is to put a “.htaccess” file (or modify the one there) in your mobile directory telling the server to do that.

We have run into issues with this part, because the code can be different for different versions of PHP. By now you should know what version PHP you have from testing the “ga.php” file. If you had to modify the “ga.php” code you most likely have PHP4. In the “.htaccess” file enter the following:

AddType application/x-httpd-php4 .html .htm
Or for PHP5
AddType application/x-httpd-php5 .html .htm

Now retest your mobile webpage. At this point things should work, but you may now be getting a completely different error….if so move on to the next step.

6). If you are now getting parsing errors from you mobile webpage it is most likely do to the header information at the top of the webpage. Look to your “doctype” and other entries to find which ones need to be changed to stop the PHP handler from stopping. After that you can modify all of your mobile webpages accordingly and soon you will have data in your Google analytics account.

I hope this article is of help to some of you, it can be very frustrating to find only parts of the solution in various forums on the web. As is typical for the web, most solution posts stop as soon as they have it working and never lead to the next step

Cheers,
Linton Consultants