Inject Google analytics code in HTML pages

To update your HTML/JSP/PHP pages with the google analytics you can
use UNIX sed command and inject the two scripts provided for your
website by google analytics. Below is the code. This code replaces
</body> tag with the scripts and </body> tag.

find ./ -type f -iname "*.htm" | xargs sed -i '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"> try { var pageTracker
 = _gat._getTracker\("UA-XXXXXXX-X"\); pageTracker._trackPageview\(\);
 } catch\(err\) {}<\/script> <\/body>/g'

Comments

Leave a Reply