Skip to content

Keep existing visitor ID cookie when requireCookieConsent is followed by setCookieConsentGiven - #25460

Open
AltamashShaikh wants to merge 7 commits into
6.x-devfrom
DEV-13943
Open

AltamashShaikh wants to merge 7 commits into
6.x-devfrom
DEV-13943

Conversation

@AltamashShaikh

@AltamashShaikh AltamashShaikh commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

Problem: When the JS tracker is set up with requireCookieConsent + setCookieConsentGiven and an additional tracker is registered with addTracker, the visitor ID cookie (_pk_id.*) is replaced by a new one on every page load, so returning visitors are counted as new visitors. With a single tracker the visitor ID is kept.

Root cause: requireCookieConsent() called disableCookies(), which immediately deletes all tracking cookies when the tracker already knows its site ID. The _paq queue applies addTracker first, so the added tracker already has a site ID when requireCookieConsent runs and it deletes the shared visitor ID cookie (same site ID and cookie domain/path). The first tracker only gets its site ID later, which is why the single-tracker setup was not affected. The setCookieConsentGiven call that follows re-enables cookies, but by then the old visitor ID is gone and a new one is generated.

Fix: requireCookieConsent() now only disables cookies (configCookiesDisabled = true) and doesn't delete them right away. This matches what requireConsent() already does. If consent is not given, the existing cookies are still deleted on the next tracking request, because getRequest() calls deleteCookies() whenever cookies are disabled. disableCookies() and forgetCookieConsentGiven() still delete cookies immediately. The minified files (js/piwik.min.js, piwik.js, matomo.js) were regenerated with the YUICompressor command from js/README.md. The only change in the minified output is this one statement.

Edge cases considered:

  • Consent required but never given on the page: existing cookies are still removed on the next tracking request. Checked end-to-end with one tracker and with addTracker: no _pk_* cookies remain after trackPageView.
  • Consent remembered via rememberCookieConsentGiven: unchanged (requireCookieConsent returns early).
  • disableCookies() / forgetCookieConsentGiven(): still delete cookies immediately (covered by an assertion in the new test).
  • Single tracker setup: unchanged. The visitor ID persists as before.

How it was tested:

  • New QUnit test Test API - requireCookieConsent keeps existing visitor ID cookie when consent is given in tests/javascript/index.php. It fails on 6.x-dev (cookie deleted, new visitor ID generated: 2 failed assertions) and passes with the fix (8/8).
  • Full JS tracker suite (tests/javascript/index.php, headless Chromium; the isInsideAnIframe test was excluded because it hangs in headless mode in this environment): with the fix, 2858/2969 assertions passed. On the base, 2856/2969 passed. Every remaining failure also fails on the base and comes from the local test URL being served from a sub-directory (TagManager URL/dom/templates, content tracking URL resolution, Overlay URL normalizer, getPiwikUrl). JSLint validation of js/piwik.js passes.
  • ./vendor/bin/phpunit --filter testPiwikJs tests/PHPUnit/Integration/ReleaseCheckListTest.php: OK (minified files up to date, piwik.js == matomo.js).
  • End-to-end: a page with the snippet from the issue (requireCookieConsent, setCookieConsentGiven, trackPageView, addTracker) was reloaded 3 times in headless Chromium. Before the fix the visitor ID changed on every load. After the fix it stays the same, just like the single-tracker setup.
  • phpcs on tests/javascript/index.php: clean (the directory is excluded by phpcs.xml). There are no PHP source changes, so phpstan was not applicable.

Jira: DEV-13943

Fixes #19511

Checklist

  • [✔] I have understood, reviewed, and tested all AI outputs before use
  • [✔] All AI instructions respect security, IP, and privacy rules

Review

