Skip to main content

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

Demo

Dashboard Overview

Dashboard Overview

Session Analytics

Session Analytics

Detailed Session View

Detailed Session View

Requirements

  • Python 3.7 or higher
  • Flask 2.0.0 or higher
  • Flask-SQLAlchemy 3.0.0 or higher
  • Flask-Login 0.6.0 or higher

Installation

pip install flask-metrics-visitors

Development Setup

To build the package locally:

  1. Install build dependencies:
pip install twine
  1. Build the package with version increment:
# For a patch release (bug fixes)
./build.sh patch

# For a minor release (new features)
./build.sh minor

# For a major release (breaking changes)
./build.sh major
  1. Upload to PyPI (if you have access):
./push.sh

The version number follows semantic versioning (MAJOR.MINOR.PATCH):

  • MAJOR version for incompatible API changes
  • MINOR version for backwards-compatible functionality
  • PATCH version for backwards-compatible bug fixes

Setup

You can initialize the extension in two ways:

1. Direct Initialization

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_metrics_visitors import MetricsVisitors

app = Flask(__name__)
db = SQLAlchemy(app)

# Initialize the metrics extension
metrics = MetricsVisitors(app, db)

# Get and register the blueprint after initialization
app.register_blueprint(metrics.get_blueprint())
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_metrics_visitors import MetricsVisitors

db = SQLAlchemy()
metrics = MetricsVisitors()

def create_app():
    app = Flask(__name__)
    
    # Configure your app
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///metrics.db'
    app.config['METRICS_VISITORS_SESSION_LIFETIME'] = 30
    app.config['METRICS_VISITORS_UPDATE_INTERVAL'] = 30
    
    # Initialize extensions
    db.init_app(app)
    metrics.init_app(app, db)
    
    # Register the blueprint after initialization
    app.register_blueprint(metrics.get_blueprint())
    
    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. Create the database tables:
with app.app_context():
    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['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///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['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///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

Package Information

  • Author: Patrick Hastings
  • License: MIT
  • Python Support: 3.7, 3.8, 3.9, 3.10
  • Development Status: Beta
  • Framework: Flask
  • Environment: Web Environment

License

MIT

Metadata

Release files for flask-metrics-visitors 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask-metrics-visitors 0.9.0
File Size Uploaded Provenance Actions
flask_metrics_visitors-0.9.0.tar.gz 16.3 kB Without provenance Details for flask_metrics_visitors-0.9.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for flask-metrics-visitors 0.9.0
File Interpreter ABI Platform Actions
flask_metrics_visitors-0.9.0-py3-none-any.whl Python 3 none any Details for flask_metrics_visitors-0.9.0-py3-none-any.whl

Total release size: 32.7 kB

Release files / flask_metrics_visitors-0.9.0.tar.gz

Download URL flask_metrics_visitors-0.9.0.tar.gz
Size 16.3 kB
Tags Source
SHA-256 checksum
How to use checksums
8dfbdc1d70614854eddb544b6fce1641398df9de59fbf4b94b7fb6d65af5163d
BLAKE2b-256 checksum
How to use checksums
9445b356ce90fc1c0db067117a8b9c7c4dd0c46b2ad00a661eb07a126df38a2d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Release files / flask_metrics_visitors-0.9.0-py3-none-any.whl

Download URL flask_metrics_visitors-0.9.0-py3-none-any.whl
Size 16.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c552a756b24016db3cd4ad6e6bbb2acac337c6fec2b24ee2fb90f1adf6d4bf1e
BLAKE2b-256 checksum
How to use checksums
d2978dc0c43e302c7f0b698acb05b6ce92f9e4a6ae11e3fc22d84e0c18204534
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.2

Security for flask-metrics-visitors 0.9.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page