Add local caching of urchin.js

quicksketch - August 22, 2007 - 04:20
Project:Google Analytics
Version:5.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:budda
Status:closed
Description

The urchin.js file may be cached locally on a webserver to avoid making a request to the (often slow) Google Analytics servers. Keeping the file request on the same domain also avoids DNS lookups for the browser and if your Apache install has enabled it, uses an existing connection to the server rather than making a new one. Overall, it can be a huge page load-time increase for people using GA.

This patch adds an option to this module that present a simple checkbox which enables caching. If checked, Drupal checks for a local copy of urchin.js. If it is missing, it makes an http request to Google to retrieve the file and save it locally. It then uses the local path to add the javascript to the page. The urchin.js file is deleted on a daily basis to keep it up to date with any possible changes to the js file by Google.

This is a fairly common practice and there are many tutorials on setup using cron, such as http://www.askapache.com/webmaster/faster-google-analytics-with-a-local-...

Official Google position on hosting locally: http://www.google.com/support/analytics/bin/answer.py?answer=43183&query...

Of course this is only an option so users don't have to enable it.

AttachmentSize
googleanalytics_cache.patch3.49 KB

#1

budda - August 22, 2007 - 09:04

Great idea, but like Google mention, is the urchin.js file not cached locally after its been downloaded once?

I wanted to get the D5 code cleanup patch applied first if possible.

The askapache.com link doesn't work (404).

#2

quicksketch - August 22, 2007 - 17:38

Yes it's cached once it's been downloaded once by the client side browser, however the first request must be made on the initial visit to the page. If a visitor is coming to your site for the first time, this would be the worst time to have the page hang while pulling in analytics code. This can help speed up that initial page load time.

The links works for me, how strange. Anyway, tons of references in Google (this was the 2nd one):
http://www.google.com/search?q=speed+up+google+analytics

#3

budda - August 27, 2007 - 21:23

As urchin.js is pulled in last for page loads (the script is in the footer) I doubt users would notice any slow down as the main page has already loaded and displayed.

I did notice the page loading delay when I used to load the script in the header.

#4

quicksketch - August 30, 2007 - 01:43

True, the contents of the page will not be affected by scripts at the bottom of the page. However, try installing several jQuery dependent modules (Fivestar, jCarousel, External Links). Many of these modules wrap all of their javascript in $(document).ready({ }). Since the document isn't fully loaded until the entire page is loaded (including urchin.js), these modules display their non-javascript versions of content for a long period of time. After urchin.js loads, lists are converted to carousels, external link icons popup, and the fivestar radio buttons are replaced with stars.

If it seems like a questionable option, that's why it's off by default and doesn't need to change on any existing installations of Google Analytics. But this option can make a real difference on sites with tons of javascript content on every page.

#5

quicksketch - August 30, 2007 - 01:50

Updating for latest 5.x

AttachmentSize
googleanalytics_cache_0.patch 3.56 KB

#6

budda - August 30, 2007 - 10:35
Assigned to:Anonymous» budda
Status:needs review» fixed

The jquery delaying is a good point, i've noticed that on a project i'm currently working on (although not related to urchin).

I've rolled the features in to the dev version. Testing on a local scratch site provided the correct results.

#7

quicksketch - August 31, 2007 - 03:39

Woot! Now I can use the module instead of hard coding GA in my theme. Thanks budda!

#8

moshe weitzman@... - August 31, 2007 - 14:14

Wow, cool. I had always assumed that google had to serve this file because it sent data back to its own server (i.e local cache violates same domain security policy). But I guess thats not true so this is a no brainer. Nice job!

#9

Anonymous - September 14, 2007 - 14:21
Status:fixed» closed
 
 

Drupal is a registered trademark of Dries Buytaert.