jump to navigation

Installing Google Analytics on a Linux hosted Website March 24, 2011

Posted by Tournas Dimitrios in Uncategorized.

Google Analytics is the enterprise-class web analytics solution that gives you rich insights into your website traffic and marketing effectiveness. Powerful, flexible and easy-to-use features now let you see and analyse your traffic data in an entirely new way. With Google Analytics, you’re more prepared to write better-targeted ads, strengthen your marketing initiatives and create higher converting websites.

If your website pages are created dynamically with a technology such as PHP or ASP, you can include the tracking code in a separate file that gets included as part of the overall page template. To see a sample implementation on a PHP page, visit the Analytics Help article . Have you ever needed to install Google Analytics  on all static HTML files on your website ? If you have a large website that can be a lot of work so you have a few options.

  1. You can have the apache webserver install it automatically on the fly http://ballade.cs.ucla.edu/~kirill/analytics.html The downside here is that this requires CPU power and can slow things down a bit if you are not using modperl
  2. Search and replace all instances of the body tag with your analytics tag. You could use perl or sed on the unix command line to do this. Some ideas here
  3. Or just use this script from http://gleamynode.net/articles/2230/

#!/bin/sh -e
# inject-google-analytics


TRACKER_CODE="<script type="\&quot;text/javascript\&quot;"><!--mce:0--></script>
   var gaJsHost = ((\"https:\" == document.location.protocol) ? \"https://ssl.\" : \"http://www.\"); document.write(unescape(\"%3Cscript src='\" + gaJsHost + \"google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E\"));
// ]]></script>
<script type="\&quot;text/javascript\&quot;"><!--mce:1--></script>
   var pageTracker = _gat._getTracker('$TRACKER_ID'); pageTracker._trackPageview();
// ]]></script>"

cd `dirname $0`
find -name '*.html.new' -delete
find -name '*.html' | egrep -v -- '-frame.html$' | while read -r TARGET; do
if grep -qi "
if grep -qi "$TRACKER_ID" "$TARGET"; then

echo "Injecting: $TARGET"

case `grep -i '' "$TARGET" | wc -l` in
cat "$TARGET" | sed "s:\\(&lt;\\/BODY&gt;\\|&lt;\\/body&gt;\\):`echo $TRACKER_CODE | sed 's:;:\\\\;:g' | sed 's/:/\\\\:/g'`\\1:gi" &gt; "$TARGET.new"
mv -f "$TARGET.new" "$TARGET"
cat "$TARGET"
} &gt; "$TARGET.new"
mv -f "$TARGET.new" "$TARGET"

Usage Tips :

  • this will search and insert Google Analytics code for ALL .html files so if you are in the master directory with multiple websites below be careful.
  • Set your filetype to unix if you are uploading this script  otherwise you will get errors
  • Be sure to put in your own analytics ID


1. Kunz - July 18, 2013

I just such as the useful facts you present for the posts. Let me search for your blog along with take a look at once again right here regularly. We are relatively ‘ will find out plenty of innovative information good right here! All the best ! for the!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s