Skip to content

Commit

Permalink
beautifulsoup injection tracking code
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenaH84 committed Nov 25, 2024
1 parent a0e371b commit 57bd521
Showing 1 changed file with 33 additions and 32 deletions.
65 changes: 33 additions & 32 deletions gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,43 +55,44 @@
# )


# def inject_ga():
# UM_ID = "adcac53d-bc65-4ca3-9f98-be5c7c4ee75d"
def inject_ga():
UM_ID = "adcac53d-bc65-4ca3-9f98-be5c7c4ee75d"

# UM_JS = """
# <script defer src="https://cloud.umami.is/script.js" data-website-id="adcac53d-bc65-4ca3-9f98-be5c7c4ee75d"></script>
# <script>
# window.dataLayer = window.dataLayer || [];
# function gtag(){dataLayer.push(arguments);}
# gtag('js, new Date());
# gtag('config', 'adcac53d-bc65-4ca3-9f98-be5c7c4ee75d');
# <script>
# """

# # Insert script into the index.html file
# index_path = pathlib.Path(st.__file__).parent / "static" / "index.html"
# soup = BeautifulSoup(index_path.read_text(), features="html.parser")
# if not soup.find(id=UM_ID):
# bck_index = index_path.with_suffix('.bck')
# if bck_index.exists():
# shutil.copy(bck_index, index_path)
# else:
# shutil.copy(index_path, bck_index)
# html = str(soup)
# new_html = html.replace('<head>', '<head>\n' + UM_JS)
# index_path.write_text(new_html)


def inject_umami():
umami_js = """
UM_JS = """
<script defer src="https://cloud.umami.is/script.js" data-website-id="adcac53d-bc65-4ca3-9f98-be5c7c4ee75d"></script>
"""
st.markdown(umami_js, unsafe_allow_html=True)
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js, new Date());
gtag('config', 'adcac53d-bc65-4ca3-9f98-be5c7c4ee75d');
<script>
"""

# Insert script into the index.html file
index_path = pathlib.Path(st.__file__).parent / "static" / "index.html"
soup = BeautifulSoup(index_path.read_text(), features="html.parser")
if not soup.find(id=UM_ID):
bck_index = index_path.with_suffix('.bck')
if bck_index.exists():
shutil.copy(bck_index, index_path)
else:
shutil.copy(index_path, bck_index)
html = str(soup)
new_html = html.replace('<head>', '<head>\n' + UM_JS)
index_path.write_text(new_html)


inject_ga()

# def inject_umami():
# umami_js = """
# <script defer src="https://cloud.umami.is/script.js" data-website-id="adcac53d-bc65-4ca3-9f98-be5c7c4ee75d"></script>
# """
# st.markdown(umami_js, unsafe_allow_html=True)


inject_umami()
# inject_umami()

# inject_ga()

##############################
# Remember user changed values
Expand Down

0 comments on commit 57bd521

Please sign in to comment.