Skip to content

Keep the third-party cookie off for consenting visitors where a compliance policy requires it - #25782

Open
nathangavin wants to merge 25 commits into
hybrid-tracking-phase-2from
dev-20959
Open

nathangavin wants to merge 25 commits into
hybrid-tracking-phase-2from
dev-20959

Conversation

@nathangavin

@nathangavin nathangavin commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Consent-aware analytics does not lift a compliance policy's "Third-Party Cookies Disabled" requirement. The cookie could not be read back to recognise a consenting visitor without their visitor ID changing with consent, so writing it would only link them on the instance's other websites.

Changes

  • A comment in ConsentAwareAnalytics::hasRelaxableRestriction() saying why the setting is not counted.

Tests

  • New integration test RequestThirdPartyCookieConsentTest: on a policy site, consent neither writes nor reads _pk_uid, with consent-aware analytics on for the site or the instance. Controls on a site without a policy show the cookie is still written and read there.

Checklist

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

Review

🤖 Generated with Claude Code

…r them

The tracker cannot see the per-site server setting that was meant to gate this
feature, so every change made here reached every site loading matomo.js. That
is what the revert was for: disableCampaignParameters() stopped stripping once
any site called setConsentGiven(), the campaign came back mid-visit, and
create_new_visit_when_campaign_changes split the visit for sites that had opted
in to nothing.

requireConsentForRestrictedData() is the gate instead, since the tracker can
see that:

- hasGivenConsent() counts a reported consent only where that call was made, so
  purify(), the campaign detection, the consent=1 on a request and the one on
  each queued request all go back to what they did before for everyone else
- the measurement consent cookie is written and read only by such a site, and
  never under Do Not Track. It is scoped by host and path, so one site's banner
  was changing what its neighbours collected
- it no longer overrides disableCookies(). The opt-out cookie is exempt because
  honouring a refusal may not depend on a cookie setting; remembering a consent
  allows more to be collected and is not the same thing
- opting out claims the consent gate only on such a site, and the opt-out form
  makes the call it always made everywhere else

Also drops a requireCookieConsent docblock left stranded above the new method.

The test that was missing: a site that calls none of this sends the same
requests as before. It fails on both counts without the gate.
The guard added for this checked that the visit held some campaign in the
clear, while the comment above it and the pull request both said the
conversion may only report one the visit itself holds. Three things it never
checked:

- consent, so a site that never opted in skipped the masking too, and so did
  one switched back off part way through a visit
- whether it was the same campaign, so a visit holding one campaign let a
  conversion report another
- that the visit's clear campaign was not simply older than the policy, a
  visit open across the policy being enforced having kept it

The campaign on a conversion comes from the attribution cookie, which outlives
the visit and is not subject to the policy while it sits in the browser, so
none of that may be assumed. It is now reported only where the visitor
consented and where it is the value the visit holds, judged per value, since a
visit can hold its campaign name and still have had the keyword withheld.

Two of the three tests are baselines for sites this feature should not reach
at all: never opted in, and opted in then switched off. All three fail against
the previous rule, writing a campaign an enforced policy requires withheld.
While enable_userid_overwrites_visitorid is on, a User ID replaces a visit's
idvisitor with an unsalted hash of itself, which identifies the visitor as
surely as the column a policy withholds would. The guard for that was written
on the existing-visit path only, and a new visit reaches its identifier by
another route: a forced new visit, or one started by the midnight split, is
created for a visitor who is still recognised, and the recognised visit's
idvisitor was carried straight over. The new visit therefore stored a hash of
a User ID while holding no user_id and no consent of its own.

A visit created now has its own consent or none, never the consent of the one
before it, so where the policy withholds the User ID for this request that
hash is not carried over. The request's own identifier stands in, and a fresh
one where it has none. Every other identifier is left alone: declining to
recognise the visitor at all would be a far larger change than this guards
against, which the second test holds in place.

Worth noting for the next reader: the recognizer copies the stored visit's
idvisitor over the one the request arrived with before this runs, so skipping
only the "visitor is known" branch is not enough - the branch below it reads
the same property and returns the same hash.
Two rules were in use. The campaign followed the consent the visit holds, so
a later request that does not repeat the flag is still part of a visit that
consented. The User ID and the referrer anonymisation level followed the flag
on the request in front of them, so the same request had its User ID withheld
and its referrer anonymised inside that same visit. A comment said as much and
left it open.

They now share one answer, Consent::visitHasConsent(), which is the rule the
campaign already used. What it changes:

- the User ID is not withheld from a later request in a visit that consented
- the referrer is anonymised to the level that visit earned, not re-anonymised
  because one request omitted the flag
- the visit's idvisitor stops switching between the User ID hash and the cookie
  id as requests arrive, which rewrote the visit and its action rows each time

