Download!Download Point responsive WP Theme for FREE!

Host Universal Analytics.js Locally

To save some milliseconds I’ve been sparing my visitors a DNS lookup by fetching the ga.js file, on which Google Analytics is dependent. I host and serve the file locally, and I run a daily cron job to execute a script that downloads the latest ga.js file from Google so that if they update it somehow I have a fresh copy. That and other handy things were made by Mr. Charles Torvalds (website, G+).

Google Analytics is now migrating to Universal analytics (google it if you’re interested, and follow Google’s steps to switch to the new system before proceeding here). Let me now try to be the first to attempt to offer you the instructions I had used (crediting Charles Torvalds one more time, all I did was tweak his code a bit) to host Google’s javascript locally, though revised slightly for the new Analytics javascript file and code snippet. Though if this stuff below looks too complicated for you to bother, I do recommend that you go into Google Analytics and switch yourself to this new Universal rig.

1) Updating script, cron job:
In your document root, make a directory (/var/www/yourwebroot/ga/ for example) where the .js will be stored. Create a script, call it analyticsupdate.sh, put it in /usr/bin/ (or wherever) with the following (but modified for your own configuration):

/bin/sh

# TMP DIRECTORY
MYTMP=/tmp/

# SAVE analytics.js HERE
INSTALL_IN=/var/www/yourwebroot/

# RESOURCE URLS
GOOGLE_GA_URL=http://www.google-analytics.com/analytics.js

# USER-AGENT
UA="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"

# CD TO TMP DIRECTORY
cd $MYTMP

# DOWNLOAD THE FILE
curl --header "Pragma:" -f -s -A "${UA}" -m 1800 --retry 15 --retry-delay 15 --max-redirs 8 -O $GOOGLE_GA_URL

# GIVE FILE CORRECT PERMISSIONS
chmod 644 $MYTMP/analytics.js

# COPY FILE TO SITE DIRECTORY
cp -r $MYTMP/analytics.js $INSTALL_IN

# RETURN TO OLDPWD
cd $OLDPWD

exit 0;

 

Save and chmod the script +x, then test the script (there should be no terminal output), make sure it drops an analytics.js in your /webroot/ga/ directory, should be roughly 30KB. Make sure the file is readable from the web, and if not, chmod / umask accordingly and check again. If it works, add this line to cron:

@daily /usr/bin/analyticsupdate.sh >/dev/null 2>&1

 

2) Your site’s global header
Now, in your site’s header file, back up your existing header, then edit your live header, comment out your existing Google Analytics script and lay this in, substituting the Xs with your site’s GA profile and your domain in the two spots:

<!--  LOCAL GOOGLE UNIVERSAL ANALYTICS JS -->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//yourdomain.com/ga/analytics.js','ga');
  ga('create', 'UA-XXXXXX-X', 'yourdomain.com');
  ga('send', 'pageview');
</script>
  <!-- END LOCAL GOOGLE UNIVERSAL ANALYTICS JS -->

3) Check again that it’s working.
Save, clear cache if needed and watch your Google Analytics live feed to see if it appears to work or if your traffic drops off over the next half hour. The next day, check your Google Analytics to make sure the data seems to exist, check your webroot/ga/ directory to see that your script worked and that your analytics.js file has a fresher timestamp. If it does, maybe remove the old Analytics code you commented out and enjoy your saved DNS lookup plus whatever this Google Universal Analytics thing is. By the way, for you RSS junkies, here’s a good one to add.

Doug Simmons

5 Comments