Skip to content

Anonymise a consenting visitor's IP address only as far as the site's own settings - #25759

Open
nathangavin wants to merge 31 commits into
hybrid-tracking-phase-2from
dev-20957
Open

nathangavin wants to merge 31 commits into
hybrid-tracking-phase-2from
dev-20957

Conversation

@nathangavin

@nathangavin nathangavin commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Description

On a site that measures consent-aware analytics, a visitor who has given their consent now has their IP address anonymised only as far as the admin configured for the site, instead of as far as a compliance policy enforces. Visitors without consent, sites that have not opted in, and sites with nothing enforced behave as before.

Changes

  • The admin's own IP anonymisation setting and mask length are cached for the tracker next to the enforced values.
  • Tracker.setVisitorIp gets a third parameter, $hasConsent, which defaults to false. When it is true, IPAnonymizer uses the admin's configured values.
  • Where a policy restriction is in force, a consented visit starts from the IP of the request, and the request that brings consent into a visit on its arrival page writes the visit's IP once, and its location is detected again from it. Elsewhere the IP and location are taken as before.
  • The visit's consent, and whether a request brings consent into the visit, come from Consent::visitConsentApplies() and Consent::isBroughtIntoVisitBy(), and the wording from ConsentAwareAnalytics::translateForScope(), all shared with the other consent-aware settings.
  • The two IP settings join the settings whose configured value is cached, which is also what makes ConsentAwareAnalytics::hasRelaxableRestriction() count them.
  • Compliance-page wording for the two IP settings changes where consent-aware analytics is on, for the site or for all websites.

Tests

  • New integration test IpAnonymisationConsentTest. It sends tracker requests and reads the stored IP for:
    • sites with and without the policy;
    • sites that are and are not opted in;
    • consent values;
    • the admin's own values;
    • split and continued visits;
    • bulk requests;
    • two sites;
    • a visit forced by a new campaign on a site with no policy, with and without consent;
    • consent turned on for all websites, forced cookieless tracking, consent sent by a ping or an event, a timed-out visit, a tracker cache without the admin's values, and a listener taking only the first two event parameters.
  • New integration test LocationConsentTest: the stored location follows the stored IP, for new visits and when consent arrives on the arrival page, and stays put otherwise.
  • Additions to PrivacyManagerConfigTest, ConsentAwareAnalyticsSettingTest (including the wording for a site and for all websites) and ApiTest.

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.
@nathangavin nathangavin added this to the 6.1.0 milestone Nov 18, 2026
…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.
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
…v-20957

# Conflicts:
#	plugins/CoreHome/Columns/Consent.php
#	plugins/PrivacyManager/Settings/ConsentAwareAnalytics.php
#	plugins/PrivacyManager/tests/Integration/ConsentAwareAnalyticsSettingTest.php
#	plugins/PrivacyManager/tests/Integration/PrivacyManagerConfigTest.php
@nathangavin
nathangavin requested a review from a team November 30, 2026 04:57
Comment thread plugins/CoreHome/Columns/VisitIp.php Resolved
Comment thread plugins/PrivacyManager/lang/en.json Resolved
Comment thread plugins/PrivacyManager/lang/en.json Resolved
Comment thread plugins/PrivacyManager/IPAnonymizer.php Resolved
…ery state the IP lift depends on

- When consent arrives on the page a visit arrived on, the location columns
  follow the IP written for the visit then, as they would have had consent
  come first. A location set through the tracking API still wins.
- The consent-aware IP wording says "for each site", which also fits the
  all-websites view.
- The processor points to VisitIp::onExistingVisit() for the rule, and that
  docblock now covers the new-visit case too.
- Tests for the all-websites opt-in, forced cookieless tracking on an
  existing visit, a full mask, consent by a ping or an event on the arrival
  page, IPv6 on the arrival page, a timed-out visit, a tracker cache without
  the admin's values, the instance's values, and a two-parameter listener.
Comment thread plugins/UserCountry/Columns/Base.php Outdated
return $value;
}

return $this->onNewVisit($request, $visitor, $action);

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 provider has no value for the new IP, the old value is kept, so one visit can mix two locations.

Reproduced (useAnonymizedIpForVisitEnrichment = 1, a provider that knows only the country for the full IP):

  1. CNIL policy enforced, site opted in, the admin's own IP anonymisation off.
  2. Page view on /landing without consent from 192.168.10.20: stored 192.168.0.0, de / BY / Masked city / 48.1 / 11.5.
  3. Page view on /landing with consent=1.
  4. Result: 192.168.10.20, fr / BY / Masked city / 48.1 / 11.5. The country is new, but the region, city, latitude and longitude still come from the masked IP.

With consent on the first request: fr / null / null / null / null.

The reason: onNewVisit() returns false when the provider has no value, and on an existing visit false means "don't update". Only the country always has a value. This happens often with city databases. Could we clear the column instead? I tried this, and the row then matches a visit that starts with consent. LocationConsentTest and IpAnonymisationConsentTest still pass:

Suggested change
return $this->onNewVisit($request, $visitor, $action);
$value = $this->onNewVisit($request, $visitor, $action);
return false === $value ? null : $value;

The mock provider in LocationConsentTest always returns every field, so a case where it returns only the country would cover this.

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