Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import glob
- """ This little script takes all the static *.html files in the directory being executed and inserts the Google Analytics tracker code. The output html files will be in the 'out' directory.
- You must set the UA_CODE from your Google Analytics panel.
- Marcelo Fernández - [email protected]
- License: Public Domain Code
- """
- GA_UA_CODE='UA-00000000-0'
- GA_TRACKER_CODE="""<script type="text/javascript">
- var _gaq = _gaq || [];
- _gaq.push(['_setAccount', '']);
- _gaq.push(['_trackPageview']);
- (function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();
- </script>""" % GA_UA_CODE
- if __name__ == '__main__':
- if not os.path.exists('out'):
- os.mkdir('out')
- for html in glob.glob('*.html'):
- f = open(html, 'r')
- html_string = f.read()
- f.close()
- head_position = html_string.find('</head>')
- if head_position < 0:
- print "There's no head tag in %s" % html
- continue
- new_html = html_string[:head_position-1] + '\n' + GA_TRACKER_CODE + html_string[head_position:]
- f = open(os.path.join('out', html), 'w')
- f.write(new_html)
- f.close()
Add Comment
Please, Sign In to add comment