Entries tagged "Google Analytics"

Deactivate Code Automatically on a Test Server

When fixing a bug or adding a new feature to a website, it may be beneficial to test the changes on a development server before going live. The problem is that some aspects of the code probably shouldn't be active on the development server. For example, we probably don't Google Analytics tracking any of those visits. The code could be manually disabled during the test phase, but are we going to remember? Instead let's look into automating the process. [Continue reading]

Using Google Analytics to Determine Which Browsers Are Used Most for Accessing Your Website

When designing websites, it's important to have a general idea of which browsers your audience prefers. Knowing this helps identify which browsers to use for testing your website. Some coding solutions may also need to be altered. For example, if 60% of visitors are still using Internet Explorer 6, you might need to rethink some of those fancy CSS techniques. So let's look into utilizing Google Analytics to figure out which browsers are being used. [Continue reading]

Excluding Website Traffic Based on an IP Address in Google Analytics

Tracking the usage of your website can be very insightful. It helps identify which pages are the most popular, shows where your visitors are from, what browsers they use, etc. However, if you're visiting the website all the time to verify things are working, updating pages, etc. it can throw off the stats. So unless you're trying to pad the numbers, let's look at filtering out results by IP address in Google Analytics. [Continue reading]

Why You Should Utilize Separate Accounts when Managing Multiple Websites

When setting up accounts for your job with services like web hosting, Google Analytics, etc., don't be tempted by the "it will be convenient for me now" argument. If you manage multiple websites, it may be easier to have all your Google Analytics reports under one Google account for example, but what if you need to split those websites up for some reason? If another developer takes over a website, will it be easy to turn over the keys? As in turning over the source files and account information…not emotionally. For that, you might need counseling. [Continue reading]

Problems with Google Analytics Code for Tracking PDFs

After struggling for nearly a week to install the asynchronous code for Google Analytics, I thought it would be good to share what I've learned. Hopefully this will save you a few sleepless nights.

When I switched to the asynchronous tracking code, everything appeared to be working correctly. But for some reason PDF downloads and visits to external websites weren't showing up in the analytics. According to the Google help documentation (How do I track files (such as PDF, AVI, or WMV) that are downloaded from my site?), I just needed to add an "onclick" event to the downloadable file/external website link:

<a href="/newsletter/2010spr.pdf" onclick="javascript:pageTracker._trackPageview('/newsletter/2010spr.pdf');">

While searching for a solution I stumbled across a Google Chrome plug-in (Google Analytics Tracking Code Debugger) used to validate your tracking code. Using the plug-in, I discovered that the Google help documentation failed to mention one crucial piece of information; you need to define "pageTracker". After a little searching I found another page (Google Analytics Installation Guide) that talks about defining "pageTracker".

With "pageTracker" now defined, the Google Chrome plug-in said that everything was working. But for some reason the code still wasn't working. I used several computers to open PDFs and visit external links. Then I checked Google Analytics an hour or so later to see if they show up. For some reason one of the clicks went through, but that was it. After a few more days of failures and posting on the Google Analytics discussion board I finally realized that the tracking code provided by Google doesn't work.

Instead you want to call the tracking code directly:

<a href="/newsletter/2010spr.pdf" onclick="javascript:_gaq.push(['_trackPageview', '/newsletter/2010spr.pdf']);">

I have no idea why the Google Chrome plug-in said everything was fine, maybe there is a bug on Google's side that prevents the analytics service from accepting results.

Related Links

Future Proofing Your Google Analytics Code for Tracking PDFs

A few years back I started using Google Analytics to get a better idea of what people are viewing on our websites. To be prepared for future revisions of the tracking code, I thought all I needed to do was store the code in a file called "GoogleAnalytics.html" and import it into the pages we wanted to track. Then as Google releases new versions of the tracking code, I would only need to update the GoogleAnalytic.html file for each website.

Unfortunately, you can't add the tracking code to every file on the website. To track how many visitors open a PDF for example, you have to add extra code to the page that links to the PDF. Let's say you wanted to link to the Spring 2010 newsletter. The tracking code would likely be added to the anchor tag using the "onclick" attribute as follows:

<a href="/newsletter/2010spr.pdf" onclick="javascript:urchinTracker('/newsletter/2010spr.pdf');">Spring 2010 Newsletter</a>

The problem was that I didn't think about future proofing this part of the tracking code. So when the time arrived to update the analytics code, I needed to replace more than 230 onclick references throughout one website.

So instead of hard-coding the onclick attribute like before, I wanted to create a PHP function that writes the tracking code for me. In addition to being able to create tracking code for PDFs, Word documents, PowerPoint files, etc., the function should be able to handle links to other websites.

Building the Function

First I defined a PHP function called "addTrackingCode". The function accepts one argument ($link) which is used to send the link to track.

//FUNCTION USED TO ADD TRACKING CODE TO DOWNLOADABLE FILES (SUCH AS PDFs) AND TO EXTERNAL WEBSITE LINKS
function addTrackingCode($link) {
}

Inside the function, I want optimize the link for Google Analytics.