The exception stays: a consent that cannot start a visit of its own, a ping
landing on a visit measured before any was given, belongs to the visit it will
start rather than the one it lands on.

Request::getForcedUserId() keeps deciding on the request by default, because
VisitorRecognizer calls it while deciding which visit a request belongs to,
before any visit is known. The callers that hold the visit pass its answer in.

The new test pins the identifier across a request that omits the flag; it fails
on the old rule, the id changing from the hash to the cookie id mid-visit.
Six docblocks had come adrift: stacked pairs in Visit.php, Consent.php and
the PrivacyManager request processor, a detached one in the Referrers base,
and two orphaned above the wrong tests. Consent::hasVisitConsented(), which
is public, had lost its own to the method added above it.

All of them come from the same habit: anchoring an insertion on the function
signature puts the new method and its docblock between the previous docblock
and the method it belongs to. Anchoring on the docblock instead avoids it.
Two people could become one visit. Consent decides whether the User ID is
recorded, and the same answer has to decide whether a request names the same
person, but that check read the request's own flag. With the policy
withholding the User ID a flagless request resolved to no user at all, so it
looked like a request naming nobody, joined the consented visit, and the
dimension then wrote its User ID over the first person's and rewrote the
visit's identifier to match.

The check now reads the consent the visit holds, so a request naming someone
else resolves to that person, does not match, and starts a visit of its own.

It takes the consent from the row the recognizer found rather than from the
visit properties: only idvisitor and user_id have been copied into those by
that point, the rest arriving later with updateVisitPropertiesFromLastVisitRow().
Reading it from the properties looks right, passes lint, and silently never
fires.

Also pins two behaviours that were left unpinned: the referrer anonymisation
level following the visit's consent, and the User ID hash guard standing down
where the consent does apply. Both survived mutation before these tests.
The consent state segment landed on 6.x-dev, and both branches add to
Consent.php. Both sets are kept: the segment metadata and its reporting
methods from 6.x-dev, the tracking methods from this branch.
lastUserIdWasSetAndDoesMatch() is protected and this processor can be
replaced through dependency injection, so a plugin may already override it.
Giving the method a third parameter makes such an override incompatible, and
PHP refuses to load one: the tracker would stop rather than degrade. The
parameter's default does not help, because it protects callers rather than
overriders.

The recognised visit's row is already published as request metadata. It is
now set before isVisitNew() runs so the method can read it back there, and
the signature returns to the one it has always had.

Pinned by a subclass that declares the old signature: widening the method
again stops that file loading at all.
The guard stood down only on the consent this request reports, which a site
that never opted in never reports either, so it also ran there: a visitor
recognised from a visit stored before the policy was enforced stopped reusing
the identifier that visit held.

It now reads the consent stored against the recognised visit. Only a visit
that consented can carry a hash of a User ID the policy withholds, so a site
that opted in to nothing is left alone, while a site that opted out after a
consented visit is still guarded - which reading the setting would not do.

Also, from review: name the consent-aware policy wording keys for what they
are rather than suffixing them, import PrivacyManager's Config instead of
naming it absolutely, say what disableCampaignParameters() now does, and
repair a comment that had lost half a sentence.
…g page

setConsentGiven() sent nothing by itself: the request carrying consent=1 came
from setCookieConsentGiven(), whose body only runs where cookies were still
disabled. Where they were already on - remembered from an earlier page view,
or enabled by a consent screen answering cookies first - the consent reached
the server only with the next page view, by which time a new visit had begun
and the landing visit kept the treatment it was measured under.

It now sends that request itself where the cookie consent had none to send.
Only a site that called requireConsentForRestrictedData() does so: every
other site makes exactly the requests it made before, which is what the first
attempt at this feature got wrong.

setCookieConsentGiven() reports whether it sent one, so the two cannot both
fire. Covered by both reported cases plus a site that asked for nothing.
While enable_userid_overwrites_visitorid is on, a User ID replaces a visit's
idvisitor with a hash of itself. Consent decided whether the User ID was
available, so it decided the identifier too, and the same person in the same
browser was one visitor on a request reporting consent and another on a
request that did not. Visits were split, merged or missed accordingly: a
flagless request could join a consented visit and relabel it, a session could
be torn into two visits, a new visit could inherit the hash, and a consenting
ping could resolve to a visit from a previous day, be found outside the visit
window, and be dropped along with the consent it carried.

A User ID a policy withholds is now left out of the identifier whether or not
the request reports consent, so the identifier no longer moves. The guard
that kept the inherited hash out of the next visit has no cause left to guard
against and goes, with the checks it needed.

On such a site nb_uniq_visitors keeps counting browsers rather than accounts,
as it did before this feature; nb_users still counts distinct User IDs.
… pinging twice

