Track & visualize user interactions with your streamlit app
Project description
streamlit-analytics 👀
Track & visualize user interactions with your streamlit app.
This is a small extension for the fantastic streamlit framework. With just one line of code, it counts page views, tracks all widget interactions across users, and visualizes the results directly in your browser. Think Google Analytics but for streamlit.
Alpha version, use with care.
Installation
pip install streamlit-analytics
How to use it
import streamlit as st
import streamlit_analytics
with streamlit_analytics.track():
st.text_input("Write something")
st.button("Click me")
That's it! 🎈 All page views and user inputs are now tracked and counted. Of course,
you can also use any other streamlit widget
in the with
block (both from st.
and st.sidebar.
).
Note: One thing that doesn't work (yet) is tracking widgets created directly from
containers, expanders, or columns (e.g. st.beta_expander().button("foo")
). Instead,
please use a with statement, e.g. with st.beta_expander(): st.button("foo")
.
To view the results, open your app like normal and append ?analytics=on
to the URL
(e.g. http://localhost:8501/?analytics=on). The results are then shown directly below
your app (see image above).
More options
-
You can password-protect your analytics results with
streamlit_analytics.track(unsafe_password="test123")
. The streamlit app will then ask you for this password. Do not choose an important password here – it is sent without encryption. -
You can persist your analytics results to a Firestore database. By default, your results get reset if you restart streamlit (e.g. if you deploy). To set up the Firestore database, follow this blogpost and pass the key file and collection name as
streamlit_analytics.track(firebase_key_file="firebase-key.json" firebase_collection_name="counts")
. -
If you don't want a huge
with
block, you can also do:import streamlit_analytics streamlit_analytics.start_tracking() # your streamlit code here streamlit_analytics.stop_tracking()
-
You can store analytics results as a json file by passing
save_to_json="path/to/file.json"
tostreamlit_analytics.track
orstreamlit_analytics.stop_tracking
. At the moment, this may lead to problems with concurrency if many users access the site at the same time.
TODO
PRs are welcome! If you want to work on any of these things, please open an issue to coordinate.
-
Track unique users (probably requires cookies? but would we need to show a consent form then?)
-
Enable tracking on widgets created directly from beta_container, beta_expander, beta_columns
-
Make a demo gif for the readme
-
Persist results after re-starting app (e.g. database or file, but where should this be saved/hosted) -
Find an easier alternative to Firestore for saving the data
-
Track time the user spent in a session and show as "complete time spent on your app" -
Implement A/B testing, e.g. by choosing one option for a new user randomly, storing it in session object, and then returning the correct bool value for below, and tracking & visualizing stats separately for both options:
if streamlit_analytics.split_test("option a", 2): st.button("Is this button text better?") if streamlit_analytics.split_test("option b", 2): st.button("...or this one?")
-
Enable tracking to Google Analytics, e.g. via custom component with react-ga. Widget interactions could also be tracked via events.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for streamlit-analytics-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17faf31cb89f70591b633d20893e187ab4b33e6826b4a25eabf3210ece66b62f |
|
MD5 | 23a382f558ff6e3d6f1160b7c1665b51 |
|
BLAKE2b-256 | c55cc6a65e3df362fbadda8e9cea627efd540a23715db95e6b0742082c4b57e7 |
Hashes for streamlit_analytics-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a3867e7a74f27283497f4b42e6f21b874f818e5e21af43ccededd79f29c0195 |
|
MD5 | f695feb1bec9c3113c4ee228fd357579 |
|
BLAKE2b-256 | 4453202f45949fe698e59ee8f5ba7e0979698bad702c43ecffda9ed3c4df5b54 |