Google Analytics Page


Here I will present 3 ways how to get Apache to automatically insert the Google Analytics script into everypage on the fly using Apache. This will cause every webpage to get sent to this perl script. This way you don't need to edit any HTML, just add the perl script and every requested HTML page will include the correct code block for tacking users. If there are any performace issues you should use mod_perl
Here is a list of things you'll need before starting:
Method 1 (Not compatible if you want your html code to also be parsed by PHP or something similar.)

How that we have the setup needed here is how to make it work:

  1. Add the following perl (footer.pl) file to your cgi-bin and correct (to represent your google analytics account) the following line:
    _uacct = "UA-000000-0";
    You can download footer.pl here: Download
    The Perl script does the following things:
    1. Reads the HTML file requested by the user through Apache.
    2. Finds the best spot to insert the footer and inserts it.
    3. Prints the headers and html.
    Make sure to 'chmod +rx footer.pl' so apache can execute it.

  2. Add the following lines to your apache.conf file:
    Action add-footer /cgi-bin/footer.pl 
    AddHandler add-footer .htm .html
    You must include a trailing space after the Action command or the path won't get sent correctly to the script.
    More help on handlers can be found on Apache's website.


Method 2 (This method won't interfere with anything else and you can add things to the Perl script at a later time.)
  1. Add the following perl (footer.pl) file to your cgi-bin and correct (to represent your google analytics account) the following line:
    _uacct = "UA-000000-0";
    You can download footer.pl here: Download
    The Perl script does the following things:
    1. Reads the file from STDIN that was passed through Apache.
    2. Finds the best spot to insert the footer and inserts it.
    3. Prints the html back to Apache.
    Make sure to 'chmod +rx footer.pl' so apache can execute it.

  2. Add the following lines to your apache.conf file:
    LoadModule ext_filter_module modules/mod_ext_filter.so
    ExtFilterDefine insert-footer cmd="/usr/bin/perl /cgi-bin/footer.pl"
    AddOutputFilter insert-footer htm html
    
    You might need to correct the paths to make it work for you. More help on filters can be found on Apache's website.


Method 3 (The fastest and simplest but adds a lot of text to your Apache configuration)
  1. Add the following lines to your apache.conf file:
    LoadModule ext_filter_module modules/mod_ext_filter.so
    ExtFilterDefine insert-footer cmd="/bin/sed s/<\/html>/the_footer<\/html>/ig"
    AddOutputFilter insert-footer htm html
    
    You might need to correct the paths to make it work for you. You also have to replace $footer by the footer code google provides you. More help on filters can be found on Apache's website.


Any comments can be sent to Kirill Minkovich --- cory_m@cs.ucla.edu