|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +from bs4 import BeautifulSoup, Tag, Comment |
| 4 | +import sys |
| 5 | + |
| 6 | +headSnippet = """(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': |
| 7 | +new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], |
| 8 | +j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= |
| 9 | +'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); |
| 10 | +})(window,document,'script','dataLayer','GTM-W59SWTR');""" |
| 11 | + |
| 12 | +iframeSrc = "https://www.googletagmanager.com/ns.html?id=GTM-W59SWTR" |
| 13 | + |
| 14 | + |
| 15 | +insert_head = "<script class='gtm'>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);})(window,document,'script','dataLayer','GTM-W59SWTR');</script>" |
| 16 | +def patch_php(filepath): |
| 17 | + # print(filepath) |
| 18 | + try: |
| 19 | + if(1): |
| 20 | + f1 = open(filepath,"r") |
| 21 | + lines = f1.readlines() |
| 22 | + string_inserted = '' |
| 23 | + for line in lines: |
| 24 | + if("<script class='gtm'>" in line): |
| 25 | + return |
| 26 | + f1.close() |
| 27 | + if(1): |
| 28 | + # print(filepath) |
| 29 | + f1 = open(filepath,"r") |
| 30 | + lines = f1.readlines() |
| 31 | + # print(lines) |
| 32 | + string_inserted = '' |
| 33 | + for line in lines: |
| 34 | + if('<head>' in line): |
| 35 | + line = line.split('<head>') |
| 36 | + temp_string = line[0] |
| 37 | + temp_string+='<head>\n' |
| 38 | + temp_string+=insert_head+'\n' |
| 39 | + for i in line[1:]: |
| 40 | + temp_string+=i |
| 41 | + line = temp_string |
| 42 | + string_inserted+=line |
| 43 | + print(string_inserted) |
| 44 | + f1.close() |
| 45 | + f = open(filepath,"w") |
| 46 | + f.write(string_inserted) |
| 47 | + f.close() |
| 48 | + |
| 49 | + except IOError as e: |
| 50 | + print "I/O error({0}): {1}".format(e.errno, e.strerror) |
| 51 | + return -1 |
| 52 | + except: |
| 53 | + print "Unexpected error:", sys.exc_info()[0] |
| 54 | + return -2 |
| 55 | + print "Analytics Patched Successfully" |
| 56 | + return 0 |
| 57 | + |
| 58 | +# load the file |
| 59 | +def patch(filepath): |
| 60 | + if("php" in filepath): |
| 61 | + patch_php(filepath) |
| 62 | + return 0 |
| 63 | + try: |
| 64 | + with open(filepath) as inf: |
| 65 | + txt = inf.read() |
| 66 | + # soup = BeautifulSoup(txt, 'html.parser') |
| 67 | + soup = BeautifulSoup(txt, "html5lib") |
| 68 | + mydiv = soup.head.find('script', { 'class': 'gtm' }) |
| 69 | + if not mydiv: |
| 70 | + scrTag = Tag(soup, name = 'script') |
| 71 | + scrTag['class'] = "gtm" |
| 72 | + scrTag.string = headSnippet |
| 73 | + soup.head.insert(0, Comment('End Google Tag Manager')) |
| 74 | + soup.head.insert(0, scrTag) |
| 75 | + soup.head.insert(0, Comment('Google Tag Manager')) |
| 76 | + #scrTag.insert_before(Comment('Google Tag Manager')) |
| 77 | + #scrTag.insert_after(Comment('End Google Tag Manager')) |
| 78 | + |
| 79 | + # insert body snippet into the document |
| 80 | + iframeTag = Tag(soup, name = 'iframe') |
| 81 | + iframeTag['src'] = iframeSrc |
| 82 | + iframeTag['height'] = "0" |
| 83 | + iframeTag['width'] = "0" |
| 84 | + iframeTag['style'] = "display:none;visibility:hidden" |
| 85 | + |
| 86 | + noscrTag = Tag(soup, name = 'noscript') |
| 87 | + noscrTag['class'] = 'gtm' |
| 88 | + noscrTag.insert(0, iframeTag) |
| 89 | + soup.body.insert(0, Comment('End Google Tag Manager (noscript)')) |
| 90 | + soup.body.insert(0, noscrTag) |
| 91 | + soup.body.insert(0, Comment('Google Tag Manager (noscript)')) |
| 92 | + #noscrTag.insert_before(Comment('Google Tag Manager (noscript)')) |
| 93 | + #noscrTag.insert_after(Comment('End Google Tag Manager (noscript)')) |
| 94 | + |
| 95 | + # save the file again |
| 96 | + with open(filepath, 'w') as outf: |
| 97 | + outf.write(str(soup)) |
| 98 | + |
| 99 | + except IOError as e: |
| 100 | + print "I/O error({0}): {1}".format(e.errno, e.strerror) |
| 101 | + return -1 |
| 102 | + except: |
| 103 | + print "Unexpected error:", sys.exc_info()[0] |
| 104 | + return -2 |
| 105 | + print "Analytics Patched Successfully " + filepath |
| 106 | + return 0 |
| 107 | + |
| 108 | + |
| 109 | +if __name__=='__main__': |
| 110 | + patch(sys.argv[1]) |
0 commit comments