Skip to content

Fix issue with Nosara aliasing#7620

Closed
withinboredom wants to merge 5 commits intomasterfrom
fix/nosara
Closed

Fix issue with Nosara aliasing#7620
withinboredom wants to merge 5 commits intomasterfrom
fix/nosara

Conversation

@withinboredom
Copy link
Copy Markdown
Contributor

@withinboredom withinboredom commented Aug 10, 2017

This overwrites the browser's tk_ai (anonymous id) cookie with the one stored in user meta. This keeps the id synchronized across all browsers and devices.

Changes proposed in this Pull Request:

  • Sets the cookie tk_ai to the correct parameters to be read from the tracks client
  • Updates the user's cookie if it is incorrect
  • Calls jetpack_tracks_get_identity( get_current_user_id() ); asap in plugin initialization in the admin to set the cookie as appropriate, before the js lib can.

Testing instructions:

  • Login with a user that is not connected
  • Navigate to the Jetpack wp-admin with a fresh browser session and check your cookies.
  • You should see only one tk_ai cookie. If you see more, that's ok. Look for the one with the / path, that's your guy.
  • Delete your tk_ai cookies to simulate using a different/new device and navigate around wp-admin.
  • You should have only one tk_ai cookie with the previous value.

You can also get creative and set the cookie to a bogus value, it should reset back to the value as before.

Proposed changelog entry for your changes:

@withinboredom withinboredom added [Feature] Tracks [Pri] Normal [Status] Needs Review This PR is ready for review. Bug When a feature is broken and / or not performing as intended labels Aug 10, 2017
@withinboredom withinboredom self-assigned this Aug 10, 2017
@withinboredom withinboredom requested a review from a team as a code owner August 10, 2017 00:08
@withinboredom withinboredom added this to the 5.3 milestone Aug 10, 2017
Copy link
Copy Markdown
Contributor

@dereksmart dereksmart left a comment

Choose a reason for hiding this comment

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

I'm not sure if I'm misunderstanding the testing instructions, or what the problem this is trying to fix, but it doesn't seem to be testing well against the instructions in the PR:

  • Navigate to the Jetpack wp-admin with a fresh browser session and check your cookies.

  • You should see only one tk_ai cookie.
    I am seeing two here, but they are the same value:
    screen shot 2017-08-22 at 2 22 35 pm

  • Delete your cookies and navigate to the Jetpack wp-admin
    I'm getting the warning reported in my above comment

  • You should have only one tk_ai cookie with the previous value
    I am seeing two again, and they are different values than they were previously.
    screen shot 2017-08-22 at 2 24 34 pm

It seems like something is not being set correctly in the user meta table? Or it's getting deleted somehow?

// User isn't linked at all. Fall back to anonymous ID.
$anon_id = get_user_meta( $user_id, 'jetpack_tracks_anon_id', true );
if ( ! $anon_id ) {
if ( ! $anon_id || $anon_id !== $_COOKIE['tk_ai'] ) {
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.

If this cookie is not set at all, it throws an undefined index warning. Let's add an isset() check here as well.

add_action( 'jetpack_notices', array( $this, 'alert_auto_ssl_fail' ) );
}

jetpack_tracks_get_identity( get_current_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.

This feels like a better home, so we're at least making sure that it's only being set if Jetpack is connected.

@withinboredom
Copy link
Copy Markdown
Contributor Author

Huh, weird ... I'm seeing that behavior now too.

@withinboredom
Copy link
Copy Markdown
Contributor Author

withinboredom commented Aug 24, 2017

@dereksmart care to give it another swing? I updated the description and steps to test.

return;
}

jetpack_tracks_get_identity( get_current_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.

Since this only is meant to fix the anonymous ID for unlinked users, I think it's a good idea to make sure it only runs for unlinked users. Something like this?

if ( ! Jetpack::is_user_connected( $user_id = get_current_user_id() ) ) {
			jetpack_tracks_get_identity( $user_id );
		}

It would prevent many useless calls to this function for already-linked users. This is how many I got on one admin page load:

[24-Aug-2017 19:56:52 UTC] connected user with meta stored
[24-Aug-2017 19:56:54 UTC] connected user with meta stored
[24-Aug-2017 19:56:55 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:56 UTC] connected user with meta stored
[24-Aug-2017 19:56:57 UTC] connected user with meta stored
[24-Aug-2017 19:56:57 UTC] connected user with meta stored
[24-Aug-2017 19:56:57 UTC] connected user with meta stored

@withinboredom
Copy link
Copy Markdown
Contributor Author

@dereksmart updated to only check when it's not an ajax request and the user isn't connected.

@dereksmart
Copy link
Copy Markdown
Contributor

@withinboredom beautiful! :shipit:

@dereksmart dereksmart added [Status] Ready to Merge Go ahead, you can push that green button! and removed [Status] Needs Review This PR is ready for review. labels Aug 25, 2017

if ( ! headers_sent() ) {
setcookie( 'tk_ai', $anon_id );
setcookie( 'tk_ai', $anon_id, time() + 1000, '/' );
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.

Won't this mean that the cookie expires about 15 minutes after it's set? This seems like it would cause some disruption. Maybe we could check if the value is correct or not and use that to update the cookie, instead of just making the old one expire.

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.

How would it cause a disruption?

);
}

if ( 0 === $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.

How is this change related to the task in question? It does seem like it could be a bug, of course... maybe we should write a test to cover this?

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.

For the same reason we excluded ajax requests (user id 0 for logged out user). We don't want to cookie logged out user.

return;
}

if ( ! defined( 'DOING_AJAX' ) && ! Jetpack::is_user_connected( $user_id = get_current_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.

Can you explain why this change is necessary?

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.

@withinboredom
Copy link
Copy Markdown
Contributor Author

Closing because this actually fixes the issue but exposed a worse bug.

@jeherve jeherve deleted the fix/nosara branch September 8, 2017 19:37
@kraftbj kraftbj removed the [Status] Ready to Merge Go ahead, you can push that green button! label Oct 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug When a feature is broken and / or not performing as intended [Feature] Tracks [Pri] Normal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants