Generic Scripts to add Google Analytics code to HTML pages

Before I start, this tip is Unix friendly (not just OSX), but requires you to know what shell scripts are and how you create/run them. Additionally you should be familiar with the workings of the “find” command in Unix.

I had lots of trouble getting the Google Analytics code onto my gallery site. The problem is that I use iWeb to create a front end that links to a discrete back end (ie: specific subdirectories generated by Photoshop, iPhoto or Aperture). I found this Automator script earlier, but it seems like every time I run the script on a Folder, the script only changes .html pages created by iWeb….Weird. So after some head scratching and googling, I found the following complementary scripts on RSVP – Xnews site. There is a certain amount of detail about what the script is actually doing on that site, but I just wanted to extract the meat and add a little garnish (yeah I made a couple of mistakes, that I hope you’ll avoid).

  • First you need a new Analytics account or if you have one (with a existing profile) you might need to create a new profile for this new site you want to track. The mistake I made was that I had a existing account that tracks nerdlogger.com and I (by mistake) used it’s analytics ID in the script. Since my gallery site is a different domain, analytics creates a new ID when you add the domain (it actually increments the last digit).
  • Then you need to create a script called insert (or whatever you like) and put this in it (just cut and paste from here):
#!/bin/bash

#INSERT SCRIPT
#Your Google Analytics Code goes below.
googleAnalyticsCode='UA-XXXXXXX-2'
textToInsert="<script src=\"http:\/\/www.google-analytics.com\/urchin.js\" type=\"text\/javascript\"><\/script><script type=\"text\/javascript\">_uacct = \"$googleAnalyticsCode\";urchinTracker();<\/script>"
textToReplace="<\/[Bb][Oo][Dd][Yy]>"
#You need to substitute the path to the top of your webdirectory below.
WebPath='/Volumes/idiskname/Web/Sites'

# this is where the actual work happens
find $WebPath -iname '*.html' -exec sed -i .bak -e "/$textToInsert/!s/$textToReplace/$textToInsert&/g" {} \; -print
  • Now create another script called remove (or whatever you like) and put this in it (just cut and paste):
#!/bin/bash

#REMOVE SCRIPT
#Your Google Analytics Code goes below.
googleAnalyticsCode='UA-XXXXXXX-2'
textToRemove="<script src=\"http:\/\/www.google-analytics.com\/urchin.js\" type=\"text\/javascript\"><\/script><script type=\"text\/javascript\">_uacct = \"$googleAnalyticsCode\";urchinTracker();<\/script>"
#You need to substitute the path to the top of your webdirectory below.
WebPath='/Volumes/idiskname/Web/Sites'

# this is where the actual work happens
find $WebPath -iname '*.html' -exec sed -i .bak -e "s/$textToRemove//g" {} \; -print
  • Now we’re almost there. Create one last script called delback (or whatever) and put the folowing one liner in it:
#DELBACK SCRIPT
#You need to substitute the path to the top of your webdirectory below.
find /Volumes/idiskname/Web/Sites -iname '*.bak' -exec rm {} \; -print

At this point you should have three scripts insert, remove and delback. Use insert to insert the code into all the HTML files under a certain path ($WebPath). This will create .bak files and once you’ve verified the insert scripts operation you can delete/clean them using the delback script. Use remove to remove the analytics code from your HTML pages (if you decide later that you don’t like google analytics or something). Again this process creates .bak files that can be removed/cleaned using the delback script.

Keep in mind also that if you use iWeb to generate your pages and they are sitting on a OSX server that by default your web addresses get expanded after the browser requests them (ie: My gallery is http://www.rcc.ryerson.ca/~mayromlo but gets expanded and rewritten as http://www.rcc.ryerson.ca:16080/~mayromlo/Site/Welcome.html). So you need to get google analytics to go to the expanded version by editing the profile information after initial entry and changing the website URL. This last issue is very mac/osx specific.

5 thoughts on “Generic Scripts to add Google Analytics code to HTML pages

  • Shaithus

    Well, your code gave me a starting point, and I found a few others with partially working examples, but unfortunately google has updated their tracking code. I tried quite a few methods, but regardless they were unhappy with the plethora of bad symbols inside the regex. Here’s what I finally got to work. It recursively adds the tracking code before the /body tag to every html page under the current dir you run it from. No backup files generated, and if you do it twice it would add the code twice. So be careful!

    The only difference between my code and the code that google suggests is that I changed the ‘ characters inside the javascript unescape to %27 (which unescaped turns into ‘) I’ve run this for my site and it works perfectly. Just change the account # to yours.

    You can safely follow the link. It just goes to a site that allows easy pasting of source code. No ads/viruses/etc. I couldn’t post source code directly into this window because the body tag made it blow up.

    http://rafb.net/p/qjplRf77.html

    Enjoy!

  • Many

    Hi Shaitus,

    Thanks for the headsup and the updated code. If I get a chance I might fuse the two together and post something back. To be honest I haven’t done anymore analytics scripting since I use flickr and blogspot now.

    TTYL
    Many

Leave a Reply