You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
plain-connect: add pageview tracking via {% connect_pageviews %}
Adds anonymous pageview analytics with optional signed-in user
attribution, injected with the {% connect_pageviews %} template tag and
reported to the pageview beacon endpoint.
The identity token encrypts the authenticated user's id (AES-256-GCM) so
the raw id never appears in page HTML; the user is read via
plain.auth.get_request_user(), with a lazy import so apps without
plain.auth still work. Includes a test suite covering identity
encryption and the rendered tag.
Also renames the stale PLAIN_CLOUD_EXPORT_ENABLED env var to
PLAIN_CONNECT_EXPORT_ENABLED.
All settings can be set via `PLAIN_`-prefixed environment variables or in `app/settings.py`.
36
40
@@ -52,6 +56,34 @@ Metrics are not affected by sampling — histograms aggregate in-process and exp
52
56
53
57
**Logs** — Records from the `plain` and `app` loggers, plus anything propagating to the root logger, are bridged into OTLP log records and exported with `trace_id` / `span_id` set from the active span. The minimum severity is controlled by `CONNECT_LOG_LEVEL` (default `INFO`); the root logger's level is widened to that floor when needed so libraries using `getLogger(__name__)` reach the exporter. To prevent feedback loops, two sources are skipped on the export path: the `opentelemetry` namespace, and any record emitted from inside the OTLP exporter's background thread (e.g. urllib3 connection errors raised by the exporter's own HTTP call). Your application's urllib3 logs are exported normally.
54
58
59
+
## Pageview tracking
60
+
61
+
plain.connect can track page views in your app — anonymous page-view analytics plus, optionally, attribution to your signed-in users. This is independent of the OTLP export above.
62
+
63
+
Add the tag to your base template, just before `</body>`:
64
+
65
+
```html
66
+
{% connect_pageviews %}
67
+
```
68
+
69
+
Then set the public endpoint token:
70
+
71
+
```
72
+
PLAIN_CONNECT_PAGEVIEWS_TOKEN=plain_pv_...
73
+
```
74
+
75
+
The tag renders nothing until the token is set. Once enabled, it reports the URL, title, referrer, and a first-party anonymous id on each page load and SPA navigation (History `pushState` / back-forward).
76
+
77
+
### User attribution
78
+
79
+
To attribute pageviews to your signed-in users, also set the secret identity key:
80
+
81
+
```
82
+
PLAIN_CONNECT_PAGEVIEWS_IDENTITY_KEY=...
83
+
```
84
+
85
+
When set, the tag encrypts the signed-in user's id (AES-256-GCM) into an opaque token that only the pageview endpoint can decrypt — the raw id never appears in your HTML. The user is read from [plain.auth](../../plain-auth/plain/auth/README.md); apps without it (or anonymous visitors) are still counted, their pageviews simply carry no user id.
86
+
55
87
## Observer coexistence
56
88
57
89
If [plain.observer](../../plain-observer/plain/observer/README.md) is also installed, both work simultaneously. plain.connect handles production export while observer provides the local dev toolbar and admin trace viewer. Observer detects the existing TracerProvider and layers its sampler and span processor on top.
0 commit comments