Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,27 @@ protected void registerDeviceToken(@Nullable String email, @Nullable String user
IterableLogger.e(TAG, "registerDeviceToken: applicationName is null, check that pushIntegrationName is set in IterableConfig");
}

apiClient.registerDeviceToken(email, userId, authToken, applicationName, deviceToken, dataFields, deviceAttributes, _setUserSuccessCallbackHandler, _setUserFailureCallbackHandler);
// Create a wrapper success handler that tracks consent before calling the original success handler
IterableHelper.SuccessHandler wrappedSuccessHandler = getSuccessHandler();

apiClient.registerDeviceToken(email, userId, authToken, applicationName, deviceToken, dataFields, deviceAttributes, wrappedSuccessHandler, _setUserFailureCallbackHandler);
}

private IterableHelper.SuccessHandler getSuccessHandler() {
IterableHelper.SuccessHandler wrappedSuccessHandler = null;
if (_setUserSuccessCallbackHandler != null || (config.enableUnknownUserActivation && getVisitorUsageTracked() && config.identityResolution.getReplayOnVisitorToKnown())) {
final IterableHelper.SuccessHandler originalSuccessHandler = _setUserSuccessCallbackHandler;
wrappedSuccessHandler = data -> {
// Track consent now that user has been created/updated via device registration
trackConsentOnDeviceRegistration();

// Call the original success handler if it exists
if (originalSuccessHandler != null) {
originalSuccessHandler.onSuccess(data);
}
};
}
return wrappedSuccessHandler;
}
Comment on lines +628 to 643
Copy link
Member

Choose a reason for hiding this comment

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

Good optimization

//endregion

Expand Down Expand Up @@ -825,6 +845,7 @@ public void setEmail(@Nullable String email, @Nullable String authToken, @Nullab

if (email == null) {
unknownUserManager.setCriteriaMatched(false);
setConsentLogged(false);
}

_setUserSuccessCallbackHandler = successHandler;
Expand Down Expand Up @@ -893,6 +914,7 @@ public void setUserId(@Nullable String userId, @Nullable String authToken, @Null

if (userId == null) {
unknownUserManager.setCriteriaMatched(false);
setConsentLogged(false);
}

_setUserSuccessCallbackHandler = successHandler;
Expand All @@ -919,10 +941,6 @@ private void attemptMergeAndEventReplay(@Nullable String emailOrUserId, boolean

if (replay && (_userId != null || _email != null)) {
unknownUserManager.syncEventsAndUserUpdate();

if (_userIdUnknown == null) {
trackConsentForUser(isEmail ? emailOrUserId : null, isEmail ? null : emailOrUserId, true);
}
}

if (!isUnknown) {
Expand Down Expand Up @@ -1484,6 +1502,41 @@ public boolean getVisitorUsageTracked() {
return sharedPreferences.getBoolean(IterableConstants.SHARED_PREFS_VISITOR_USAGE_TRACKED, false);
}

private boolean getConsentLogged() {
if (_applicationContext == null) {
return false;
}
SharedPreferences sharedPreferences = _applicationContext.getSharedPreferences(IterableConstants.SHARED_PREFS_FILE, Context.MODE_PRIVATE);
Copy link
Member

Choose a reason for hiding this comment

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

Good that we are persisting it now

return sharedPreferences.getBoolean(IterableConstants.SHARED_PREFS_CONSENT_LOGGED, false);
}

private void setConsentLogged(boolean consentLogged) {
if (_applicationContext == null) {
return;
}
SharedPreferences sharedPref = _applicationContext.getSharedPreferences(IterableConstants.SHARED_PREFS_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
if (consentLogged) {
editor.putBoolean(IterableConstants.SHARED_PREFS_CONSENT_LOGGED, true);
} else {
editor.remove(IterableConstants.SHARED_PREFS_CONSENT_LOGGED);
}
editor.apply();
}

/**
* Tracks consent during device registration if conditions are met.
*/
private void trackConsentOnDeviceRegistration() {
if (config.enableUnknownUserActivation && getVisitorUsageTracked() && config.identityResolution.getReplayOnVisitorToKnown()) {
if (!getConsentLogged()) {
boolean isUserKnown = (_userIdUnknown == null);
trackConsentForUser(_email, _userId, isUserKnown);
setConsentLogged(true);
Copy link
Member

Choose a reason for hiding this comment

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

I saw that a user can be created even with device registration failing due to no device ID generation.
Do we not want consent to be tracked in that 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.

Adding consent to failure callback in a separate PR.

}
}
}

/**
* Tracks user consent with the timestamp from when visitor usage was first tracked.
* This should be called when transitioning from unknown to known user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public final class IterableConstants {
public static final String SHARED_PREFS_VISITOR_USAGE_TRACKED = "itbl_visitor_usage_track";
public static final String SHARED_PREFS_VISITOR_USAGE_TRACKED_TIME = "itbl_visitor_usage_track_time";
public static final String SHARED_PREFS_DEVICE_NOTIFICATIONS_ENABLED = "itbl_notifications_enabled";
public static final String SHARED_PREFS_CONSENT_LOGGED = "itbl_consent_logged";

//Action buttons
public static final String ITBL_BUTTON_IDENTIFIER = "identifier";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ private void createUnknownUser(String criteriaId) {
IterableApi.getInstance().config.iterableUnknownUserHandler.onUnknownUserCreated(userId);
}
IterableApi.getInstance().setUnknownUser(userId);
IterableApi.getInstance().trackConsentForUser(null, userId, false);
}, (reason, data) -> handleTrackFailure(data));
}

Expand Down
Loading
Loading