Skip to content

Add Matomo retention dimension: Days Since Registration#12227

Merged
mekarpeles merged 9 commits intomasterfrom
12144/feature/retention-metrics
Apr 3, 2026
Merged

Add Matomo retention dimension: Days Since Registration#12227
mekarpeles merged 9 commits intomasterfrom
12144/feature/retention-metrics

Conversation

@mekarpeles
Copy link
Copy Markdown
Member

Closes #12144

Wire up Matomo visit-scoped custom dimension 1 ("Days Since Registration") to enable patron retention analytics on openlibrary.org.

Technical

The dimension was already created in Matomo admin (dimension ID 1, visit-scoped). This PR adds the server-side bucketing logic and the JS call to send it.

openlibrary/plugins/openlibrary/code.py

  • get_patron_status(user) — maps a patron's account age to one of seven dimension values: visitor, d0, d1+, d7+, d14+, d30+, d90+. Reads user.created directly (infogami stores this as a datetime.datetime). Falls back to d90+ on any error.
  • setup_contextvars processor — computes web.ctx.patron_status once per request after the user session is resolved, so it's available to all templates.
  • setup_context_defaults — adds patron_status: 'visitor' as a safe default.

openlibrary/templates/site/head.html

  • Inside the existing $if not is_bot() guard, initializes _paq and calls setCustomDimension(1, '$ctx.patron_status') before the Matomo Tag Manager container loads. Because the MTM container script is async, the _paq queue is populated first and processed in order when the tracker initializes — so the dimension is set before trackPageView fires.

No individual user tracking; no user IDs or hashes. Purely aggregate, compatible with IP anonymization.

Testing

  1. Log out → view any page → check Matomo real-time visitor log → dimension 1 should be visitor
  2. Log in with a brand-new account → view any page → dimension 1 should be d0
  3. Log in with an account created 30+ days ago → dimension 1 should be d30+ (or appropriate bucket)
  4. Run unit tests: pytest openlibrary/plugins/openlibrary/tests/test_patron_status.py -v

Screenshot

N/A — analytics-only change, no UI.

Stakeholders

@mekarpeles

mekarpeles and others added 2 commits March 31, 2026 00:17
- Add get_patron_status(user) helper that buckets patron account age
  into visit-scoped Matomo dimension values: visitor, d0, d1+, d7+,
  d14+, d30+, d90+
- Set web.ctx.patron_status per-request in setup_contextvars processor
- Add patron_status default ('visitor') to context.defaults
- Push _paq.setCustomDimension(1, patron_status) in head.html before
  the Matomo Tag Manager container fires trackPageView
- Add unit tests covering all bucket boundaries and error cases
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a Matomo visit-scoped custom dimension (“Days Since Registration”, dimension ID 1) to enable retention analytics by bucketing a patron’s account age and sending that bucket with pageview tracking.

Changes:

  • Add get_patron_status(user) helper to bucket account age into visitor, d0, d1+, d7+, d14+, d30+, d90+.
  • Compute and store patron_status during request setup and provide a safe default in context defaults.
  • Send the computed bucket to Matomo via _paq.setCustomDimension(1, ...) in the site <head>.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
openlibrary/plugins/openlibrary/code.py Adds bucketing helper + sets web.ctx.patron_status per request + adds default context value
openlibrary/templates/site/head.html Pushes Matomo custom dimension 1 to _paq before tag manager loads
openlibrary/plugins/openlibrary/tests/test_patron_status.py Adds unit tests for bucketing behavior and fallbacks

Comment thread openlibrary/templates/site/head.html Outdated
Comment thread openlibrary/plugins/openlibrary/code.py Outdated
Comment thread openlibrary/plugins/openlibrary/tests/test_patron_status.py
@mekarpeles mekarpeles marked this pull request as ready for review March 31, 2026 19:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comment thread openlibrary/plugins/openlibrary/code.py
Comment thread openlibrary/accounts/__init__.py
Comment thread openlibrary/plugins/openlibrary/tests/test_patron_status.py
Copy link
Copy Markdown
Collaborator

@cdrini cdrini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@mekarpeles mekarpeles merged commit 5881e3e into master Apr 3, 2026
7 checks passed
@mekarpeles mekarpeles deleted the 12144/feature/retention-metrics branch April 3, 2026 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Matomo Visitor-Type Analytics {Visitor, Registrant, Returning}

3 participants