A minimal example demonstrating how to integrate Aptabase analytics into a Textual TUI application.
Perfect for learning the basics of adding privacy-first analytics to your terminal apps!
This is a simple counter app that:
- Increments a counter when you click a button
- Resets the counter to zero
- Tracks all interactions with Aptabase analytics
- Shows notifications for user feedback
- Clean UI: Centered layout with large counter display
- Two Buttons:
- "Click Me!" - Increments the counter
- "Reset" - Resets to zero
- Analytics Tracking:
- App start/stop events
- Button clicks with counter values
- Reset actions with previous count
- Session duration
# Install dependencies
uv pip install textual aptabase
# Run the app
uv run counter.py# Create and activate virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install textual aptabase
# Run the app
python counter.py# If you have the pyproject.toml file
uv sync
uv run counterBefore running, you need to set your Aptabase app key:
- Sign up at aptabase.com
- Create a new app
- Copy your app key (format:
A-EU-XXXXXXXXXXorA-US-XXXXXXXXXX) - Update the key in
counter.py:
# Replace with your actual Aptabase app key
app = CounterApp(app_key="A-EU-XXXXXXXXXX")The app automatically tracks:
Sent when the app launches.
{
"event": "app_started"
}Sent when "Click Me!" is pressed.
{
"event": "button_clicked",
"action": "increment",
"count": 5 # Current counter value
}Sent when "Reset" is pressed.
{
"event": "counter_reset",
"previous_count": 10 # Value before reset
}Sent when the app exits.
{
"event": "app_closed",
"final_count": 7 # Final counter value
}- Start the app: Run
python main.py - Click the button: Press "Click Me!" to increment (or press
Enterwhen focused) - Reset: Click "Reset" to set counter back to zero
- Quit: Press
qorCtrl+C
class CounterApp(App):
def __init__(self, app_key: str):
# Initialize with your Aptabase key
async def on_mount(self):
# Start Aptabase when app starts
async def on_unmount(self):
# Stop Aptabase and send final events
async def on_button_pressed(self, event):
# Handle button clicks and track eventsAptabase Initialization:
self.aptabase = Aptabase(
app_key=self.app_key,
app_version="1.0.0",
is_debug=True # Shows debug info
)
await self.aptabase.start()Tracking Events:
await self.aptabase.track("button_clicked", {
"action": "increment",
"count": self.counter
})Cleanup:
await self.aptabase.stop() # Flushes pending eventsHere are some ways to extend this app:
yield Button("Increment by 5", id="btn-plus5")
yield Button("Decrement", id="btn-decrement")import time
self.last_click = time.time()
# In button handler
time_since_last = time.time() - self.last_click
await self.aptabase.track("button_clicked", {
"count": self.counter,
"time_since_last_click": round(time_since_last, 2)
})BINDINGS = [
Binding("space", "increment", "Increment"),
Binding("r", "reset", "Reset"),
]
async def action_increment(self):
self.counter += 1
await self.aptabase.track("keyboard_increment")self.high_score = 0
if self.counter > self.high_score:
self.high_score = self.counter
await self.aptabase.track("new_high_score", {
"score": self.high_score
})Cause: Invalid app key or network issues.
Solutions:
- Check your app key format: Must be
A-EU-*orA-US-* - Verify network connectivity
- Check Aptabase dashboard status
- Look for error details in the console
ModuleNotFoundError: No module named 'textual'Solution: Install dependencies
pip install textual aptabaseCheck Python version:
python --version # Must be 3.11+Make sure to check the Debug-mode dashboard (not Release-mode)
MIT License - feel free to use this as a starting point for your own projects!
This is a demo app. Feel free to fork and modify for your needs!
- Aptabase: https://aptabase.com/
- Textual: https://textual.textualize.io/