Skip to content

Commit

Permalink
Start new session when user anonymisation is toggled on tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
matus-tomlein committed Nov 14, 2022
1 parent b5529d9 commit e9734c9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
Expand Up @@ -26,6 +26,7 @@
import com.snowplowanalytics.snowplow.TestUtils;
import com.snowplowanalytics.snowplow.emitter.EventStore;
import com.snowplowanalytics.snowplow.event.SelfDescribing;
import com.snowplowanalytics.snowplow.event.Structured;
import com.snowplowanalytics.snowplow.payload.SelfDescribingJson;
import com.snowplowanalytics.snowplow.tracker.DevicePlatform;
import com.snowplowanalytics.snowplow.internal.emitter.Emitter;
Expand Down Expand Up @@ -512,6 +513,39 @@ public void testExceptionHandler() {
handler1.uncaughtException(Thread.currentThread(), new Throwable("Illegal State Exception has been thrown!"));
}

@Test
public void testChangeUserAnonymisation() {
Emitter emitter = new Emitter(getContext(), "fake-uri", new Emitter.EmitterBuilder()
.option(BufferOption.Single)
);
emitter.pauseEmit();

tracker = new Tracker(new Tracker.TrackerBuilder(emitter, "ns", "myAppId", getContext())
.base64(false)
.level(LogLevel.VERBOSE)
.sessionContext(true)
.installTracking(false)
.applicationCrash(false)
.screenviewEvents(false)
.foregroundTimeout(5)
.backgroundTimeout(5)
.timeUnit(TimeUnit.SECONDS)
);

tracker.track(new Structured("c", "a"));
String sessionIdStart = tracker.getSession().getState().getSessionId();

tracker.setUserAnonymisation(true);
tracker.track(new Structured("c", "a"));
String sessionIdAnonymous = tracker.getSession().getState().getSessionId();
assertNotEquals(sessionIdStart, sessionIdAnonymous);

tracker.setUserAnonymisation(false);
tracker.track(new Structured("c", "a"));
String sessionIdNotAnonymous = tracker.getSession().getState().getSessionId();
assertNotEquals(sessionIdAnonymous, sessionIdNotAnonymous);
}

public static class TestExceptionHandler implements Thread.UncaughtExceptionHandler {

private final String expectedMessage;
Expand Down
Expand Up @@ -345,6 +345,7 @@ public TrackerBuilder screenviewEvents(@NonNull Boolean screenviewEvents) {
*/
@NonNull
public TrackerBuilder userAnonymisation(@NonNull Boolean userAnonymisation) {
boolean changedUserAnonymisation = this.userAnonymisation != userAnonymisation;
this.userAnonymisation = userAnonymisation;
return this;
}
Expand Down Expand Up @@ -387,7 +388,7 @@ public TrackerBuilder trackerVersionSuffix(@Nullable String trackerVersionSuffix
boolean installTracking;
boolean activityTracking;
boolean applicationContext;
boolean userAnonymisation;
private boolean userAnonymisation;
String trackerVersionSuffix;

private boolean deepLinkContext;
Expand Down Expand Up @@ -985,6 +986,16 @@ public void setDeepLinkContext(boolean deepLinkContext) {
}
}

/** Internal use only */
public void setUserAnonymisation(boolean userAnonymisation) {
if (this.userAnonymisation != userAnonymisation) {
this.userAnonymisation = userAnonymisation;
if (trackerSession != null) {
trackerSession.startNewSession();
}
}
}

// --- Getters

/** Internal use only */
Expand All @@ -997,10 +1008,16 @@ public boolean getDeepLinkContext() {
return deepLinkContext;
}

/** Internal use only */
public boolean getSessionContext() {
return sessionContext;
}

/** Internal use only */
public boolean isUserAnonymisation() {
return userAnonymisation;
}

/**
* @return the tracker version that was set
*/
Expand Down
Expand Up @@ -188,6 +188,7 @@ public interface TrackerConfigurationInterface {

/**
* Whether to anonymise client-side user identifiers in session (userId, previousSessionId), subject (userId, networkUserId, domainUserId, ipAddress) and platform context entities (IDFA)
* Setting this property on a running tracker instance starts a new session (if sessions are tracked).
*/
void setUserAnonymisation(boolean userAnonymisation);

Expand Down
Expand Up @@ -312,14 +312,14 @@ public void setDiagnosticAutotracking(boolean diagnosticAutotracking) {

@Override
public boolean isUserAnonymisation() {
return getTracker().userAnonymisation;
return getTracker().isUserAnonymisation();
}

@Override
public void setUserAnonymisation(boolean userAnonymisation) {
getDirtyConfig().userAnonymisation = userAnonymisation;
getDirtyConfig().userAnonymisationUpdated = true;
getTracker().userAnonymisation = userAnonymisation;
getTracker().setUserAnonymisation(userAnonymisation);
}

@Nullable
Expand Down

0 comments on commit e9734c9

Please sign in to comment.