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.

Table of Contents

  1. Introduction
    1. XHTML for hosting ga.js locally
    2. Shell Script to Update ga.js
    3. Updating ga.js with Crontab
    4. More Speed Tips

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.



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

  • mod_rewrite Fix for Caching Updated Files
  • Speed up your site with Caching and cache-control
  • Top methods for Faster, Speedier web sites

Speed Up Google Analytics, use local ga.js - AskApache

Charles Torvalds
30 Jan 2008

  • Site Map WireShark GNU Non-GNU Tor Project cURL TLDP - Documentation
  • Htaccess Files Hacking Htaccess Javascript Linux Optimization PHP Security Shell Scripting WordPress
  • Base64 Image Converter Raw HTTP Header Debugger Graphical ASCII Text Generator Mac Address Vendor Lookup Who Am I – Your IP Information Request Method Security Scanner .htpasswd file Generator Compress CSS DNS Tracer
Copyright © 2025 AskApache
  • Site Map
  • Htaccess Files
  • Hacking
  • Htaccess
  • Javascript
  • Linux
  • Optimization
  • PHP
  • Security
  • Shell Scripting
  • WordPress
  • Base64 Image Converter
  • Raw HTTP Header Debugger
  • Graphical ASCII Text Generator
  • Mac Address Vendor Lookup
  • Who Am I – Your IP Information
  • Request Method Security Scanner
  • .htpasswd file Generator
  • Compress CSS
  • DNS Tracer
Exit mobile version