Adding Google Analytics into a Large Number of Static HTML Pages with Perl
If you've ever dealt with Dreamweaver based templates, then you'll know all it really does it insert static blocks of code wherever the template/library item tags are located. Dreamweaver does a pretty good job at this, but once sites grow to over 1000 files, it's a bit more complicated and time consuming to modify every page easily... especially if the Dreamweaver template files are corrupt or missing.
We were faced with the task of inserting Google Analytics script code at the bottom of every static page... on a site with several thousand *.html pages. The Dreamweaver templates were missing, and honestly we didn't feel like rebuilding them and resituating the site in Dreamweaver. Plus, we don't plan to maintain this site with Dreamweaver in the future.
So Tom setup a quick little perl command to take care of this task for us. It might not be perfect, and I of course assume no responsibility for its use, but it worked great for us!
This should get you started:
perl -p -i -e "s/<\/body>/<script type=\"text\/javascript\">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=\"text\/javascript\"> var pageTracker = _gat\._getTracker\(\"UA-**YOUR_ACCOUNT_NO-HERE**\"\); pageTracker\._initData\(\); pageTracker\._trackPageview\(\); <\/script><\/body>/gi" `find ./ -name \*\.html`
What it does:
This command will search any document with extension html and will find the closing </body> tag. It will then replace the </body> tag with the analytics script code, then add back in a closing </body> tag. Done. Simple. Cool!

This script worked FANTASTIC
This script worked FANTASTIC for Groovy Adventures. This site was what 1.5 gigs almost? Thanks for doing this guys it would have been an absolute nightmare to do it all through Dreamweaver. Half of the templates are broke and busted and it would have taken as long manually to do as it took to reupload this entire website.
Post new comment