Skip to content
This repository has been archived by the owner on Nov 23, 2019. It is now read-only.

Add flag and logic to delete cross domain identifiers #54

Merged
merged 4 commits into from
Jan 14, 2019

Conversation

f2prateek
Copy link
Contributor

Some customers may want us to delete cross domain analytics related identifiers when it is disabled. This PR adds a new flag deleteCrossDomainId that is checked before running this new logic.

We delete seg_xid (and metadata) from cookies and crossDomainId from traits in localStorage. The deletion logic is run only if deletion is enabled for this project, and when either the seg_xid cookie or crossDomainId localStorage trait exists.

To delete the localStorage traits, we use an internal API in analytics.js. This minimizes the risk of introducing a bug that could arise due to interacting with lower level localStorage APIs directly.

Ref: https://segment.atlassian.net/browse/LIB-752

Some customers may want us to delete cross domain analytics related identifiers when it is disabled. This PR adds a new flag `deleteCrossDomainId` that is checked before running this new logic.

We delete  seg_xid (and metadata) from cookies and  crossDomainId from traits in localStorage. The deletion logic is run only if deletion is enabled for this project, and when either the seg_xid cookie or crossDomainId localStorage trait exists.

To delete the localStorage traits, we use an internal API in analytics.js. This minimizes the risk of introducing a bug that could arise due to interacting with lower level localStorage APIs directly.

Ref: https://segment.atlassian.net/browse/LIB-752
@f2prateek
Copy link
Contributor Author

This is not dependent on https://github.com/segmentio/ajs-renderer/pull/75 to deploy.

lib/index.js Outdated
@@ -179,6 +180,9 @@ Segment.prototype.initialize = function() {
this.cookie('segment_cross_domain_id_timestamp', null);
}

// Delete cross domain identifiers.
this.deleteCrossDomainId();

Choose a reason for hiding this comment

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

a bit nitpicking, but I find the function name confusing. Here it looks like we're going to always delete the cross domain id, which is not the case. Maybe checkCrossDomainIdForDeletion(), or something better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updating to deleteCrossDomainIdIfNeeded()

lib/index.js Outdated
}

// Only continue if we have any lingering identifiers.
if (this.cookie('seg_xid') == null && this.analytics.user().traits().crossDomainId == null) {
Copy link

@ladanazita ladanazita Jan 9, 2019

Choose a reason for hiding this comment

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

Is it necessary to account for a case where seg_xid is empty? I'm guessing there is a slim to none chance this occurs, but just checking.

What about if the other identifiers such as seg_xid_fd and seg_xid_ts are null/empty?

The only think I wonder about is in the case where this is reenabled, if having lingering other identifiers / empty values would impact the re-enablement of the XID Service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This checks if both the cookie and localStorage trait don't exist. If both don't exist, then we don't need to do anything can bail. Otherwise at least 1 exists, and we proceed to delete both.

One improvement we could make is make the checks independent (i.e. delete cookie if it exists and delete localStorage trait if it exists).

we are not checking here if the other metadata (e.g. seg_xid_fd or seg_xid_ts) are empty, so it doesn't really matter in this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One improvement we could make is make the checks independent (i.e. delete cookie if it exists and delete localStorage trait if it exists).

Updated now!

@f2prateek
Copy link
Contributor Author

@nhi-nguyen I pushed another update - could you take a 2nd look?

it('should delete cross domain identifiers if enabled', function() {
segment.options.deleteCrossDomainId = true;

segment.cookie('seg_xid', 'test_xid');

Choose a reason for hiding this comment

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

here we could add seg_xid_fd and seg_xid_ts and then later verify that they are deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added!

age: 26
});
});
});
});

Choose a reason for hiding this comment

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

there could be another test for the case when the cookie doesn't exist but crossDomainId trait in localStorage exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added 2 - one for that and one for reverse.

assert.equal(analytics.user().traits().crossDomainId, null);
});

it('should delete xid cookie if only cookie exists', function() {

Choose a reason for hiding this comment

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

Looks like the description is wrong here. This is the case where the localStorage trait exists?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops - fixed now.

assert.equal(analytics.user().traits().crossDomainId, null);
});

it('should delete localStorage trait if only traits exists', function() {

Choose a reason for hiding this comment

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

this test case doesn't test what it's supposed to test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorry - fixed now!

@f2prateek f2prateek merged commit 1766424 into master Jan 14, 2019
@f2prateek f2prateek deleted the deleteCrossDomainId branch January 14, 2019 17:47
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants