Oct
23

Speed up Google Analytics using simple PHP Script

Filed Under (Programming) by Joyce on 23-10-2007

Update:  Thanks to the wonder idea by Carl Mercier, the script is now available as a WordPress pluging. Please try Local Analytics.

Google Analytics is one of the best web traffic analysis service available. The best thing about Google Analytics is that it is FREE, while most  of the similar services requires a premium membership for such detailed reports. Microsoft announced starting their own Web Analytics service codenamed

Back to our topic. Recently I came across an article in JonLee.ca on speeding Up Google Analytics by hosting urchin.js it locally. Google support says that it is allowed, though not recommended, to host urchin.js locally. Google recommends directly linking to the file hosted on their server, because it will ensure that your users are downloading the latest version of the file.

A work around to this proposed by the above site and many other blogs is to setup a cron job to automatically update the file at regular intervals. But most of the sites does not provide any information on how to do that. Also not all webhosts support crontab. When a gzip enabled browser requests for the file, Google compresses the file before sending theirby reducing the file size from 21KB to 6KB. If your server is not configured to automatically compress all Javascript files, then this can increase the file download time.

Hence I have created a simple php script that will automatically download the script from Google Server at regular intervals and provides your users with latest version of urchin.js. The script also supports Gzip compression by default, though it can be turned off by changing $useGzip to false.
Here is the script:

< ?php

// Remote file to download
$remoteFile = 'http://www.google-analytics.com/urchin.js';
// Local File name. Must be made writable
$localFile = "local-urchin.js";
// Time to cache in hours
$cacheTime = 24;
// Connection time out
$connTimeout = 10;
// Use Gzip compression
$useGzip = true;

if($useGzip){
     ob_start('ob_gzhandler');
}

if(file_exists($localFile) && (time() - ($cacheTime * 3600) < filemtime($localFile))){
     readfile($localFile);
}else{
     $url = parse_url($remoteFile);
     $host = $url['host'];
     $path = isset($url['path']) ? $url['path'] : '/';

     if (isset($url['query'])) {
          $path .= '?' . $url['query'];
     } 

     $port = isset($url['port']) ? $url['port'] : '80';

     $fp = @fsockopen($host, '80', $errno, $errstr, $connTimeout ); 

     if(!$fp){
          // On connection failure return the cached file (if it exist)
          if(file_exists($localFile)){
               readfile($localFile);
          }
     }else{
          // Send the header information
          $header = "GET $path HTTP/1.0\r\n";
          $header .= "Host: $host\r\n";
          $header .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6\r\n";
          $header .= "Accept: */*\r\n";
          $header .= "Accept-Language: en-us,en;q=0.5\r\n";
          $header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n";
          $header .= "Keep-Alive: 300\r\n";
          $header .= "Connection: keep-alive\r\n";
          $header .= "Referer: http://$host\r\n\r\n";

          fputs($fp, $header);

          $response = '';
          // Get the response from the remote server
          while($line = fread($fp, 4096)){
               $response .= $line;
          } 

          // Close the connection
          fclose( $fp );

          // Remove the headers
          $pos = strpos($response, "\r\n\r\n");
          $response = substr($response, $pos + 4);

          // Return the processed response
          echo $response;

          // Save the response to the local file
          if(!file_exists($localFile)){
               // Try to create the file, if doesn't exist
               fopen($localFile, 'w');
          }
          if(is_writable($localFile)) {
               if($fp = fopen($localFile, 'w')){
                    fwrite($fp, $response);
                    fclose($fp);
               }
          }
     }
}

?>

 Installing the Script

Installing the script is extremely simple. Save the above code in a php file, say analytics.php or download it from here. Edit your Google Analytics code and change http://www.google-analytics.com/urchin.js to your new file path, say http://www.yourdomain.com/analytics.php.

Now your code will be something like this


Use this code instead of your original Google Analytics code.

After uploading analytics.php to the server, directly access the file using your web browser. This will create a local copy of urchin.js, named local-urchin.js, on your server. Make sure that local-urchin.js is writable (chmod to 777).

Important: If your server is using the zlib compression for compressing javascript files, then turn off gzip compression by changing line no. 11 to :

$useGzip = true;

Advantages of using the script

  • Your webpage loads faster, since your user’s browser can use the existing connection with your web server to download the file and doesn’t need to create a new connection to google’s server.
  • Sometimes, though very rarely, google servers become overloaded and your page load time will be affected drastically.
  • Some ad blockers are now blocking Google Analytics too. This script will be a work around to that too.
  • Unlike other methods, the file is compressed before sending to gzip enabled browsers.
    Read More   
11 Comments »
Subscribed to comments via emailCarl Mercier on 28 October, 2007 at 11:55 am #

This is an amazing idea! I think I’ll have to do that for all my sites. If somebody could turn that into a Wordpress plugin, it would make our life a bit easier!

Thanks for the code, Joyce.

Jon Lee on 28 October, 2007 at 12:23 pm #

Man nice stuff, this is very useful!
I agree, a Wordpress plugin would be useful. I’d be happy to make it if you let me steal your code!

Subscribed to comments via emailCarl Mercier on 28 October, 2007 at 12:27 pm #

Hope you can steal it ;-)

Ideally, the plugin would allow me to enter my Analytics key and would automatically insert the required Javascript code somewhere in my WP pages, saving me from editing my theme.

There’s about a million WP/Analytics plugins so I guess you should be able to re-use an existing one without reinventing the wheel!

 
 
 
Joyce on 29 October, 2007 at 2:23 am #

Thanks for the appreciation. :grin:
@Carl - That is a good idea. After reading ur comment I have started creating one. It is almost 90% complete and has more configurable options.

@Jon - If u want to use the code feel free to use the code anyway you want. No need to steal it, its yours :wink:

 
Karan on 29 November, 2007 at 12:11 pm #

I love your site. I found your blog via Google while searching for honeywell home security systems and your post regarding nnial 2007 - salvatore iaconesi - del.icio.us poetry looks very interesting to me. It really looks very nice. The articles provided are long enough to provide great content but not so long as to be totally engrossing, if you know what I mean.

 
Lane Campbell on 5 February, 2008 at 12:16 am #

Wow! This is brilliant! Google should be in contact with you to implement this solution as an official one!

My load times for my site are now in the 0.xx instead of the x.xx thanks to this script!

 
Ric on 15 April, 2008 at 2:12 pm #

Nice idea, but what about the new tracking code too?

 
 
 
 
 
Post a Comment
Name (*)

E-mail (*)

URI
OR
OpenID
DoFollow Enabled
Your Comment ( smaller size | larger size )

You may use <a> <abbr> <acronym> <b> <blockquote> <code> <em> <i> <strike> <strong> in your comment.