Skip to content

Commit

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


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

UM_JS = """
# 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 = """
<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)


inject_ga()
"""
st.markdown(umami_js, unsafe_allow_html=True)


inject_umami()

# inject_ga()

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

0 comments on commit a0e371b

Please sign in to comment.