From review:

- A visitor arriving from another site on a campaign URL the tracker had
  stripped was attributed to that site. Consent then brought the campaign
  back, which counted as a different campaign and forced a new visit - and a
  request that cannot start one is dropped, taking the consent with it. The
  campaign a tracker withheld until consent now completes that arrival
  instead, from one rule the split decision and the four referrer columns
  share.
- A visit can begin with an event, and then has no entry page until the
  first page view arrives. That page view is the arrival, so consent it
  carries belongs to this visit rather than a second one.
- setConsentGiven() sent a ping even where the requests from the page
  already carried the consent, so a consent screen calling it after each
  page view added one request per page view. It now sends nothing where
  the server has already been told.
- Withdrawing consent in another window stopped this one reporting it but
  left its cookies on, so a visitor id was written straight back. It now
  follows forgetConsentGiven() and clears them.
- isReferrerInformationNew(), hasReferrerColumnChanged() and getForcedUserId()
  keep the signatures they have always had, with the extra arguments moved to
  new methods beside them. A plugin overriding any of the three no longer
  fails to load. These were the only three signatures the branch changed.
…is gone

The cookies are already deleted by the window that withdrew the consent, so
this one only has to stop writing a new visitor id back - which avoids calling
a function declared further down the file than the call.
…ay what changed

From review:

- The developer changelog claimed getForcedUserId() gained an optional
  parameter, which it no longer does. Replaced with the shorter set of
  entries the reviewer proposed, organised by what a plugin developer needs:
  the setting, the consent parameter, the column, the tracker API, the new
  methods, the tracker cache entries and the identifier rule. Every name and
  cache key in it is checked against the code.
- Four of the helpers added here are only reachable from within this class,
  so they are private rather than protected. The five the referrer columns
  call stay protected and say @internal, so extending this class does not
  pin them.
