-
Notifications
You must be signed in to change notification settings - Fork 5
2. Advanced Configuration
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:
-
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 thestart_tracking()
andstop_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.
-
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 anunsafe_password
when calling thetrack()
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.
-
What
Firestore integration enables you to store your analytics data in a Firestore database, providing a persistent and scalable solution for data storage. The original post for setting up this feature was written by Streamlit here. -
How
Follow the steps below or take a look at this streamlit blog post.
- Head to console.firebase.google.com
- Click on "Get started with a Firebase Project".
- Next, select an existing GCP(Google Cloud Platform) project or make a new one.
- Keep on clicking through and decide whether to enable Google Analytics for this project.
- Wait a few minutes and then your firebase project should be ready.
- Click on "Project Overview" cogwheel in top left corner and select "Usage and Billing". It should say "Project Cost: Spark". If it does not say Spark, do not proceed to the next step unless you are prepared to have the use of Firebase create a fee to you for its use. Anything besides Spark will likely cost you money.
- Go to the left bar and under the "Build" dropdown, click on "Firestore Database"
- Click on "Create Database"
- 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.
- Select a Region closes to your users, if you know where they are. If you are unsure, select a multi region zone like nam5 or eur3.
- Select "Start in Production mode" and then click create
- Once ready, click on "Start 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 - Next, click on "auto-id" for the document field or give it a name if you know what you are doing.
- Click save.
- Now you need to get your authentication token to firebase. Go to "Proeject Settings"
- Then go to "Service accounts", select "Python", and click "Generate new private key".
- You should have downloaded a file that looks like my-fancy-project-firebase-adminsdk-4enia-e106b71674.json. Move it into your Git repo, and let's rename it to something simpler like firestore-key.json
You are done. Make note of the collection name since you will need to pass it in as an 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.
- Run this code to create the streamlit secrets directory and add your firebase key to
.streamlit/secrets.toml
. (Replacepath_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)
- 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_analytics2 as streamlit_analytics
with streamlit_analytics.track(
firestore_collection_name="streamlit-analytics2",
firestore_document_name="data",
streamlit_secrets_firestore_key="path/to/firebase.json",
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.
-
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.
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.