Skip to main content

Track & visualize user interactions with your streamlit app

Project description

streamlit-analytics  👀

PyPi

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.


🎈 Live Demo 🎈


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

  • If you don't want a huge with block, you can also do:

    streamlit_analytics.start_tracking()
    # your streamlit code here
    streamlit_analytics.stop_tracking()
    
  • You can password-protect your analytics results with:

    streamlit_analytics.track(unsafe_password="test123")
    # or pass the same arg to `stop_tracking`
    

    The app will then ask for this password before showing any results. Do not choose an important password here, it's not encrypted. If you push your code to Github, you should probably store the password in a .env file (which is in .gitignore) and load it via dotenv.

  • If you don't want the results to get reset after restarting streamlit (e.g. during deployment), you can sync them to a Firestore database. Follow this blogpost to set up the database and pass the key file and collection name:

    streamlit_analytics.track(firestore_key_file="firebase-key.json", firestore_collection_name="counts")
    # or pass the same args to `start_tracking` AND `stop_tracking`
    
  • You can store analytics results as a json file with:

    streamlit_analytics.track(save_to_json="path/to/file.json")
    # or pass the same arg to `stop_tracking`
    

    And load with:

    streamlit_analytics.track(load_from_json="path/to/file.json")
    # or pass the same arg to `start_tracking`
    

    (Thanks to @Uranium2 for implementing loading!)

    You can also combine both args to persist data to a json file. Note that this file might get deleted when doing a fresh deploy on a cloud service. Use Firestore instead for persistence, see above. Also note that load_from_json will fail silently if the JSON file does not exist. Writing to JSON 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.

  • Pass all settings args in start_tracking and not in stop_tracking

  • Do not track default values for selectbox, text_input etc. This can probably be done easily if I switch to using on_change.

  • Track unique users -> best way is to use cookies (e.g. with react-cookies) but this probably requires to show a consent form (could also build this in with react-cookie-consent)

  • 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.

  • Add a button to reset analytics results (see issue #2, this should probably show another prompt for confirmation, similar to if you delete a Github repo)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit-analytics-0.3.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_analytics-0.3.0-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file streamlit-analytics-0.3.0.tar.gz.

File metadata

  • Download URL: streamlit-analytics-0.3.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.6.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for streamlit-analytics-0.3.0.tar.gz
Algorithm Hash digest
SHA256 d2a79ce59d112af78fb483ef91ad75dd9d83b09461c9ad1083f1880c64ab1c11
MD5 db43ef8abe3a4e81b723711ebb7c17ee
BLAKE2b-256 3f81ba47fe2918151d6332d8978af33c49b2735e667b975072110f7fa488f8af

See more details on using hashes here.

File details

Details for the file streamlit_analytics-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: streamlit_analytics-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.6.1 requests/2.24.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.5

File hashes

Hashes for streamlit_analytics-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f51879b6b0eb98a5901c2fca324b6335626655daf922dcb36c5da67489e8162d
MD5 cff45dc5beaddb75737cd35582c5eec5
BLAKE2b-256 accf3f1c05209343d4dc36314f1644f1f71c67355aa6c6491e647ab00c0aca3f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page