Skip to content

Latest commit

 

History

History
86 lines (57 loc) · 2.42 KB

README.md

File metadata and controls

86 lines (57 loc) · 2.42 KB

Flask-Product-Analytics

Web and product analytics for Flask

Installation

pip install flask-product-analytics

Usage

from flask import app
from flask_product_analytics import ProductAnalytics

app = Flask(__name__)
analytics = ProductAnalytics(app, backend="posthog")

The backend name can either be a builtin backend names (see Backends section) or an instance of a backend.

Track events

Track events from the backend:

analytics.track_event("myevent")
analytics.track_event("myevent-with-data", {"prop": "value"})

Identify the current user using analytics.identify(user_id, props).

Register global request properties using analytics.prop(name, value). These properties will be attached to all further events tracked during the request.

Tracking page views from the backend

Activate page view tracking using the ANALYTICS_PAGE_VIEWS app option.

If you wish to store the page title (same as browser script), use analytics_page_title() in your template:

<title>{{analytics_page_title("My page title")}}</title>

You can also track individual page views using the @pageview() decorator.

Routing events to different backends

analytics = ProductAnalytics(app, backend="posthog", routing={
    "pageview": ["umami", "posthog"],
    "conversion": "google_tag"
})

Tracking script

Some backends provide a tracking script for pageviews and frontend events. Use analytics_tracking_script() in your template to embed it:

<head>{{analytics_tracking_script()}}</head>

It optionnaly takes a backend name as argument.

Configuration

Config key Extension argument  Description Default
ANALYTICS_BACKEND backend Default backends (can be a single name or list) local
ANALYTICS_ROUTING routing The routing table for events Everything to default backend
ANALYTICS_PAGEVIEWS pageviews Whether to track page views from the backend False
ANALYTICS_SILENT silent All errors are silent True

Backends

Umami

Backend name: umami

| Config key | Description | Default | | --- | --- | --- | --- | | UMAMI_WEBSITE | website | Website ID | | UMAMI_API_URL | URL of the API | https://api.umami.is/v1 | UMAMI_API_KEY | API key when using Cloud version | | UMAMI_AUTH_TOKEN | Auth token when self hosting | | UMAMI_SCRIPT_URL | script_url | URL of the browser tracking script | https://us.umami.is/script.js