//FUNCTION USED TO ADD TRACKING CODE TO DOWNLOADABLE FILES (SUCH AS PDFs) AND TO EXTERNAL WEBSITE LINKS
function addTrackingCode($link) {
     //IF THE LINK LEADS TO MY WEBSITE (http://www.mywebsite.com/newsletter/2010spr.pdf), REMOVE THE "http://" AND THE DOMAIN NAME
     if(substr($link, 0, 24)=='http://www.mywebsite.com') {
          $link = substr($link, 24);
          
     //ELSE…IF THE LINK STARTS WITH "http", IT'S AN EXTERNAL LINK
     } elseif(substr($link, 0, 4)=='http') {
          //IF THE LINK STARTS WITH "https://", REMOVE IT
          if(substr($link, 0, 8)=='https://')  {
               $link = substr($link, 8);
          
          //ELSE…IF THE LINK STARTS WITH "http://", REMOVE IT
          } elseif(substr($link, 0, 7)=='http://') {
               $link = substr($link, 7);
          }
          
          //ADD "/external/" TO THE BEGINNING OF THE LINK SO THAT ALL THE EXTERNAL LINKS ARE GROUPED INTO THE SAME FOLDER IN GOOGLE ANALYTICS
          $link = '/external/' . $link;
     
     //ELSE…IF THE LINK DOESN'T LEAD WITH A SLASH, ADD ONE (note that if the link doesn't begin with a slash, Google Analytics groups it under a folder labeled "/../")
     } elseif(substr($link, 0, 1) != '/') {
          $link = '/' . $link;
     }
}

Now we need to return the "onclick" attribute containing the tracking code for the formatted link.

     …
     } elseif(substr($link, 0, 1) != '/') {
          $link = '/' . $link;
     }
    
     //RETURN THE FORMATTED LINK
     return " onclick=\"javascript:urchinTracker('$link');\"";
}

Where Does the Function Go?

Since the standard Google Analytics code is already being imported, I added the function to the "GoogleAnalytics.html" page:

<?php
//STANDARD GOOGLE ANALYTICS TRACKING CODE (note that the "UA-XXXXXXX-X" needs to be replaced with your tracking code ID)
?>
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-XXXXXXX-X";
urchinTracker();
</script>
<?php
//FUNCTION USED TO ADD TRACKING CODE TO DOWNLOADABLE FILES (SUCH AS PDFs) AND TO EXTERNAL WEBSITE LINKS
function addTrackingCode($link) {
     //IF THE LINK LEADS TO MY WEBSITE (http://www.mywebsite.com/newsletter/2010spr.pdf), REMOVE THE "http://" AND THE DOMAIN NAME
     if(substr($link, 0, 24)=='http://www.mywebsite.com') {
          $link = substr($link, 24);
          
     //ELSE…IF THE LINK STARTS WITH "http", IT'S AN EXTERNAL LINK
     } elseif(substr($link, 0, 4)=='http') {
          //IF THE LINK STARTS WITH "https://", REMOVE IT
          if(substr($link, 0, 8)=='https://')  {
               $link = substr($link, 8);
          
          //ELSE…IF THE LINK STARTS WITH "http://", REMOVE IT
          } elseif(substr($link, 0, 7)=='http://') {
               $link = substr($link, 7);
          }
          
          //ADD "/external/" TO THE BEGINNING OF THE LINK SO THAT ALL THE EXTERNAL LINKS ARE GROUPED INTO THE SAME FOLDER IN GOOGLE ANALYTICS
          $link = '/external/' . $link;
     
     //ELSE…IF THE LINK DOESN'T LEAD WITH A SLASH, ADD ONE (note that if the link doesn't begin with a slash, Google Analytics groups it under a folder labeled "/../")
     } elseif(substr($link, 0, 1) != '/') {
          $link = '/' . $link;
     }
    
     //RETURN THE FORMATTED LINK
     return " onclick=\"javascript:urchinTracker('$link');\"";
}
?>

Using the Function

Now that the function is imported along with the standard Google Analytics code, it can be used when linking to files like PDFs. So instead of hard-coding the tracking code like earlier:

<a href="/newsletter/2010spr.pdf" onclick="javascript:urchinTracker('/newsletter/2010spr.pdf');">Spring 2010 Newsletter</a>

We can now utilize the PHP function:

<a href="/newsletter/2010spr.pdf"<?php echo addTrackingCode('/newsletter/2010spr.pdf'); ?>">Spring 2010 Newsletter</a>

Updating the Analytics Code

Now that all the Google-specific tracking code is located in the "GoogleAnalytics.html" page, we can easily modify the code as Google releases new versions. We just need to replace the standard tracking code and the return statement at the end. The code below shows the newest tracking code provided by Google:

<?php
//STANDARD GOOGLE ANALYTICS TRACKING CODE (note that the "UA-XXXXXXX-X" needs to be replaced with your tracking code ID)
?>
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
<?php
//FUNCTION USED TO ADD TRACKING CODE TO DOWNLOADABLE FILES (SUCH AS PDFs) AND TO EXTERNAL WEBSITE LINKS
function addTrackingCode($link) {

     //RETURN THE FORMATTED LINK
return " onclick=\"javascript:_gaq.push(['_trackPageview', '$link']);\"";
}
?>

Feedback

Do you use Google Analytics to track visits to external websites from your website? If so, do you do anything special to group those visits together? Also, don't hesitate to ask if you have any questions about the analytics function or if you have suggestions.

Related Resources

  • PHP.net – the online PHP manual which is helpful for finding out more information on the PHP substr() function for example
  • Google Analytics Help – the help section for Google Analytics which may be able to answer your questions about the service