What Claude verified

  • matomo-review (skill): verdict Yes, merge readiness Ready, blocking findings: none.
  • Applied rule sets: matomo-test-runner (regression test present and verified to target the bug condition, clean); matomo-code-quality (tests/javascript/index.php is a PHP file but only inline JS changed, and tests/javascript/* is excluded from phpcs.xml, clean); matomo-security-rules (privacy/consent path checked, no trust-boundary, secret or report-derived content, clean); matomo-deprecation-rules (public JS API signature and return value unchanged, clean); matomo-i18n, api-development, plugin-architecture, twig, migrations, vue, frontend-direction, css, documentation: not triggered.
  • Regression risk: Low. Only the body of Tracker.requireCookieConsent changed: it now sets configCookiesDisabled = true instead of calling disableCookies(). The return value and the remembered-consent short-circuit are unchanged. disableCookies(), forgetCookieConsentGiven() and setDoNotTrack() still delete cookies right away. While cookies are disabled, getCookie and setCookie still return 0 or no-op, and getRequest() still calls deleteCookies() on every request, so no cookie is read or sent without consent. The one observable change is the timing of deletion for trackers with a known site ID (addTracker or direct getTracker(url, id)): deletion moves from call time to the next tracking request. This aligns them with the main _paq tracker and with requireConsent(). The minified piwik.min.js, piwik.js and matomo.js are byte-identical (blob 073d448e70) and differ only in that statement.
  • Medium findings:
    • None
  • Low / polish findings:
    • Behaviour change for direct API callers (js/piwik.js ~6517-6525): a tracker whose site ID is already known no longer deletes existing pk* cookies the moment requireCookieConsent() is called. They are now deleted on the next getRequest() (~4098-4100). If a page never sends a tracking request and consent is never given, earlier pk* cookies stay in document.cookie, though the tracker will not read or write them while cookies are disabled (getCookie returns 0). This now matches how the main _paq tracker already behaved (requireCookieConsent is applied before setSiteId) and matches requireConsent(). But unlike requireConsent, there is no unload hook that deletes cookies. This is acceptable and is documented in the inline comment and the commit message. Worth a short note in the PR description.
    • Test coverage gap (tests/javascript/index.php ~5334-5352): the new test covers the consent-given path. The privacy invariant (cookies required but consent never given, so cookies are deleted on the next tracking request) was only checked by hand by the author. An automated assertion would guard against a future regression here.
  • Regression test fails without the fix and passes with it; see "How it was tested" above for the exact commands and what could not be run here.

🤖 Generated with Claude Code

… by setCookieConsentGiven

requireCookieConsent called disableCookies, which deletes all tracking cookies right away when the tracker already knows its site ID (e.g. trackers added via addTracker). A following setCookieConsentGiven then generated a new visitor ID on every page view. Only disable cookies now; if consent is not given, cookies are still deleted on the next tracking request.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@chippison chippison added this to the 6.0.0 milestone Oct 7, 2026

@chippison chippison left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@AltamashShaikh
Everything looks good, but can we add a test to check that cookies are still deleted on the next request when consent is never given

@github-actions

Copy link
Copy Markdown
Contributor

If you don't want this PR to be closed automatically in 28 days then you need to assign the label 'Do not close'.

@github-actions github-actions Bot added the Stale The label used by the Close Stale Issues action label Oct 22, 2026
@sgiehl sgiehl modified the milestones: 6.0.0, 6.1.0 Oct 22, 2026
@github-actions github-actions Bot removed the Stale The label used by the Close Stale Issues action label Oct 23, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

If you don't want this PR to be closed automatically in 28 days then you need to assign the label 'Do not close'.

@github-actions github-actions Bot added the Stale The label used by the Close Stale Issues action label Nov 7, 2026
@AltamashShaikh
AltamashShaikh marked this pull request as ready for review November 25, 2026 05:07
# Conflicts:
#	js/piwik.min.js
#	matomo.js
#	piwik.js
#	tests/javascript/index.php
@AltamashShaikh AltamashShaikh added Do not close PRs with this label won't be marked as stale by the Close Stale Issues action and removed Stale The label used by the Close Stale Issues action labels Nov 25, 2026
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

@chippison Added a testcase, can you please re-check ?

@sgiehl sgiehl left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The fix for requireCookieConsent() looks good. The same problem still exists in requireConsentForRestrictedData(), though: it still calls this.disableCookies() (js/piwik.js:6669).

That method is new in 6.1.0. Its docblock and the changelog describe it as the replacement for requireCookieConsent() for sites that measure under a consent exemption. Tag Manager's Matomo tag also calls it right after creating the tracker with a site ID (MatomoTag.web.js). So take addTracker (or a Tag Manager tag), then requireConsentForRestrictedData(), then setConsentGiven(). The tracker already knows its site ID, so disableCookies() deletes the existing _pk_id right away. setConsentGiven() then turns cookies back on through setCookieConsentGiven() with a newly generated visitor ID. That's the same "new visitor on every page load" problem this PR fixes.

Since it isn't released yet, it would be good to fix it before 6.1.0 ships. Ideally that happens here, with the same change (only set configCookiesDisabled = true) and a matching test. I traced this in the code and haven't reproduced it in a browser.

…ata is followed by setConsentGiven (DEV-13943)
@AltamashShaikh

Copy link
Copy Markdown
Contributor Author

@sgiehl Fixed, please re-check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

auto-pr-bug-backlog Do not close PRs with this label won't be marked as stale by the Close Stale Issues action

Development

Successfully merging this pull request may close these issues.

Unique visitor ID not working with setCookieConsentGiven and addTracker

3 participants