srmsoumya commited on
Commit
a63a92a
·
1 Parent(s): 4c12a82

enh: Add tracking using plausible - tracks query & download button

Browse files
Files changed (1) hide show
  1. gazet_demo.py +55 -5
gazet_demo.py CHANGED
@@ -8,6 +8,7 @@ import re
8
  import pandas as pd
9
  import requests
10
  import streamlit as st
 
11
 
12
  try:
13
  import pydeck as pdk
@@ -137,6 +138,49 @@ def _render_map(geojson, placeholder):
137
 
138
 
139
  API = os.environ.get("GAZET_API_URL", "http://127.0.0.1:8000")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  EXAMPLES = [
141
  "Odisha, India",
142
  "Neighbouring states of Odisha",
@@ -149,6 +193,7 @@ EXAMPLES = [
149
  ]
150
 
151
  st.set_page_config(page_title="Gazet", page_icon="🌍", layout="wide")
 
152
  st.markdown("""<style>
153
  [data-testid="stBaseButton-tertiary"] {
154
  border: 1px dashed rgba(128,128,128,0.3) !important;
@@ -187,10 +232,12 @@ with col1:
187
  search_clicked = st.button("Go!", type="primary")
188
  if search_clicked and q:
189
  st.session_state.run_q = q
 
190
  st.caption("Click an example to get started ->")
191
  for ex in EXAMPLES:
192
  if st.button(ex, key=ex, type="tertiary"):
193
  st.session_state.run_q = ex
 
194
 
195
  with col2:
196
  to_run = st.session_state.run_q
@@ -277,13 +324,14 @@ with col2:
277
  n = len(geojson.get("features", []))
278
  status_ph.success(f"**{to_run}** → {n} feature(s)")
279
  _slug = re.sub(r"[^\w]+", "_", to_run.lower()).strip("_") or "result"
280
- download_ph.download_button(
281
  "Download GeoJSON",
282
  data=json.dumps(geojson),
283
  file_name=f"{_slug}.geojson",
284
  mime="application/geo+json",
285
  key=f"dl_{_slug}",
286
- )
 
287
  _render_map(geojson, map_ph)
288
 
289
  elif t == "error":
@@ -303,13 +351,15 @@ with col2:
303
  st.success(f"**{query}** -> {n_feat} feature(s)")
304
  if result["geojson"]:
305
  _slug = re.sub(r"[^\w]+", "_", query.lower()).strip("_") or "result"
306
- st.download_button(
 
307
  "Download GeoJSON",
308
  data=json.dumps(result["geojson"]),
309
  file_name=f"{_slug}.geojson",
310
  mime="application/geo+json",
311
- key=f"dl_cached_{_slug}",
312
- )
 
313
  _render_map(result["geojson"], st.empty())
314
  if result["places"]:
315
  with st.expander("Extracted place names"):
 
8
  import pandas as pd
9
  import requests
10
  import streamlit as st
11
+ import streamlit.components.v1 as components
12
 
13
  try:
14
  import pydeck as pdk
 
138
 
139
 
140
  API = os.environ.get("GAZET_API_URL", "http://127.0.0.1:8000")
141
+ PLAUSIBLE_SRC = os.environ.get(
142
+ "PLAUSIBLE_SRC",
143
+ "https://plausible.io/js/pa-cOxJkIdBOhaQIfgDA-a4y.js",
144
+ )
145
+
146
+
147
+ def _inject_plausible():
148
+ """Inject the Plausible script into the top-level document once per session."""
149
+ components.html(
150
+ f"""
151
+ <script>
152
+ (function() {{
153
+ var doc = window.parent.document;
154
+ if (doc.getElementById('plausible-script')) return;
155
+ var s = doc.createElement('script');
156
+ s.id = 'plausible-script';
157
+ s.async = true;
158
+ s.src = '{PLAUSIBLE_SRC}';
159
+ doc.head.appendChild(s);
160
+ var win = window.parent;
161
+ win.plausible = win.plausible || function() {{(win.plausible.q = win.plausible.q || []).push(arguments)}};
162
+ win.plausible.init = win.plausible.init || function(i) {{win.plausible.o = i || {{}}}};
163
+ win.plausible.init();
164
+ }})();
165
+ </script>
166
+ """,
167
+ height=0,
168
+ )
169
+
170
+
171
+ def _track(event_name: str, **props):
172
+ """Fire a custom Plausible event via the parent window."""
173
+ components.html(
174
+ f"""
175
+ <script>
176
+ if (window.parent.plausible) {{
177
+ window.parent.plausible({json.dumps(event_name)}, {{props: {json.dumps(props)}}});
178
+ }}
179
+ </script>
180
+ """,
181
+ height=0,
182
+ )
183
+
184
  EXAMPLES = [
185
  "Odisha, India",
186
  "Neighbouring states of Odisha",
 
193
  ]
194
 
195
  st.set_page_config(page_title="Gazet", page_icon="🌍", layout="wide")
196
+ _inject_plausible()
197
  st.markdown("""<style>
198
  [data-testid="stBaseButton-tertiary"] {
199
  border: 1px dashed rgba(128,128,128,0.3) !important;
 
232
  search_clicked = st.button("Go!", type="primary")
233
  if search_clicked and q:
234
  st.session_state.run_q = q
235
+ _track("Query Submitted", q=q, source="button")
236
  st.caption("Click an example to get started ->")
237
  for ex in EXAMPLES:
238
  if st.button(ex, key=ex, type="tertiary"):
239
  st.session_state.run_q = ex
240
+ _track("Query Submitted", q=ex, source="example")
241
 
242
  with col2:
243
  to_run = st.session_state.run_q
 
324
  n = len(geojson.get("features", []))
325
  status_ph.success(f"**{to_run}** → {n} feature(s)")
326
  _slug = re.sub(r"[^\w]+", "_", to_run.lower()).strip("_") or "result"
327
+ if download_ph.download_button(
328
  "Download GeoJSON",
329
  data=json.dumps(geojson),
330
  file_name=f"{_slug}.geojson",
331
  mime="application/geo+json",
332
  key=f"dl_{_slug}",
333
+ ):
334
+ _track("Download GeoJSON", q=to_run, features=n)
335
  _render_map(geojson, map_ph)
336
 
337
  elif t == "error":
 
351
  st.success(f"**{query}** -> {n_feat} feature(s)")
352
  if result["geojson"]:
353
  _slug = re.sub(r"[^\w]+", "_", query.lower()).strip("_") or "result"
354
+ _n_cached = len(result["geojson"].get("features", []))
355
+ if st.download_button(
356
  "Download GeoJSON",
357
  data=json.dumps(result["geojson"]),
358
  file_name=f"{_slug}.geojson",
359
  mime="application/geo+json",
360
+ key=f"dl_{_slug}",
361
+ ):
362
+ _track("Download GeoJSON", q=query, features=_n_cached)
363
  _render_map(result["geojson"], st.empty())
364
  if result["places"]:
365
  with st.expander("Extracted place names"):