FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home » Google » Speed Up Google Analytics, use local ga.js

by 9 comments


Google Analytics ga.js SpeedLast year I wrote about hosting Google Analytics urchin.js file on your own server, to speed up the loading of your site. Well, google has migrated to a new analytics javascript, ga.js. You can read the urchin.js to ga.js migration guide if you want. They also wrote about the changes on the Google Analytics Blog. And if you want to learn about advanced features of ga.js check out this Google Analytics Documentation.


XHTML for hosting ga.js locally

If you use a relative link like below, you don't need the https detection part of the script.

Also note that the -151 is from my advanced mod_rewrite Fix for Caching Updated Files. So you won't need that unless you implement that technique.

<script src="/j/ga-151.js" type="text/javascript"></script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-732153-7");
pageTracker._initData();pageTracker._trackPageview();
</script>

Shell Script to Update ga.js

Here is an updated shell script that fetches the latest ga.js file from google and places it in the folder of your site.

#!/bin/sh
 
# TMP DIRECTORY
MYTMP=/tmp/
 
# SAVE ga.js HERE
INSTALL_IN=/web/user/askapache.com/z/j/
 
# RESOURCE URLS
GOOGLE_GA_URL=http://www.google-analytics.com/ga.js
 
# USER-AGENT
UA="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"
 
# 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/ga.js
 
# COPY FILE TO SITE DIRECTORY
cp -r $MYTMP/ga.js $INSTALL_IN
 
# RETURN TO OLDPWD
cd $OLDPWD
 
exit 0;

Updating ga.js with Crontab

Just add this to your crontab by typing crontab -e where google-analytics-update.sh is the location of your shell script.

@daily /web/user/scripts/google-analytics-update.sh >/dev/null 2>&1

More Speed Tips

Tags

Comments Welcome

AskApache is an FSF Contributing Member



My Online Tools
Popular Articles


Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman









[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | htaccess.io

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service | @Htaccess

↑ TOPMain