Add local caching of urchin.js
| Project: | Google Analytics |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | budda |
| Status: | closed |
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.
| Attachment | Size |
|---|---|
| googleanalytics_cache.patch | 3.49 KB |

#1
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
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
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
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
Updating for latest 5.x
#6
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
Woot! Now I can use the module instead of hard coding GA in my theme. Thanks budda!
#8
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