Repository navigation
Fix dspace-customers#1083 R23: load Matomo only after Klaro consent, never replay declined activity - #1668
Merged
Conversation
added 2 commits
October 27, 2026 14:53
main.browser.ts loaded matomo.js and queued tracking on every page load, before and regardless of cookie consent; Klaro had no Matomo service. - Klaro gets an opt-in "matomo" service whose callback drives MatomoConsentTracker. - The tracker loads matomo.js only with consent and a configured hostUrl and siteId. Without consent nothing is pushed to _paq; only the current page is kept and tracked once consent is given. - Revocation calls forgetConsentGiven, and a request processor empties requests built while consent is revoked, so Matomo's consent queue cannot replay them after a later accept. - enableCookieConsentPopup: false keeps tracking without consent.
- Reset the pending item dimension on every consent change, and let each page_view event set or clear it, so an identifier seen in one consent period cannot be attached to a page view in another. - Push setConsentGiven as a function: Matomo runs queued 'setConsentGiven' names before other commands, so accept, revoke, accept before matomo.js loaded left Matomo in the revoked state. - Treat a blank hostUrl or siteId as not configured. - Test that BrowserInitService routes Angulartics Matomo tracking through the consent gate. - Define MATOMO_KLARO_KEY next to the other Klaro service keys.
There was a problem hiding this comment.
🔵 Needs a closer look
Human production-browser verification is needed to confirm the Matomo/Klaro integration, network behavior, and accept/revoke/re-accept ordering under asynchronous callback semantics.
No open issues 🎉
What changed in this PR
This pull request gates Matomo analytics behind Klaro consent, preventing Matomo from loading or replaying activity from periods when consent was declined while preserving legacy tracking when the consent popup is disabled.
Changes:
- Added
MatomoConsentTrackerfor consent-aware loading, page tracking, custom dimensions, and request filtering. - Routed Angulartics Matomo tracking through the consent tracker and removed unconditional initialization.
- Registered Matomo as an optional Klaro service.
- Added English and Czech consent translations.
- Added tests covering consent, revocation, configuration, legacy mode, and browser initialization.
Review note: the Matomo translation keys are currently provided only for English and Czech; other active locales may display raw translation keys in the consent dialog.
| File | Description |
|---|---|
src/modules/app/browser-init.service.ts |
Routes Angulartics Matomo events through the consent tracker. |
src/modules/app/browser-init.service.spec.ts |
Verifies browser initialization uses the consent gate. |
src/main.browser.ts |
Removes unconditional Matomo loading while preserving _paq initialization. |
src/assets/i18n/en.json5 |
Adds English Matomo consent labels. |
src/assets/i18n/cs.json5 |
Adds Czech Matomo consent labels. |
src/app/statistics/matomo-consent-tracker.ts |
Implements consent-aware Matomo loading, tracking, and request filtering. |
src/app/statistics/matomo-consent-tracker.spec.ts |
Tests consent transitions, deferred tracking, revocation, configuration, and Klaro wiring. |
src/app/shared/cookies/klaro-configuration.ts |
Adds the optional Matomo Klaro service and consent callback. |
Review effort: Lite
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
References
dspace-customers issue 1083, finding R23.
No upstream fix:
src/app/statistics/matomo.service.tsdoes not exist ondspace-7_x/dspace-8_x. It is built on Orejime,ngx-matomo-clientand Angular 18 standalone config, not Klaro,angulartics2and NgModules, so nothing cherry-picks.matomo.jswhatever the consent and togglessetConsentGiven/forgetConsentGiven, whose queue replays requests from a revoked period.clarin-v7still loads Matomo unconditionally insrc/main.browser.ts; its PRs and issues for "matomo", "klaro" and "consent" contain no fix.Description
main.browser.tsloadedmatomo.jsand queued page views on every page load, before and regardless of cookie consent. Klaro had no Matomo service, so declining did not stop it. Matomo's own consent API would not be enough: commands pushed to_paqrun when the script loads, and requests made while consent is missing are queued in memory and sent onsetConsentGiven.Fix:
matomoservice (statistical). Its callback reports consent to the newMatomoConsentTracker.matomo.jsonly with consent and a non-blankhostUrlandsiteId. Without consent nothing is pushed to_paq. Only the current page (path and item dimension) is kept, and it is tracked once consent is given.forgetConsentGiven. AsetCustomRequestProcessinghook empties every request built while consent is revoked, and Matomo never sends an empty request, so clicks from that period are not replayed after a later accept.setConsentGivenis pushed as a function, so it keeps its queue position. As a string, Matomo would run it before an earlier queuedforgetConsentGiven.Behaviour changes:
matomoentry. Until they save, all optional services count as declined.ping=1), which carries the last consented page URL.Unchanged:
info.enableCookieConsentPopup: falsestill tracks without consent.Instructions for Reviewers
Changes:
src/app/statistics/matomo-consent-tracker.ts(new): the consent gate. It holds the code previously inmain.browser.tsandBrowserInitService.init().src/main.browser.ts: unconditionaladdMatomoStatistics()removed._paqis still initialised, becauseAngulartics2Matomowarns "Matomo not found" without it.src/modules/app/browser-init.service.ts: routesAngulartics2Matomo.eventTrack/pageTrackthrough the tracker.src/app/shared/cookies/klaro-configuration.ts:matomoKlaro service.src/assets/i18n/en.json5,cs.json5: Klaro title and description for the new service. Without them the consent modal shows the raw keys.Tests:
matomo-consent-tracker.spec.ts:should load Matomo and track the current page only once the visitor consents: no script and no_paqcommand before consent. On accept, the current page and its item dimension are tracked.should stop tracking on revocation and never send activity from the declined period: pages and dimensions seen while declined are not tracked after re-accept, and requests built while declined are emptied.should not load Matomo without a tracker URL and site ID: "valid config and consent", including a blanksiteId.should track without Klaro consent when the consent popup is explicitly disabled: legacy mode is kept.Klaro service should ask for individual opt-in consent and forward it to the tracker: the Klaro wiring.browser-init.service.spec.ts:should route Angulartics Matomo tracking through the consent gate: the realinit()pushes nothing to_paqbefore consent.How to test:
matomo.jsormatomo.phpappears.matomo.jsloads and one page view for the current page is sent.ping=1) is sent; no download or page from step 4.Local run (Node 18):
browser-init.service.spec.tstest fails on dtq-dev'sBrowserInitService;main.browser.tsin headless Chrome requestsmatomo.jsand gets a tracker cookie with no consent.browser-klaro.service.spec.tspass, 26/26;eslinton changed files: 0 errors, no new warnings;ng build --configuration production: OK.matomo.jsloads leaves tracking working;_pk_*cookie remains.Not in scope:
forgetConsentGivenalready deletes Matomo's_pk_*cookies in its configured scope; parent-domain or other-path cookies set by other trackers are not touched.matomo.jsload. This is unchanged and fails closed.hostUrl.