Perl script to add Google Analytics code to your website pages

Posted by ljmacphee on July 2, 2007 under blogger, how to, perl, tools, useful sites, wordpress |

Google Analytics is yet another free statistic collector for your website. Google Analytics tallies up the data once a day. You’ll get incoming link information, number of visitors, location of visitors and time spent on your site and the pages each visitor viewed.

Analytics will give you more data than most of the free statistics tools out there. But I’m still a fan of StatCounter too. They update data much more often.

I waited a long time to use Google Analytics because you have to enter the JavaScript with your code on every page on your website. Now that TimesToCome is cleaned up and much of it moved to blog format I decided it was time. Still there were about 500 files that needed the code added to them.

So I wrote a small PERL script ( permanent link on top left of page ) to do this. It searches every file in the directory you place it in for ‘</body>’ and adds in the Google Analytics code just before that tag. Read the notes in the script before using it. You’ll need to enter your personal analytics code number to the script.

Of course for your Blogger and Wordpress blogs you need only change the template and enter the code just above the </body> tag. One entry is needed for Blogger. You may have to change index.php, single.php and page.php for Wordpress depending on your theme. Where ever you find </body> in your template files you need to add the script.

  • rchmura said,

    If you like how often statcounter updates, you should try GoStats. The updates are super fast and the amount of data that you can analyze is much larger than the 500 limit which statcounter has.

  • feemurk said,

    Hi There,

    Was interested in your Pearl script - it won’t download (http 404 error).

  • admin said,

    Sorry, I’ve moved all the websites to a new server and it behaves a bit different than the previous one.

    Give me a little while - it looks like none of the Perl scripts are downloading. Will have it fixed soon.

  • admin said,

    OK, I saved them as .txt files, not optimal but I’ve moved 10 websites to a new host this week and there are lots of kinks to work out.

  • MyWebsiteAdviser said,

    You have a nice and useful blog.

    I saw you have a few articles about Coppermine gallery.

    I tried to find how to get web statistics for Coppermine gallery, but didn’t found anything. So I wrote it myself - you can see it here:
    Web Statistics for Coppermine Gallery

    Enjoy,
    Alex Webs.

  • ljmacphee said,

    Thank you.

    I’m sure many webmasters will find that useful.

  • ljmacphee said,

    I received an email from Tom, who sent a nice change to the script. This will let you run the code in test mode to see which files would be changed.

    Run google-code.pl test it will list the files about to change. Run it without test and it will change the files.

    > ===== start paste =====
    >
    > #!/usr/bin/perl
    >
    >
    > my $test = ”;
    > $test = $ARGV[0] if $ARGV[0]; # cmdline google.com.pl test … will
    > show files it will modify.
    >
    > # http://herselfswebtools.com
    > # Linda MacPhee-Cobb
    > # This program will add in the Google Analytics code to every file in
    > THIS directory
    > # that has in it.
    > #
    > # if you used or or something else be sure to change
    > # the line below $oldString = “” to match the case of the body
    > tag used in your files
    > #
    > # YOU MUST change the _uacct = \”ZZ-9999999-9″ to your Google analytics
    > account number
    > # this is a dummy number. See the line that starts $newString it’s in there
    >
    > #
    > # Creative Commons Attribution 3.0 License
    > #
    >
    >
    > # no warranties are given or implied.
    > # use at your own risk
    > # preferably on a copy not your original files.
    > #
    >
    >
    > #
    > # Put this file in the same directory as the files you wish to change
    > # files in this directory will be re-written. The delete or move this
    > # file out of that directory when you are finished
    > #
    >
    > #
    > # use current directory
    > #
    > $directory = “.”;
    >
    >
    > #
    > # out with the old
    > #
    > $oldString = ‘‘;
    >
    >
    >
    > #
    > # In with the new
    > # Change the _uacct to be your Google Analytics account number!!!!
    > #
    > # just copy and paste here the google analytics code given.
    > #
    > $newString = < >
    >
    > EONS
    > chomp($newString);
    >
    > #
    > # last chance to bail before changing files
    > #
    > print “\nDid you change the uacct number? If not bail now!!! y or n: “;
    > $answer = ;
    > chomp($answer);
    >
    > if( $answer eq “y” ) {
    > print( “Altering files now…\n” );
    > } else {
    > die ( “No changes were made, try again!\n” );
    > }
    >
    >
    > #
    > # read current working directory and store file names in @flist
    > #
    > opendir( INDIR, “$directory”) || die (”$0: Can not read directory”);
    >
    > while( $fname = readdir INDIR){
    > next if $fname =~ /^\./; # not hidden
    > next if $fname =~ /^\.\.?$/; # it’s pwd or parent
    > next if $fname =~ “google-code.pl”; # don’t change this file
    > push @flist, $fname;
    > }
    > closedir(INDIR);
    >
    >
    > #
    > # read each file and switch strings if needed
    > # write to a file named temp in current dir
    > # if changed any lines, let user know and copy
    > # temp to correct file name in current directory
    > #
    > foreach $fn (@flist) {
    >
    > open( IN, “$directory/$fn”) || die (”\n can not read $fn \n”);
    > open( OUT, “>temp”) || die (”\n can not write $fn \n”);
    > $flag = 0;
    > $lineNo = 0;
    >
    > while( ){
    >
    > $lineNo++;
    > $line = $_;
    >
    > if ( $line =~ m/$oldString/i ) { $flag = 1 if(!$test); }
    > $line =~ s/$oldString/$newString\n$oldString/i && print ” Changed:
    > $fn :: line $lineNo :: $_ “;
    > print OUT “$line” if(!$test);
    > }
    >
    > close(IN);
    > close(OUT);
    >
    > if($flag){
    > rename (temp, $fn) || die “couldn’t rename temp to $fn.\n”;
    > }
    > }
    >
    >
    >
    >

Add A Comment

You must be logged in to post a comment.