simple-module-analytics 0.0.15__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- simple_module_analytics-0.0.15.dist-info/METADATA +57 -0
- simple_module_analytics-0.0.15.dist-info/RECORD +50 -0
- simple_module_analytics-0.0.15.dist-info/WHEEL +4 -0
- simple_module_analytics-0.0.15.dist-info/entry_points.txt +2 -0
- sm_analytics/__init__.py +1 -0
- sm_analytics/collection/__init__.py +1 -0
- sm_analytics/collection/privacy.py +85 -0
- sm_analytics/collection/retention.py +33 -0
- sm_analytics/collection/service.py +105 -0
- sm_analytics/core/__init__.py +1 -0
- sm_analytics/core/constants.py +15 -0
- sm_analytics/core/geo.py +133 -0
- sm_analytics/core/runtime.py +58 -0
- sm_analytics/core/settings.py +22 -0
- sm_analytics/core/tenancy.py +26 -0
- sm_analytics/index.ts +3 -0
- sm_analytics/locales/de.json +118 -0
- sm_analytics/locales/en.json +118 -0
- sm_analytics/models/__init__.py +87 -0
- sm_analytics/module.py +103 -0
- sm_analytics/package.json +21 -0
- sm_analytics/pages/Dashboard.tsx +164 -0
- sm_analytics/reporting/__init__.py +1 -0
- sm_analytics/reporting/reports.py +145 -0
- sm_analytics/ui/components/DateRangeControl.tsx +126 -0
- sm_analytics/ui/components/DeleteProperty.tsx +68 -0
- sm_analytics/ui/components/FilterChips.tsx +74 -0
- sm_analytics/ui/components/PropertyEditor.tsx +189 -0
- sm_analytics/ui/components/PropertySettings.tsx +120 -0
- sm_analytics/ui/components/RankingPanel.tsx +119 -0
- sm_analytics/ui/components/RecentActivity.tsx +63 -0
- sm_analytics/ui/components/ReportExport.tsx +63 -0
- sm_analytics/ui/components/ReportFilters.tsx +119 -0
- sm_analytics/ui/components/ReportView.tsx +86 -0
- sm_analytics/ui/components/TrafficChart.tsx +123 -0
- sm_analytics/ui/utils/api.ts +52 -0
- sm_analytics/ui/utils/csrf.ts +46 -0
- sm_analytics/ui/utils/hostTracker.ts +52 -0
- sm_analytics/ui/utils/i18n.ts +89 -0
- sm_analytics/ui/utils/reportExport.ts +48 -0
- sm_analytics/ui/utils/tracker.ts +102 -0
- sm_analytics/ui/utils/trackingPaths.ts +36 -0
- sm_analytics/ui/utils/types.ts +55 -0
- sm_analytics/ui/utils/useAnalytics.ts +111 -0
- sm_analytics/web/__init__.py +1 -0
- sm_analytics/web/deps.py +30 -0
- sm_analytics/web/endpoints/__init__.py +1 -0
- sm_analytics/web/endpoints/admin.py +115 -0
- sm_analytics/web/endpoints/public.py +76 -0
- sm_analytics/web/schemas.py +132 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
{
|
|
2
|
+
"menu": {
|
|
3
|
+
"analytics": "Analysen"
|
|
4
|
+
},
|
|
5
|
+
"title": "Analysen",
|
|
6
|
+
"description": "Besuche und Anwendungsaktivität verstehen, ohne Besucher zu identifizieren.",
|
|
7
|
+
"properties": "Projekte",
|
|
8
|
+
"property": "Projekt",
|
|
9
|
+
"new_property": "Neues Projekt",
|
|
10
|
+
"edit_property": "Projekt bearbeiten",
|
|
11
|
+
"no_properties": "Erstellen Sie ein Projekt, um Besuche zu messen. Die Erfassung bleibt bis zur Aktivierung ausgeschaltet.",
|
|
12
|
+
"name": "Name",
|
|
13
|
+
"kind": "Erfassungsart",
|
|
14
|
+
"public": "Öffentliche Seiten",
|
|
15
|
+
"application": "Anwendungsnutzung",
|
|
16
|
+
"origins": "Website-Ursprünge",
|
|
17
|
+
"origins_help": "Ein vollständiger Ursprung pro Zeile, zum Beispiel https://example.com.",
|
|
18
|
+
"routes": "Erfasste Pfade",
|
|
19
|
+
"routes_help": "Ein Pfad pro Zeile. :id steht für ein variables Segment, zum Beispiel /records/:id. Andere Pfade werden ausgeschlossen.",
|
|
20
|
+
"events_config": "Eigene Ereignisse",
|
|
21
|
+
"events_help": "Ein Ereignis pro Zeile: download: format, category. Leer lassen, um nur Seitenaufrufe zu erfassen.",
|
|
22
|
+
"utm": "Kampagnenparameter",
|
|
23
|
+
"utm_help": "Wählen Sie nur die Kampagnenwerte, die gespeichert werden sollen. Andere Abfrageparameter werden verworfen.",
|
|
24
|
+
"enabled": "Erfassung aktivieren",
|
|
25
|
+
"enabled_help": "Die Erfassung ist anfangs ausgeschaltet. Öffentliche Seiten und Anwendungsnutzung verwenden getrennte Projekte.",
|
|
26
|
+
"save": "Projekt speichern",
|
|
27
|
+
"saving": "Wird gespeichert…",
|
|
28
|
+
"cancel": "Abbrechen",
|
|
29
|
+
"delete": "Projekt löschen",
|
|
30
|
+
"delete_title": "Dieses Projekt löschen?",
|
|
31
|
+
"delete_description": "Das Projekt und alle erfassten Besuche und Ereignisse werden dauerhaft gelöscht.",
|
|
32
|
+
"deleting": "Wird gelöscht…",
|
|
33
|
+
"edit": "Bearbeiten",
|
|
34
|
+
"loading": "Analysen werden geladen…",
|
|
35
|
+
"error": "Die Analysen konnten nicht geladen werden. Versuchen Sie es erneut.",
|
|
36
|
+
"refresh": "Aktualisieren",
|
|
37
|
+
"no_data": "Keine Aktivität in diesem Zeitraum.",
|
|
38
|
+
"start": "Startdatum (UTC)",
|
|
39
|
+
"end": "Enddatum (UTC)",
|
|
40
|
+
"all": "Alle",
|
|
41
|
+
"unknown": "Unbekannt",
|
|
42
|
+
"clear_filters": "Filter zurücksetzen",
|
|
43
|
+
"filters": "Aktivität filtern",
|
|
44
|
+
"page_filter": "Seitenpfad",
|
|
45
|
+
"event_filter": "Ereignisname",
|
|
46
|
+
"pageviews": "Seitenaufrufe",
|
|
47
|
+
"visitors": "Geschätzte Besucher",
|
|
48
|
+
"sessions": "Geschätzte Sitzungen",
|
|
49
|
+
"events": "Eigene Ereignisse",
|
|
50
|
+
"estimates": "Besucher sind tägliche Schätzungen. Sitzungen enden nach 30 Minuten Inaktivität oder um Mitternacht UTC.",
|
|
51
|
+
"period": "Besuche im Zeitverlauf",
|
|
52
|
+
"date": "Datum",
|
|
53
|
+
"count": "Anzahl",
|
|
54
|
+
"value": "Wert",
|
|
55
|
+
"recent": "Aktuelle Aktivität",
|
|
56
|
+
"recent_help": "Letzte fünf Minuten; wird bei sichtbarer Seite alle 30 Sekunden aktualisiert.",
|
|
57
|
+
"recent_visitors": "Aktive Besucher",
|
|
58
|
+
"time": "Zeit (UTC)",
|
|
59
|
+
"location": "Standort",
|
|
60
|
+
"geo_title": "Besucherstandorte",
|
|
61
|
+
"geo_approximate": "Standorte sind Näherungswerte auf Basis von Netzwerkadressen, nicht Geräte-GPS.",
|
|
62
|
+
"geo_ready": "Standortdatenbank verfügbar",
|
|
63
|
+
"geo_unavailable": "Standortdatenbank nicht verfügbar. Aktivität wird weiterhin mit unbekannten Standorten erfasst.",
|
|
64
|
+
"geo_disabled": "Standortberichte sind deaktiviert.",
|
|
65
|
+
"geo_age": "Alter der Datenbank: {days} Tage",
|
|
66
|
+
"setup": "Erfassung einrichten",
|
|
67
|
+
"setup_help": "Der Demo-Host erfasst passende Pfade, sobald dieses Projekt aktiviert ist. Andere Hosts können den Analytics-Host-Adapter installieren und freigegebene Ereignisse mit track(name, properties) senden.",
|
|
68
|
+
"event_format_error": "Ein Ereignis pro Zeile verwenden: ereignis: eigenschaft, eigenschaft.",
|
|
69
|
+
"dimensions": {
|
|
70
|
+
"path": "Seiten",
|
|
71
|
+
"referrer": "Verweise",
|
|
72
|
+
"utm_source": "Kampagnenquellen",
|
|
73
|
+
"utm_medium": "Kampagnenmedien",
|
|
74
|
+
"utm_campaign": "Kampagnen",
|
|
75
|
+
"browser": "Browser",
|
|
76
|
+
"os": "Betriebssysteme",
|
|
77
|
+
"device": "Geräte",
|
|
78
|
+
"country": "Länder",
|
|
79
|
+
"region": "Regionen",
|
|
80
|
+
"city": "Städte",
|
|
81
|
+
"event": "Ereignisse"
|
|
82
|
+
},
|
|
83
|
+
"country": "Land",
|
|
84
|
+
"region": "Region",
|
|
85
|
+
"city": "Stadt",
|
|
86
|
+
"status": "Status",
|
|
87
|
+
"tracking_on": "Erfassung aktiviert",
|
|
88
|
+
"tracking_off": "Erfassung pausiert",
|
|
89
|
+
"event_property": "Ereigniseigenschaft: {name}",
|
|
90
|
+
"ui": {
|
|
91
|
+
"overview": "Übersicht",
|
|
92
|
+
"sources": "Quellen",
|
|
93
|
+
"audience": "Zielgruppe",
|
|
94
|
+
"campaigns": "Kampagnen",
|
|
95
|
+
"date_range": "Zeitraum",
|
|
96
|
+
"today": "Heute",
|
|
97
|
+
"last_7_days": "Letzte 7 Tage",
|
|
98
|
+
"last_30_days": "Letzte 30 Tage",
|
|
99
|
+
"last_90_days": "Letzte 90 Tage",
|
|
100
|
+
"custom_range": "Eigene Daten",
|
|
101
|
+
"apply": "Anwenden",
|
|
102
|
+
"filters_button": "Filter",
|
|
103
|
+
"manage": "Eigenschaft verwalten",
|
|
104
|
+
"close": "Schließen",
|
|
105
|
+
"show_more": "Mehr anzeigen",
|
|
106
|
+
"show_less": "Weniger anzeigen",
|
|
107
|
+
"chart_data": "Diagrammdaten anzeigen",
|
|
108
|
+
"chart_metric": "Diagrammkennzahl",
|
|
109
|
+
"active_now": "{count} gerade aktiv",
|
|
110
|
+
"activity": "Aktivität",
|
|
111
|
+
"date_help": "Datumsangaben in UTC. Das Enddatum muss am oder nach dem Startdatum und innerhalb von 366 Tagen liegen.",
|
|
112
|
+
"clear_filter": "Filter {name} entfernen",
|
|
113
|
+
"settings": "Eigenschaftseinstellungen",
|
|
114
|
+
"export_csv": "CSV exportieren",
|
|
115
|
+
"export_help": "Aktuellen Bericht herunterladen: Summen, täglicher Traffic und bis zu 50 Zeilen je Aufschlüsselung. Zeitraum und Filter sind enthalten.",
|
|
116
|
+
"export_failed": "CSV-Export fehlgeschlagen. Bitte erneut versuchen."
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
{
|
|
2
|
+
"menu": {
|
|
3
|
+
"analytics": "Analytics"
|
|
4
|
+
},
|
|
5
|
+
"title": "Analytics",
|
|
6
|
+
"description": "Understand traffic and application activity without identifying visitors.",
|
|
7
|
+
"properties": "Properties",
|
|
8
|
+
"property": "Property",
|
|
9
|
+
"new_property": "New property",
|
|
10
|
+
"edit_property": "Edit property",
|
|
11
|
+
"no_properties": "Create a property to start measuring traffic. Tracking stays off until you enable it.",
|
|
12
|
+
"name": "Name",
|
|
13
|
+
"kind": "Tracking type",
|
|
14
|
+
"public": "Public pages",
|
|
15
|
+
"application": "Application usage",
|
|
16
|
+
"origins": "Website origins",
|
|
17
|
+
"origins_help": "One full origin per line, for example https://example.com.",
|
|
18
|
+
"routes": "Tracked routes",
|
|
19
|
+
"routes_help": "One path per line. Use :id for one variable segment, for example /records/:id. Unlisted routes are excluded.",
|
|
20
|
+
"events_config": "Custom events",
|
|
21
|
+
"events_help": "One event per line: download: format, category. Leave empty to collect pageviews only.",
|
|
22
|
+
"utm": "Campaign parameters",
|
|
23
|
+
"utm_help": "Choose only the campaign values you want to keep. Other query parameters are discarded.",
|
|
24
|
+
"enabled": "Enable tracking",
|
|
25
|
+
"enabled_help": "Tracking is off by default. Public pages and application usage use separate properties.",
|
|
26
|
+
"save": "Save property",
|
|
27
|
+
"saving": "Saving…",
|
|
28
|
+
"cancel": "Cancel",
|
|
29
|
+
"delete": "Delete property",
|
|
30
|
+
"delete_title": "Delete this property?",
|
|
31
|
+
"delete_description": "This permanently deletes the property and all its collected traffic and events.",
|
|
32
|
+
"deleting": "Deleting…",
|
|
33
|
+
"edit": "Edit",
|
|
34
|
+
"loading": "Loading analytics…",
|
|
35
|
+
"error": "Analytics could not be loaded. Try again.",
|
|
36
|
+
"refresh": "Refresh",
|
|
37
|
+
"no_data": "No activity in this period.",
|
|
38
|
+
"start": "Start date (UTC)",
|
|
39
|
+
"end": "End date (UTC)",
|
|
40
|
+
"all": "All",
|
|
41
|
+
"unknown": "Unknown",
|
|
42
|
+
"clear_filters": "Clear filters",
|
|
43
|
+
"filters": "Filter activity",
|
|
44
|
+
"page_filter": "Page path",
|
|
45
|
+
"event_filter": "Event name",
|
|
46
|
+
"pageviews": "Pageviews",
|
|
47
|
+
"visitors": "Estimated visitors",
|
|
48
|
+
"sessions": "Estimated sessions",
|
|
49
|
+
"events": "Custom events",
|
|
50
|
+
"estimates": "Visitors are daily estimates. Sessions reset after 30 minutes of inactivity or at UTC midnight.",
|
|
51
|
+
"period": "Traffic over time",
|
|
52
|
+
"date": "Date",
|
|
53
|
+
"count": "Count",
|
|
54
|
+
"value": "Value",
|
|
55
|
+
"recent": "Recent activity",
|
|
56
|
+
"recent_help": "Last five minutes; updates every 30 seconds while this page is visible.",
|
|
57
|
+
"recent_visitors": "Active visitors",
|
|
58
|
+
"time": "Time (UTC)",
|
|
59
|
+
"location": "Location",
|
|
60
|
+
"geo_title": "Visitor locations",
|
|
61
|
+
"geo_approximate": "Locations are approximate and based on network addresses, not device GPS.",
|
|
62
|
+
"geo_ready": "Location database available",
|
|
63
|
+
"geo_unavailable": "Location database unavailable. Activity is still collected with unknown locations.",
|
|
64
|
+
"geo_disabled": "Location reporting is disabled.",
|
|
65
|
+
"geo_age": "Database age: {days} days",
|
|
66
|
+
"setup": "Tracking setup",
|
|
67
|
+
"setup_help": "The demo host tracks matching routes once this property is enabled. Other hosts can install the analytics host adapter and send approved custom events with track(name, properties).",
|
|
68
|
+
"event_format_error": "Use one event per line: event: property, property.",
|
|
69
|
+
"dimensions": {
|
|
70
|
+
"path": "Pages",
|
|
71
|
+
"referrer": "Referrers",
|
|
72
|
+
"utm_source": "Campaign sources",
|
|
73
|
+
"utm_medium": "Campaign media",
|
|
74
|
+
"utm_campaign": "Campaigns",
|
|
75
|
+
"browser": "Browsers",
|
|
76
|
+
"os": "Operating systems",
|
|
77
|
+
"device": "Devices",
|
|
78
|
+
"country": "Countries",
|
|
79
|
+
"region": "Regions",
|
|
80
|
+
"city": "Cities",
|
|
81
|
+
"event": "Events"
|
|
82
|
+
},
|
|
83
|
+
"country": "Country",
|
|
84
|
+
"region": "Region",
|
|
85
|
+
"city": "City",
|
|
86
|
+
"status": "Status",
|
|
87
|
+
"tracking_on": "Tracking enabled",
|
|
88
|
+
"tracking_off": "Tracking paused",
|
|
89
|
+
"event_property": "Event property: {name}",
|
|
90
|
+
"ui": {
|
|
91
|
+
"overview": "Overview",
|
|
92
|
+
"sources": "Sources",
|
|
93
|
+
"audience": "Audience",
|
|
94
|
+
"campaigns": "Campaigns",
|
|
95
|
+
"date_range": "Date range",
|
|
96
|
+
"today": "Today",
|
|
97
|
+
"last_7_days": "Last 7 days",
|
|
98
|
+
"last_30_days": "Last 30 days",
|
|
99
|
+
"last_90_days": "Last 90 days",
|
|
100
|
+
"custom_range": "Custom dates",
|
|
101
|
+
"apply": "Apply",
|
|
102
|
+
"filters_button": "Filters",
|
|
103
|
+
"manage": "Manage property",
|
|
104
|
+
"close": "Close",
|
|
105
|
+
"show_more": "Show more",
|
|
106
|
+
"show_less": "Show less",
|
|
107
|
+
"chart_data": "View chart data",
|
|
108
|
+
"chart_metric": "Chart metric",
|
|
109
|
+
"active_now": "{count} active now",
|
|
110
|
+
"activity": "Activity",
|
|
111
|
+
"date_help": "Dates are shown in UTC. The end date must be on or after the start date, within 366 days.",
|
|
112
|
+
"clear_filter": "Remove {name} filter",
|
|
113
|
+
"settings": "Property settings",
|
|
114
|
+
"export_csv": "Export CSV",
|
|
115
|
+
"export_help": "Download the current report: totals, daily traffic and up to 50 rows per breakdown. Dates and filters are included.",
|
|
116
|
+
"export_failed": "CSV export failed. Try again."
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Analytics-owned tables, discovered by host migrations."""
|
|
2
|
+
|
|
3
|
+
from datetime import UTC, datetime
|
|
4
|
+
from uuid import uuid4
|
|
5
|
+
|
|
6
|
+
from simple_module_db import MultiTenantMixin, create_module_base
|
|
7
|
+
from sqlalchemy import JSON, Column, Index, UniqueConstraint
|
|
8
|
+
from sqlmodel import Field
|
|
9
|
+
|
|
10
|
+
Base = create_module_base("analytics")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def new_id() -> str:
|
|
14
|
+
return str(uuid4())
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def utcnow() -> datetime:
|
|
18
|
+
return datetime.now(UTC)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class AnalyticsProperty(Base, MultiTenantMixin, table=True):
|
|
22
|
+
__tablename__ = "analytics_property"
|
|
23
|
+
id: str = Field(default_factory=new_id, primary_key=True, max_length=36)
|
|
24
|
+
name: str = Field(max_length=100)
|
|
25
|
+
kind: str = Field(default="public", max_length=20)
|
|
26
|
+
origins: list = Field(default_factory=list, sa_column=Column(JSON, nullable=False))
|
|
27
|
+
enabled: bool = False
|
|
28
|
+
route_patterns: list = Field(default_factory=list, sa_column=Column(JSON, nullable=False))
|
|
29
|
+
event_allowlist: dict = Field(default_factory=dict, sa_column=Column(JSON, nullable=False))
|
|
30
|
+
utm_parameters: list = Field(default_factory=list, sa_column=Column(JSON, nullable=False))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class AnalyticsSession(Base, MultiTenantMixin, table=True):
|
|
34
|
+
__tablename__ = "analytics_session"
|
|
35
|
+
__table_args__ = (
|
|
36
|
+
Index("ix_analytics_session_visitor", "property_id", "tenant_id", "visitor", "last_seen"),
|
|
37
|
+
Index("ix_analytics_session_retention", "tenant_id", "last_seen", "id"),
|
|
38
|
+
)
|
|
39
|
+
id: str = Field(default_factory=new_id, primary_key=True, max_length=36)
|
|
40
|
+
property_id: str = Field(foreign_key="analytics_property.id", max_length=36)
|
|
41
|
+
visitor: str = Field(max_length=64)
|
|
42
|
+
started_at: datetime = Field(default_factory=utcnow)
|
|
43
|
+
last_seen: datetime = Field(default_factory=utcnow)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class AnalyticsEvent(Base, MultiTenantMixin, table=True):
|
|
47
|
+
__tablename__ = "analytics_event"
|
|
48
|
+
__table_args__ = (
|
|
49
|
+
UniqueConstraint("tenant_id", "property_id", "event_id", name="uq_analytics_event_id"),
|
|
50
|
+
Index("ix_analytics_event_time", "property_id", "tenant_id", "created_at"),
|
|
51
|
+
Index("ix_analytics_event_retention", "tenant_id", "created_at", "id"),
|
|
52
|
+
*(
|
|
53
|
+
Index(
|
|
54
|
+
f"ix_analytics_event_{dimension}",
|
|
55
|
+
"tenant_id",
|
|
56
|
+
"property_id",
|
|
57
|
+
dimension,
|
|
58
|
+
"created_at",
|
|
59
|
+
)
|
|
60
|
+
for dimension in ("country", "region", "city", "path", "name")
|
|
61
|
+
),
|
|
62
|
+
)
|
|
63
|
+
id: str = Field(default_factory=new_id, primary_key=True, max_length=36)
|
|
64
|
+
property_id: str = Field(foreign_key="analytics_property.id", max_length=36)
|
|
65
|
+
session_id: str = Field(foreign_key="analytics_session.id", max_length=36, index=True)
|
|
66
|
+
event_id: str = Field(max_length=36)
|
|
67
|
+
visitor: str = Field(max_length=64)
|
|
68
|
+
created_at: datetime = Field(default_factory=utcnow)
|
|
69
|
+
kind: str = Field(max_length=10)
|
|
70
|
+
path: str = Field(max_length=500)
|
|
71
|
+
name: str = Field(default="", max_length=50)
|
|
72
|
+
properties: dict = Field(default_factory=dict, sa_column=Column(JSON, nullable=False))
|
|
73
|
+
referrer: str = Field(default="", max_length=253)
|
|
74
|
+
browser: str = Field(default="Other", max_length=30)
|
|
75
|
+
os: str = Field(default="Other", max_length=30)
|
|
76
|
+
device: str = Field(default="desktop", max_length=10)
|
|
77
|
+
utm_source: str = Field(default="", max_length=200)
|
|
78
|
+
utm_medium: str = Field(default="", max_length=200)
|
|
79
|
+
utm_campaign: str = Field(default="", max_length=200)
|
|
80
|
+
utm_term: str = Field(default="", max_length=200)
|
|
81
|
+
utm_content: str = Field(default="", max_length=200)
|
|
82
|
+
country: str = Field(default="", max_length=2)
|
|
83
|
+
country_name: str = Field(default="", max_length=200)
|
|
84
|
+
region: str = Field(default="", max_length=20)
|
|
85
|
+
region_name: str = Field(default="", max_length=200)
|
|
86
|
+
city: str = Field(default="", max_length=20)
|
|
87
|
+
city_name: str = Field(default="", max_length=200)
|
sm_analytics/module.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"""Module discovery, settings, guards, permissions and lifecycle."""
|
|
2
|
+
|
|
3
|
+
import asyncio
|
|
4
|
+
from importlib import metadata, resources
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from fastapi import APIRouter, Depends, FastAPI
|
|
8
|
+
from simple_module_core.body_limits import BodyLimitRegistry
|
|
9
|
+
from simple_module_core.menu import MenuItem, MenuRegistry
|
|
10
|
+
from simple_module_core.module import ModuleBase, ModuleMeta
|
|
11
|
+
from simple_module_core.module_settings import register_module_settings
|
|
12
|
+
from simple_module_core.permissions import PermissionRegistry
|
|
13
|
+
from simple_module_core.public_routes import PublicRouteRegistry
|
|
14
|
+
from simple_module_core.rate_limit import RateLimitRegistry, RateLimitRule
|
|
15
|
+
from simple_module_hosting.csrf import get_csrf_token
|
|
16
|
+
from simple_module_hosting.inertia_deps import InertiaDep
|
|
17
|
+
from simple_module_hosting.permissions import RequiresPermission, resolved_permissions_for
|
|
18
|
+
from starlette.requests import Request
|
|
19
|
+
|
|
20
|
+
from sm_analytics.core import constants as c
|
|
21
|
+
from sm_analytics.core.runtime import Runtime
|
|
22
|
+
from sm_analytics.core.settings import AnalyticsSettings
|
|
23
|
+
from sm_analytics.core.tenancy import bind_admin
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class AnalyticsModule(ModuleBase):
|
|
27
|
+
meta = ModuleMeta(
|
|
28
|
+
name=c.MODULE_NAME,
|
|
29
|
+
route_prefix=c.API_PREFIX,
|
|
30
|
+
view_prefix=c.VIEW_PREFIX,
|
|
31
|
+
depends_on=[c._MODULE_SETTINGS],
|
|
32
|
+
version=metadata.version("simple_module_analytics"),
|
|
33
|
+
requires_framework=">=1.0,<2.0",
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
def register_settings(self, app: FastAPI) -> None:
|
|
37
|
+
register_module_settings(app, c.PACKAGE, AnalyticsSettings, Runtime)
|
|
38
|
+
|
|
39
|
+
def register_routes(self, api_router: APIRouter, view_router: APIRouter) -> None:
|
|
40
|
+
from sm_analytics.web.endpoints.admin import router as admin
|
|
41
|
+
from sm_analytics.web.endpoints.public import router as public
|
|
42
|
+
|
|
43
|
+
api_router.include_router(admin)
|
|
44
|
+
api_router.include_router(public)
|
|
45
|
+
|
|
46
|
+
@view_router.get(
|
|
47
|
+
"/",
|
|
48
|
+
response_model=None,
|
|
49
|
+
dependencies=[Depends(bind_admin), Depends(RequiresPermission(c.PERM_VIEW))],
|
|
50
|
+
)
|
|
51
|
+
async def dashboard(request: Request, inertia: InertiaDep):
|
|
52
|
+
permissions = resolved_permissions_for(request)
|
|
53
|
+
return await inertia.render(
|
|
54
|
+
c._PAGE_DASHBOARD,
|
|
55
|
+
{
|
|
56
|
+
"can_manage": "*" in permissions or c.PERM_MANAGE in permissions,
|
|
57
|
+
"csrf_token": get_csrf_token(request) or "",
|
|
58
|
+
},
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def register_permissions(self, registry: PermissionRegistry) -> None:
|
|
62
|
+
registry.add_group(c.MODULE_NAME, [c.PERM_VIEW, c.PERM_MANAGE])
|
|
63
|
+
|
|
64
|
+
def register_menu_items(self, registry: MenuRegistry) -> None:
|
|
65
|
+
registry.add(
|
|
66
|
+
MenuItem(
|
|
67
|
+
label="Analytics",
|
|
68
|
+
label_key="analytics.menu.analytics",
|
|
69
|
+
url=f"{c.VIEW_PREFIX}/",
|
|
70
|
+
icon="bar-chart-3",
|
|
71
|
+
order=117,
|
|
72
|
+
permissions=[c.PERM_VIEW],
|
|
73
|
+
)
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
def register_public_routes(self, registry: PublicRouteRegistry) -> None:
|
|
77
|
+
registry.add_exact(c.COLLECT_PATH, methods={"POST"})
|
|
78
|
+
registry.add_exact(c.CONFIG_PATH, methods={"GET"})
|
|
79
|
+
|
|
80
|
+
def register_body_limits(self, registry: BodyLimitRegistry) -> None:
|
|
81
|
+
registry.add_exact(c.COLLECT_PATH, c.MAX_BODY_BYTES, methods={"POST"})
|
|
82
|
+
|
|
83
|
+
def register_rate_limits(self, registry: RateLimitRegistry) -> None:
|
|
84
|
+
registry.add_exact(
|
|
85
|
+
c.COLLECT_PATH, RateLimitRule("120/minute", key="ip", methods=frozenset({"POST"}))
|
|
86
|
+
)
|
|
87
|
+
registry.add_exact(
|
|
88
|
+
c.CONFIG_PATH, RateLimitRule("120/minute", key="ip", methods=frozenset({"GET"}))
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
def locale_dirs(self) -> dict[str, Path]:
|
|
92
|
+
return {"analytics": Path(str(resources.files("sm_analytics") / "locales"))}
|
|
93
|
+
|
|
94
|
+
async def on_startup(self, app: FastAPI) -> None:
|
|
95
|
+
state = app.state.sm_analytics
|
|
96
|
+
state.geo.path = state.settings.geo_database_path
|
|
97
|
+
state.geo.enabled = state.settings.geo_enabled
|
|
98
|
+
state.geo.status()
|
|
99
|
+
if state.settings.scheduler_enabled:
|
|
100
|
+
state.task = asyncio.create_task(state.run(app.state.sm.db.session_factory))
|
|
101
|
+
|
|
102
|
+
async def on_shutdown(self, app: FastAPI) -> None:
|
|
103
|
+
await app.state.sm_analytics.stop()
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@simple-module-py/analytics",
|
|
3
|
+
"version": "0.0.15",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "Anonymous traffic and application analytics",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./sm_analytics/index.ts"
|
|
8
|
+
},
|
|
9
|
+
"peerDependencies": {
|
|
10
|
+
"@inertiajs/react": "^3.9.1",
|
|
11
|
+
"@simple-module-py/i18n": "*",
|
|
12
|
+
"@simple-module-py/ui": "*",
|
|
13
|
+
"lucide-react": "^1.43.0",
|
|
14
|
+
"react": "^19.3.1",
|
|
15
|
+
"react-dom": "^19.3.1",
|
|
16
|
+
"recharts": "^3.10.1"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@simple-module-py/tsconfig": "*"
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { Head } from '@inertiajs/react';
|
|
2
|
+
import { PageShell } from '@simple-module-py/ui/components/PageShell';
|
|
3
|
+
import { Button } from '@simple-module-py/ui/components/ui/button';
|
|
4
|
+
import { Label } from '@simple-module-py/ui/components/ui/label';
|
|
5
|
+
import { AuthenticatedLayout } from '@simple-module-py/ui/layouts/AuthenticatedLayout';
|
|
6
|
+
import { DateRangeControl } from '../ui/components/DateRangeControl';
|
|
7
|
+
import { FilterChips } from '../ui/components/FilterChips';
|
|
8
|
+
import { PropertySettings } from '../ui/components/PropertySettings';
|
|
9
|
+
import { ReportExport } from '../ui/components/ReportExport';
|
|
10
|
+
import { ReportFilters } from '../ui/components/ReportFilters';
|
|
11
|
+
import { ReportView } from '../ui/components/ReportView';
|
|
12
|
+
import { deleteProperty, saveProperty } from '../ui/utils/api';
|
|
13
|
+
import { keys, useT } from '../ui/utils/i18n';
|
|
14
|
+
import { useAnalytics } from '../ui/utils/useAnalytics';
|
|
15
|
+
|
|
16
|
+
export default function Dashboard({ can_manage = false }: { can_manage?: boolean }) {
|
|
17
|
+
const { t } = useT();
|
|
18
|
+
const data = useAnalytics();
|
|
19
|
+
const property = data.properties.find((item) => item.id === data.selected);
|
|
20
|
+
const settings = (
|
|
21
|
+
<PropertySettings
|
|
22
|
+
key={property?.id ?? 'new'}
|
|
23
|
+
canManage={can_manage}
|
|
24
|
+
property={property}
|
|
25
|
+
geo={data.geo}
|
|
26
|
+
onSave={async (input, id) => {
|
|
27
|
+
const saved = await saveProperty(input, id);
|
|
28
|
+
data.reload();
|
|
29
|
+
data.setSelected(saved.id);
|
|
30
|
+
if (saved.id !== data.selected)
|
|
31
|
+
data.setFilters({
|
|
32
|
+
...data.filters,
|
|
33
|
+
path: '',
|
|
34
|
+
event: '',
|
|
35
|
+
country: '',
|
|
36
|
+
region: '',
|
|
37
|
+
city: '',
|
|
38
|
+
});
|
|
39
|
+
}}
|
|
40
|
+
onDelete={async () => {
|
|
41
|
+
if (!property) return;
|
|
42
|
+
await deleteProperty(property.id);
|
|
43
|
+
data.setSelected('');
|
|
44
|
+
data.reload();
|
|
45
|
+
}}
|
|
46
|
+
/>
|
|
47
|
+
);
|
|
48
|
+
return (
|
|
49
|
+
<AuthenticatedLayout>
|
|
50
|
+
<Head title={t(keys.analytics.title)} />
|
|
51
|
+
<PageShell title={t(keys.analytics.title)}>
|
|
52
|
+
<div className="min-w-0 space-y-5">
|
|
53
|
+
<div className="flex flex-wrap items-center justify-between gap-3">
|
|
54
|
+
<div className="flex min-w-0 flex-wrap items-center gap-x-4 gap-y-2">
|
|
55
|
+
<div className="min-w-0 max-w-full">
|
|
56
|
+
<Label htmlFor="analytics-property" className="sr-only">
|
|
57
|
+
{t(keys.analytics.property)}
|
|
58
|
+
</Label>
|
|
59
|
+
<select
|
|
60
|
+
id="analytics-property"
|
|
61
|
+
className="h-9 w-full max-w-72 rounded-md border bg-background px-3 text-sm font-medium"
|
|
62
|
+
value={data.selected}
|
|
63
|
+
onChange={(event) => {
|
|
64
|
+
data.setSelected(event.target.value);
|
|
65
|
+
data.setFilters({
|
|
66
|
+
...data.filters,
|
|
67
|
+
path: '',
|
|
68
|
+
event: '',
|
|
69
|
+
country: '',
|
|
70
|
+
region: '',
|
|
71
|
+
city: '',
|
|
72
|
+
});
|
|
73
|
+
}}
|
|
74
|
+
>
|
|
75
|
+
<option value="" disabled>
|
|
76
|
+
{t(keys.analytics.properties)}
|
|
77
|
+
</option>
|
|
78
|
+
{data.properties.map((item) => (
|
|
79
|
+
<option key={item.id} value={item.id}>
|
|
80
|
+
{item.name}
|
|
81
|
+
</option>
|
|
82
|
+
))}
|
|
83
|
+
</select>
|
|
84
|
+
</div>
|
|
85
|
+
{property && (
|
|
86
|
+
<span
|
|
87
|
+
role="status"
|
|
88
|
+
aria-label={t(keys.analytics.status)}
|
|
89
|
+
className="flex items-center gap-2 text-xs text-muted-foreground"
|
|
90
|
+
title={t(
|
|
91
|
+
property.kind === 'public' ? keys.analytics.public : keys.analytics.application,
|
|
92
|
+
)}
|
|
93
|
+
>
|
|
94
|
+
<span
|
|
95
|
+
aria-hidden="true"
|
|
96
|
+
className={`size-2 rounded-full ${property.enabled ? 'bg-emerald-500' : 'bg-muted-foreground'}`}
|
|
97
|
+
/>
|
|
98
|
+
{t(property.enabled ? keys.analytics.tracking_on : keys.analytics.tracking_off)}
|
|
99
|
+
</span>
|
|
100
|
+
)}
|
|
101
|
+
{data.report && (
|
|
102
|
+
<span className="text-xs tabular-nums text-muted-foreground">
|
|
103
|
+
{t(keys.analytics.ui.active_now, { count: data.report.recent.visitors })}
|
|
104
|
+
</span>
|
|
105
|
+
)}
|
|
106
|
+
</div>
|
|
107
|
+
<div className="flex max-w-full flex-wrap items-center gap-2">
|
|
108
|
+
<Button variant="ghost" size="sm" onClick={data.reload}>
|
|
109
|
+
{t(keys.analytics.refresh)}
|
|
110
|
+
</Button>
|
|
111
|
+
{property && (
|
|
112
|
+
<>
|
|
113
|
+
<DateRangeControl value={data.filters} onChange={data.setFilters} />
|
|
114
|
+
<ReportFilters
|
|
115
|
+
value={data.filters}
|
|
116
|
+
onChange={data.setFilters}
|
|
117
|
+
report={data.report}
|
|
118
|
+
/>
|
|
119
|
+
<ReportExport
|
|
120
|
+
property={property}
|
|
121
|
+
filters={data.filters}
|
|
122
|
+
report={data.report}
|
|
123
|
+
busy={data.loading || Boolean(data.error)}
|
|
124
|
+
/>
|
|
125
|
+
{settings}
|
|
126
|
+
</>
|
|
127
|
+
)}
|
|
128
|
+
</div>
|
|
129
|
+
</div>
|
|
130
|
+
{data.error && (
|
|
131
|
+
<p
|
|
132
|
+
role="alert"
|
|
133
|
+
className="rounded-md border border-destructive p-3 text-sm text-destructive"
|
|
134
|
+
>
|
|
135
|
+
{data.error}
|
|
136
|
+
</p>
|
|
137
|
+
)}
|
|
138
|
+
{data.geo && (!data.geo.enabled || !data.geo.available) && (
|
|
139
|
+
<aside className="text-sm text-muted-foreground">
|
|
140
|
+
{t(!data.geo.enabled ? keys.analytics.geo_disabled : keys.analytics.geo_unavailable)}
|
|
141
|
+
</aside>
|
|
142
|
+
)}
|
|
143
|
+
{!data.loading && data.properties.length === 0 && (
|
|
144
|
+
<div className="space-y-4 py-12 text-center">
|
|
145
|
+
<p className="text-muted-foreground">{t(keys.analytics.no_properties)}</p>
|
|
146
|
+
{settings}
|
|
147
|
+
</div>
|
|
148
|
+
)}
|
|
149
|
+
{property && (
|
|
150
|
+
<FilterChips value={data.filters} onChange={data.setFilters} report={data.report} />
|
|
151
|
+
)}
|
|
152
|
+
{data.loading && !data.report && (
|
|
153
|
+
<p role="status" className="py-8 text-center text-sm text-muted-foreground">
|
|
154
|
+
{t(keys.analytics.loading)}
|
|
155
|
+
</p>
|
|
156
|
+
)}
|
|
157
|
+
{data.report && (
|
|
158
|
+
<ReportView report={data.report} filters={data.filters} onFilter={data.setFilters} />
|
|
159
|
+
)}
|
|
160
|
+
</div>
|
|
161
|
+
</PageShell>
|
|
162
|
+
</AuthenticatedLayout>
|
|
163
|
+
);
|
|
164
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Analytics services."""
|