Skip to main content

A Flask extension for tracking user metrics and visit statistics

Project description

Flask Metrics Visitors

A Flask extension for tracking and visualizing visitor metrics.

Features

  • Track visitor sessions and page views
  • Display real-time session analytics
  • Filterable session data with DataTables
  • Session duration and click tracking
  • Page URL tracking
  • Timezone-aware timestamps
  • Persistent SQLite database storage

Installation

pip install flask-metrics-visitors

Development Setup

To build the package locally:

  1. Install build dependencies:
pip install build twine
  1. Build the package:
./build.sh
  1. Upload to PyPI (if you have access):
./push.sh

Setup

You can initialize the extension in two ways:

1. Direct Initialization

from flask import Flask
from flask_metrics_visitors import MetricsVisitors

app = Flask(__name__)
metrics = MetricsVisitors(app)

2. Factory Pattern (Recommended)

from flask import Flask
from flask_metrics_visitors import MetricsVisitors

metrics = MetricsVisitors()

def create_app():
    app = Flask(__name__)
    metrics.init_app(app)
    return app

app = create_app()
  1. Include the session tracking JavaScript in your base template:
<script src="{{ url_for('metrics_visitors.static', filename='js/session-tracker.js') }}"></script>
  1. Add the metrics blueprint to your application:
app.register_blueprint(metrics.blueprint)
  1. Create the database tables:
with app.app_context():
    metrics.db.create_all()

Database Configuration

The extension uses SQLite for persistent storage of session data. By default, the database is stored in the application's instance folder. You can configure the database path using:

app.config['METRICS_VISITORS_DB_PATH'] = 'path/to/your/database.db'

Database Backup

It's recommended to implement regular backups of your SQLite database. Here's a simple backup strategy:

  1. Create a backup directory:
mkdir -p backups
  1. Set up a cron job to backup the database daily:
0 0 * * * cp /path/to/your/database.db /path/to/backups/database_$(date +\%Y\%m\%d).db

Database Maintenance

The extension automatically:

  • Cleans up old session data based on the configured retention period
  • Handles database migrations when the schema changes
  • Optimizes the database for performance

Usage

  1. Access the metrics dashboard at /metrics
  2. View session analytics at /metrics/session-analytics
  3. Session data is automatically tracked and updated every 30 seconds
  4. Use the DataTables interface to filter and sort session data

Session Tracking

The session tracker automatically:

  • Tracks session duration
  • Counts clicks
  • Records page URLs
  • Updates stats periodically
  • Sends final stats on page unload
  • Handles page visibility changes

Configuration

The following configuration options are available:

app.config['METRICS_VISITORS_DB_PATH'] = 'metrics.db'  # SQLite database path
app.config['METRICS_VISITORS_SESSION_LIFETIME'] = 30  # Session lifetime in days
app.config['METRICS_VISITORS_UPDATE_INTERVAL'] = 30  # Update interval in seconds

License

MIT

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

flask_metrics_visitors-0.1.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

flask_metrics_visitors-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file flask_metrics_visitors-0.1.0.tar.gz.

File metadata

  • Download URL: flask_metrics_visitors-0.1.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for flask_metrics_visitors-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5b90b501c2cf03c9508f35b0d1e177f65f47b9532bee40724b2af0c845720854
MD5 ebe25faebe7b92d3bba400d6fe7f2b12
BLAKE2b-256 03d11eb8acc5c63e18025014a5719b49b9b175da7c8036086d5effb4304540ae

See more details on using hashes here.

File details

Details for the file flask_metrics_visitors-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flask_metrics_visitors-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c961b776bbca91aedd8f9b769a54259d2348214b1922d5a7d9e5b6a6dff14fc
MD5 b431a43993ad4b08219d6abc6d498dae
BLAKE2b-256 7716e0cf34b2f04797911321b5232e192aa64c2eaa2fcc7676fec78982741b80

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