Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't create cookies when disabled #18935

Merged
merged 33 commits into from Mar 24, 2022
Merged

Don't create cookies when disabled #18935

merged 33 commits into from Mar 24, 2022

Conversation

justinvelluppillai
Copy link
Contributor

@justinvelluppillai justinvelluppillai commented Mar 14, 2022

Description:

Fixes #17892

Supersedes #18135 (created a few new PRs to get around submodule issues).

This PR moves visitorId cookie creation a bit later, ie only when needed for trackPageView() or similar call. VisitorId is now stored internally in the tracker to offset some of the impact of this, but in some cases where multiple trackers are being used there may be some functional differences as implied by the changes I've made to the tests where they only pass when trackPageView() is called before other tracker methods.

Review

@justinvelluppillai
Copy link
Contributor Author

build js

@justinvelluppillai justinvelluppillai added Needs Review PRs that need a code review not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org. labels Mar 14, 2022
@justinvelluppillai justinvelluppillai added this to the 4.9.0 milestone Mar 14, 2022
@justinvelluppillai
Copy link
Contributor Author

All relevant tests now passing, so this is ready for review with particular consideration to implications for other plugins or unusual tracking setups.

@sgiehl
Copy link
Member

sgiehl commented Mar 16, 2022

I looked through the code changes and imho they are looking fine. The only "issue" I could see is the slightly changed behavior when using multiple trackers. But that actually should only affect using the visitorid in the trackers before a first tracking request was send. To circumvent this, I guess the only possible solution would be to somehow store the initital visitorid in a global scope, that can be accessed in each tracker instance. But not sure if that is needed.
@tsteur do you think we should move that to a global scope?

@tsteur
Copy link
Member

tsteur commented Mar 16, 2022

@sgiehl I don't quite understand the issue. AFAIK, we expect each tracker to have a different visitorId, not the same. But I might remember it wrongly. In what case exactly would it cause an issue? Like how can it be reproduced?

@justinvelluppillai
Copy link
Contributor Author

These tests suggest we want the same visitorId across trackers

test("Default visitorId should be equal across Trackers", function() {
. There's maybe no issue there but there is a change of behaviour which could be removed by setting a global visitorId on the page.

@tsteur
Copy link
Member

tsteur commented Mar 16, 2022

They are only the same if they point to the same URL and site.

If there are different trackers used (pointing to a different site, matomo url, or cookie prefix), then they should be different. Which we can also see in

var prefixTracker = Piwik.getTracker();
prefixTracker.trackPageView();
prefixTracker.setCookieNamePrefix('_test_cookie_prefix');
var prefixVisitorId = prefixTracker.getVisitorId();
notEqual(Piwik.getAsyncTracker().getVisitorId(), prefixVisitorId, 'Visitor ID are different when using a different cookie prefix');
var customTracker = Piwik.getTracker('customTrackerUrl', '71');
var customVisitorId = customTracker.getVisitorId();
notEqual(Piwik.getAsyncTracker().getVisitorId(), customVisitorId, 'Visitor ID are different on different websites');

Meaning it shouldn't be global if I see this right.

Because in

asyncTracker.trackPageView();
we are tracking a page view, I would have expected the cookie would be set and we shouldn't see a change in behaviour.

So the test is definitely expecting the right thing, the question is why isn't the delayedTracker reading the same visitorId. Maybe it's not returning the correct tracker instance? My random guess might be calling tracker.removeAllAsyncTrackersButFirst() in the beginning might fix the issue in case there's a tracker configured differently from a previous test.

@tsteur
Copy link
Member

tsteur commented Mar 20, 2022

@sgiehl I don't think that be a problem that often (using getVisitorId and manually creating the tracker)? Do you maybe have a specific implementation example where this be a problem? Do I see this right if getVisitorId is called too early it would return an empty visitor ID?

@justinvelluppillai
Copy link
Contributor Author

If we set the cookie when getVisitorId is called then we still have the original problem of potentially having cookies set when disabled. getVisitorId wouldn't return an empty visitor ID if called early, it would call loadVisitorIdCookie which will generate a UUID and return a new visitor ID if no cookie exists yet.

@tsteur
Copy link
Member

tsteur commented Mar 21, 2022

That be fine in that case @justinvelluppillai 👍 At least then people would have a way to fix this issue by calling getVisitorId after configuring cookie consent etc. And we improve the situation for 99.8% of the people

…n getVisitorId is called, if it is not existing
@justinvelluppillai
Copy link
Contributor Author

build js

@justinvelluppillai
Copy link
Contributor Author

build js

@justinvelluppillai
Copy link
Contributor Author

build js

@justinvelluppillai
Copy link
Contributor Author

build js

@justinvelluppillai
Copy link
Contributor Author

Nice one thanks @tsteur. I've implemented that approach here and it passes tests without making those changes 👍

A couple of unrelated tests fail but this is good for review.

@sgiehl
Copy link
Member

sgiehl commented Mar 21, 2022

@justinvelluppillai there is also getVisitorInfo, which currently might return varying userids until a cookie was set, guess we should set the cookie in that method as well.
Also I'm not sure if it is expected that the visitorId is currently empty until the cookie could be set. But I guess this actually already was the case before.

@justinvelluppillai
Copy link
Contributor Author

build js

@justinvelluppillai
Copy link
Contributor Author

@sgiehl I've added to now set visitor id cookie in getVisitorInfo also. And yes, this doesn't change behaviour from before with the visitorId before the cookie is set, except previously the visitorId cookie was set immediately if possible.

@justinvelluppillai
Copy link
Contributor Author

build js

js/piwik.js Show resolved Hide resolved
@justinvelluppillai
Copy link
Contributor Author

build js

@sgiehl sgiehl merged commit b2a17cb into 4.x-dev Mar 24, 2022
@sgiehl sgiehl deleted the j-17892-2 branch March 24, 2022 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review PRs that need a code review not-in-changelog For issues or pull requests that should not be included in our release changelog on matomo.org.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Don't create test cookies when cookies are disabled
3 participants