views:

123

answers:

3

I have a directory full of hundreds, if not thousands, of html and PHP files that I'm hosting for a client. They currently include no google analytics tracking tags, but we'd like to add them -- ideally without actually modifying the files themselves.

What's the best way to do this?

I'd imagine I'd want to redirect all requests for *.html files to a PHP script, and have that script return the content of the relevant file with analytics tags added. But that doesn't work as well for the PHP files -- I'd need apache to render the files normally, and then pass the output to my script.

Would mod_actions be helpful? Is mod_actions compatible with mod_php?

+4  A: 

I hate to come back with an answer to my own question so quickly, but it looks like PHP's auto_append_file directive will probably do what I want.

Frank Farmer
I'd use auto_prepend_file aswell to start output buffering, in the file you are appending you close the buffering and insert your analytics snippets right before </body> with str_replace()
NA
Good call. If you submit that as an answer, I'll gladly accept it (rather than accepting my own answer)
Frank Farmer
+1  A: 

Here's a way to get a footer on every page, html or php or both... using PERL

http://stein.cshl.org/WWW/docs/handout.html#Adding%5Fa%5FCanned%5FFooter%5Fto%5FEvery%5F

randy melder
A: 

I ended up using something like the following:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/html/example.com
    # Add google analytics to .php pages
    php_value auto_append_file "/var/www/html/x.example.com/myFooter.php"
    # run html pages through php so the same auto_append gets applied
    AddHandler application/x-httpd-php .html
</VirtualHost>

Found the AddHandler ref here. AddHandler php-handler did not work. Originally, I used a rewrite rule to pump all .html requests to a PHP script, but that broke things like requests for directories (which actually request index.html).

Frank Farmer