- A docblock had been separated from the method it describes.
@nathangavin nathangavin added this to the 6.1.0 milestone Nov 20, 2026
Base automatically changed from dev-20566-reland to 6.x-dev November 24, 2026 01:29
@nathangavin
nathangavin changed the base branch from 6.x-dev to hybrid-tracking-phase-2 November 26, 2026 04:50
Comment thread core/Tracker/Request.php Outdated
if (!$privacyConfig->forceCookielessTracking) {
// - If set to use 3rd party cookies for Visit ID, read the cookie
if (!$found) {
// Not lifted by consent, unlike where the cookie is written: as with the User ID

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.

With this, the cookie is written for a consenting visitor but never read on the policy site. So its only effect is on other sites of the same Matomo.

Reproduced:

  1. Site A: CNIL policy, opted in. Site B: no policy. use_third_party_id_cookie = 1.
  2. Request on A with consent=1 and _id=1111…: _pk_uid is written with 1111….
  3. Request on B with that cookie and _id=2222….
  4. Result: on B, idvisitor is 1111…. On A, idvisitor is still 1111…, from _id, not from the cookie.

Without consent on A: nothing is written, and B uses 2222….

The ticket asks to set the cookie, but also says a consenting visitor should be measured as if the policy were not enforced, which would mean reading it too. I understand why it is not read (the visitor ID should not change from one request to the next). But then, on the policy site, the cookie does nothing for the visitor who consented, and it links them on the other sites.

Possible options:

  • don't write it at all while the policy is enforced;
  • also read it, with a rule that stays stable for the visit (like Consent::visitHasConsent() for the User ID);

Whatever we choose, a two-site test would pin it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, writing it without reading it isn't worth it, and reading it doesn't work either. The visitor ID is decided before the visit is looked up, so reading the cookie could only follow the request's own consent flag. The visitor ID would then change when consent arrives: the pageview before consent stays in a visit under _id, and the consented requests move to the shared cookie's ID. Keeping the visitor ID the same across consent is the rule the User ID lift follows too.

So consent no longer lifts this setting: on a site whose policy keeps third-party cookies off, _pk_uid stays off for every visitor, as before. The PR now only adds a test pinning that (no write and no recognition under consent, with consent-aware analytics on for the site or the whole instance, plus controls on a site without a policy) and a comment in hasRelaxableRestriction() saying why it is left out.

Comment thread core/Tracker/Request.php Outdated
public function setThirdPartyCookie($idVisitor)
{
if (!$this->shouldUseThirdPartyCookie()) {
if (!$this->shouldUseThirdPartyCookieForConsent($this->hasConsent())) {

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.

Not blocking: when the visitor withdraws consent, nothing removes the cookie written here.

  1. CNIL policy, site opted in, use_third_party_id_cookie = 1.
  2. Request with consent=1: _pk_uid is written (13 months).
  3. The visitor withdraws consent (forgetConsentGiven()), and the next requests come without consent=1.
  4. Result: the write is only skipped. _pk_uid stays, and other sites without a policy keep reading it.

Only the opt-out and the Do Not Track check remove it (IgnoreCookie::deleteThirdPartyCookieUIDIfExists()). The cookie is set by the Matomo server, so the JS tracker can't remove it. Should a request without consent on a policy site that measures consent-aware analytics remove an existing _pk_uid?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not applicable any more: consent no longer writes the cookie, see the reply on Request.php:910.

Comment thread lang/en.json Outdated
"ThirdPartyCookieSettingWhatItDoes": "Third-party cookies are disabled. Matomo uses first-party cookies so that cookie-based visitor identifiers remain limited to the website that created them.<br><br>This prevents cookie-based identification of visitors across unrelated websites.",
"ThirdPartyCookieSettingImpact": "Matomo cannot use third-party cookies for cross-site visitor recognition. Visitor identifiers remain limited to the current website or first-party cookie scope.",
"ThirdPartyCookieSettingWhatItDoesConsentAware": "Third-party cookies are disabled for visitors who have not given their consent. Matomo uses first-party cookies so that their cookie-based visitor identifiers remain limited to the website that created them.<br><br>This prevents cookie-based identification of those visitors across unrelated websites. Where a visitor has given their consent, the third-party cookie is set if it is enabled in the Matomo configuration.",
"ThirdPartyCookieSettingImpactConsentAware": "For visitors who have not given their consent, Matomo cannot use third-party cookies for cross-site visitor recognition, and their visitor identifiers remain limited to the current website or first-party cookie scope.",

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.

Nitpick: this reads as if visitors who gave their consent are recognised across sites. On this website they are not, because the cookie is not read here.

Suggested change
"ThirdPartyCookieSettingImpactConsentAware": "For visitors who have not given their consent, Matomo cannot use third-party cookies for cross-site visitor recognition, and their visitor identifiers remain limited to the current website or first-party cookie scope.",
"ThirdPartyCookieSettingImpactConsentAware": "Matomo does not use third-party cookies for cross-site visitor recognition on this website, and visitor identifiers remain limited to the current website or first-party cookie scope. Where a visitor has given their consent, the third-party cookie may still be set, but it is not used to recognise them on this website.",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not applicable any more: consent no longer writes the cookie, see the reply on Request.php:910.

Comment thread core/Tracker/Request.php Outdated
*
* @return mixed
*/
public function shouldUseThirdPartyCookieForConsent(bool $hasConsent)

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.

Not blocking: a few points on this new public method:

  • Both callers pass $this->hasConsent(). Could it read it itself, with no parameter? Today a plugin could pass the visit's consent, or use it to decide recognition, which the comment in getVisitorId() says must not happen. A name like shouldSetThirdPartyCookie() would also say it is only for writing.
  • Could it return bool instead of @return mixed?
  • With consent, it reads ThirdPartyCookies directly, so a subclass that overrides shouldUseThirdPartyCookie() (the case the docblock mentions) is ignored for consenting requests. Maybe worth one line in the docblock.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not applicable any more: consent no longer writes the cookie, see the reply on Request.php:910.

Comment thread core/Tracker/Request.php Outdated
// the bypass sets aside every policy requirement on this setting, which is what consent
// lifts: should a policy ever require it off whatever the visitor consents to, it has to
// be kept here
return PolicyEnforcementBypass::run(function () use ($idSite) {

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.

Not blocking: the other settings that consent lifts read the admin's own value through the setting itself (PrivacyManager\Config::getConfiguredValue(), UserIdDisabled::shouldDiscardUserId()). Here, Request uses PolicyEnforcementBypass, which is @internal and was only used on the admin side so far.

Could ThirdPartyCookies have a getConfiguredValue(?int $idSite) (site config, then global, as getInstance() does)? Then the next setting has only one pattern to copy.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not applicable any more: consent no longer writes the cookie, see the reply on Request.php:910.

@tzi tzi 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.

Not blocking: some cases have no test yet:

  • a consented request without _id: setThirdPartyCookie() gets false and saves an empty cookie (this already happens without a policy, but it is now reachable under it);
  • a consented request with uid and enable_userid_overwrites_visitorid: the cookie should hold _id, not the User ID hash;
  • QueuedTracking, which writes the cookie at queue time with getVisitorIdForThirdPartyCookie().

@nathangavin nathangavin changed the title Set the third-party cookie for a consenting visitor where the admin configured it Keep the third-party cookie off for consenting visitors where a compliance policy requires it Dec 1, 2026
@nathangavin

Copy link
Copy Markdown
Contributor Author

Not applicable any more: consent no longer writes the cookie, see the reply on Request.php:910.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants