Repository navigation
Keep existing visitor ID cookie when requireCookieConsent is followed by setCookieConsentGiven - #25460
Keep existing visitor ID cookie when requireCookieConsent is followed by setCookieConsentGiven#25460AltamashShaikh wants to merge 7 commits into
Conversation
… 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>
There was a problem hiding this comment.
@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
|
If you don't want this PR to be closed automatically in 28 days then you need to assign the label 'Do not close'. |
|
If you don't want this PR to be closed automatically in 28 days then you need to assign the label 'Do not close'. |
# Conflicts: # js/piwik.min.js # matomo.js # piwik.js # tests/javascript/index.php
…est when consent is never given (DEV-13943)
|
@chippison Added a testcase, can you please re-check ? |
…abled in the requireCookieConsent test (DEV-13943)
sgiehl
left a comment
There was a problem hiding this comment.
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)
|
@sgiehl Fixed, please re-check. |
Description
Problem: When the JS tracker is set up with
requireCookieConsent+setCookieConsentGivenand an additional tracker is registered withaddTracker, 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()calleddisableCookies(), which immediately deletes all tracking cookies when the tracker already knows its site ID. The_paqqueue appliesaddTrackerfirst, so the added tracker already has a site ID whenrequireCookieConsentruns 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. ThesetCookieConsentGivencall 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 whatrequireConsent()already does. If consent is not given, the existing cookies are still deleted on the next tracking request, becausegetRequest()callsdeleteCookies()whenever cookies are disabled.disableCookies()andforgetCookieConsentGiven()still delete cookies immediately. The minified files (js/piwik.min.js,piwik.js,matomo.js) were regenerated with the YUICompressor command fromjs/README.md. The only change in the minified output is this one statement.Edge cases considered:
addTracker: no_pk_*cookies remain aftertrackPageView.rememberCookieConsentGiven: unchanged (requireCookieConsentreturns early).disableCookies()/forgetCookieConsentGiven(): still delete cookies immediately (covered by an assertion in the new test).How it was tested:
Test API - requireCookieConsent keeps existing visitor ID cookie when consent is givenintests/javascript/index.php. It fails on6.x-dev(cookie deleted, new visitor ID generated: 2 failed assertions) and passes with the fix (8/8).tests/javascript/index.php, headless Chromium; theisInsideAnIframetest 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 ofjs/piwik.jspasses../vendor/bin/phpunit --filter testPiwikJs tests/PHPUnit/Integration/ReleaseCheckListTest.php: OK (minified files up to date,piwik.js==matomo.js).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.phpcsontests/javascript/index.php: clean (the directory is excluded byphpcs.xml). There are no PHP source changes, so phpstan was not applicable.Jira: DEV-13943
Fixes #19511
Checklist
Review
matomo-review, no blocking findings)requireCookieConsentdefers cookie deletion to the next tracking request instead of doing it immediately)What Claude verified
matomo-review(skill): verdict Yes, merge readiness Ready, blocking findings: none.🤖 Generated with Claude Code