I find it annoying to paste in the Google code for the tracker at the end of each page.

Is there a way to have Apache deliver the <script> with each *.htm and *.php file downloaded?

link|improve this question

60% accept rate
feedback

2 Answers

This is certainly possible via Apache. You can use the add-handler to add an action for all .htm or .php file on a per server, per directory or even in .htaccess.

Action add-footer /path/to/footer.cgi
AddHandler add-footer .htm

Where footer.cgi is a script which will add the footer to the file.

You can also do this within the php processing (although it would then only work for php files) using

php_value auto_append_file /path/to/footer.php

This will add the code in footer.php to your PHP code.

link|improve this answer
It doesn't actually matter where the GA code is executed; browsers will execute JavaScript placed after </html>, even. – yahelc Feb 16 '11 at 19:14
Ah ha, I didn't try it, but sounds right. – Dave Drager Feb 16 '11 at 20:02
1  
@yc01 of course dropping a script block outside the main html block wouldn't be what you'd call valid (x)HTML. – Jeff Albert Feb 17 '11 at 1:28
feedback

We solved this problem a year ago by writing a module. It looks like someone else did this as well: https://github.com/dragon3/mod_google_analytics.

This is especially useful if you are front-ending an application you can't modify or something in tomcat you don't want to tweak.

After looking at that module, it seems like they don't check for text/html.. which might mean the module could trash your binary content if you don't use AddOutputFilterByType .. text/html. Be warned!

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.