Skip to content

2. Advanced Configuration

444B edited this page Apr 15, 2024 · 4 revisions

streamlit-analytics2 offers several advanced configuration options to give you more control over tracking and to enhance the functionality of your analytics. Here's how to utilize these features effectively:

Custom Tracking

  • What
    Custom tracking allows you to monitor specific events or interactions in your Streamlit app beyond the default page views and widget interactions.

  • How
    To implement custom tracking, you can use the start_tracking() and stop_tracking() methods around your custom event code:

import streamlit as st
import streamlit_analytics2

streamlit_analytics2.start_tracking()
# Custom event tracking code here
streamlit_analytics2.stop_tracking()
  • Why
    Use custom tracking to gather insights on specific user actions that are crucial for your application's success, such as the completion of a form, interactions within a particular section of the app, or the usage frequency of a new feature.

Password Protection

  • What
    This feature allows you to add a simple password layer to your analytics dashboard, helping to restrict access to sensitive analytics data.

  • How
    Enable password protection by specifying an unsafe_password when calling the track() method:

streamlit_analytics2.track(unsafe_password="your_simple_password")

Note: This password is not encrypted, so choose something non-sensitive.

  • Why
    Password protection is essential if your analytics data contains sensitive information or if you want to limit access to your analytics dashboard to authorized personnel only.

Firestore Integration

  • What
    Firestore integration enables you to store your analytics data in a Firestore database, providing a persistent and scalable solution for data storage.

  • How
    Follow the steps below or take a look at this streamlit blog post.

  1. Head to console.firebase.google.com
  2. Click on "Create a project".
  3. Next, select an existing GCP(Google Cloud Platform) project or make a new one. Please note that this setup guide aims for an optimized use or free tier but the usage of GCP products and network remains your responsability.
    For this, please make sure you are on the "Spark" plan and not the "Blaze" plan.
  4. Next, decide whether to enable Google Analytics for this project.
  5. Wait a few minutes and then your firebase project should be ready
  6. Go to the left bar and under "Build", click on "Firestore" or anywhere else on the page that will take you to the firestore setup.
  7. Click on "Create Database"
  8. Only the (default) database qualifies for the free quota. If this is shown and you cant select anything else, then you are successfully on the free tier.
  9. Select "Start in Production mode" and then click create
  10. Once ready, click on "Create Collection" and give it a name.
    A collection is a set of documents that contain data Example: Collection "users" would contain a unique document for each user For our example, lets use "streamlit-analytics2" or the name of your app
  11. Next, click on "auto-id" for the document field or give it a name if you know what you are doing.
  12. Click save.

You are done. Make note of the collection name since you will need to pass it in as ar agument to the tracking function, as seen below:

streamlit_analytics2.track(firestore_key_file="path_to_your_firebase_key.json",
                            firestore_collection_name="your_collection_name")
  • If you don't want to push your firebase-key.json to GitHub, you can do the following to securely deploy on Streamlit Cloud, or your own hosting solution.
  1. Run this code to create the streamlit secrets directory and add your firebase key to .streamlit/secrets.toml. (Replace path_to_firebase_key.json with your path)
import toml
import os

# Create streamlit secrets directory and secrets.toml if it doesn't exist
if not os.path.exists("./.streamlit"):
  os.mkdir("./.streamlit")
  f = open("./.streamlit/secrets.toml", "x")
  f.close()

output_file = ".streamlit/secrets.toml"

with open(path_to_firebase_key.json) as json_file:
    json_text = json_file.read()

config = {"firebase": json_text}
toml_config = toml.dumps(config)

with open(output_file, "w") as target:
    target.write(toml_config)
  1. Add this to the top of your file
with streamlit_analytics.track(firestore_collection_name="counts", streamlit_secrets_firestore_key="firebase", firestore_project_name=firestore_project_name):
# or pass the same args to `start_tracking` AND `stop_tracking`

Full Example

import streamlit as st
import streamlit_analytics

with streamlit_analytics.track(firestore_collection_name="counts", streamlit_secrets_firestore_key="firebase", firestore_project_name=firestore_project_name):
    st.text_input("Write something")
    st.button("Click me")
  • Why
    Integrating Firestore is beneficial for long-term storage and analysis of analytics data. It's particularly useful for apps deployed over multiple sessions or requiring historical data analysis.

JSON Storage

  • What
    JSON storage allows you to save your analytics results as a JSON file, offering a simple way to persist and later retrieve data.

  • How
    To save analytics data to a JSON file:

streamlit_analytics2.track(save_to_json="path/to/file.json")

And to load existing data:

streamlit_analytics2.track(load_from_json="path/to/file.json")
  • Why
    Storing data in a JSON file is a straightforward method for persisting analytics data without needing a database. It's suitable for smaller applications or for scenarios where data needs to be easily shared or analyzed offline.

Conclusion

These advanced configuration options enhance the flexibility and functionality of streamlit-analytics2, allowing you to tailor the analytics to your application's specific needs. Whether you're looking to implement custom tracking, secure your data, persist data across sessions, or simplify your data storage solution, these features provide the tools you need to gain deeper insights into your Streamlit app's usage.