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

Google

Comments