Skip to content

[MOB-9558] refactor AnonymousUserManager after update user changes #791

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

Draft
wants to merge 4 commits into
base: feature/itbl-track-anon-user
Choose a base branch
from
Draft
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 @@ -200,54 +200,60 @@ private void createKnownUser(String criteriaId) {
//generate anon user id
String userId = UUID.randomUUID().toString();

if (userData.isEmpty()) return;

try {
if (!userData.isEmpty()) {
JSONArray trackEventList = getEventListFromLocalStorage();
JSONObject updateUserTrack = null;
int updateUserTrackPosition = 0;

//find last update user event data fields and position
for (int i = 0; i < trackEventList.length(); i++) {
JSONObject trackEvent = trackEventList.getJSONObject(i);
if ((trackEvent.has(IterableConstants.SHARED_PREFS_EVENT_TYPE)
&& trackEvent.getString(IterableConstants.SHARED_PREFS_EVENT_TYPE).equals(IterableConstants.KEY_USER))
&& trackEvent.has(IterableConstants.KEY_DATA_FIELDS)) {
updateUserTrackPosition = i;
updateUserTrack = trackEvent.getJSONObject(IterableConstants.KEY_DATA_FIELDS);
break;
}
}
//find last update user event data fields and position
JSONArray trackEventList = getEventListFromLocalStorage();
int updateUserTrackPosition = getLastUpdateUserEventPosition(trackEventList);
JSONObject updateUserTrack = null;

//remove update user event from local event list
if (updateUserTrack != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
trackEventList.remove(updateUserTrackPosition);
}
if (updateUserTrackPosition != -1) {
updateUserTrack = trackEventList.getJSONObject(updateUserTrackPosition).getJSONObject(IterableConstants.KEY_DATA_FIELDS);
}

//remove update user event from local event list
if (updateUserTrack != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
trackEventList.remove(updateUserTrackPosition);
}
saveEventListToLocalStorage(trackEventList);
}
saveEventListToLocalStorage(trackEventList);

JSONObject userSessionDataJson = new JSONObject(userData);
JSONObject userDataJson = userSessionDataJson.getJSONObject(IterableConstants.SHARED_PREFS_ANON_SESSIONS);
JSONObject userSessionDataJson = new JSONObject(userData);
JSONObject userDataJson = userSessionDataJson.getJSONObject(IterableConstants.SHARED_PREFS_ANON_SESSIONS);

//update user data
if (!getPushStatus().isEmpty()) {
userDataJson.put(IterableConstants.SHARED_PREFS_PUSH_OPT_IN, getPushStatus());
}
userDataJson.put(IterableConstants.SHARED_PREFS_CRITERIA_ID, Integer.valueOf(criteriaId));

//track anon session with new user
iterableApi.apiClient.trackAnonSession(getCurrentTime(), userId, userDataJson, updateUserTrack, data -> {
// success handler
IterableApi.getInstance().setAnonUser(userId);
syncEvents();
}, (reason, data) -> handleTrackFailure(data));
//update user data
if (!getPushStatus().isEmpty()) {
userDataJson.put(IterableConstants.SHARED_PREFS_PUSH_OPT_IN, getPushStatus());
}
userDataJson.put(IterableConstants.SHARED_PREFS_CRITERIA_ID, Integer.valueOf(criteriaId));

//track anon session with new user
iterableApi.apiClient.trackAnonSession(getCurrentTime(), userId, userDataJson, updateUserTrack, data -> {
// success handler
IterableApi.getInstance().setAnonUser(userId);
syncEvents();
}, (reason, data) -> handleTrackFailure(data));

} catch (JSONException e) {
e.printStackTrace();
}
}

private int getLastUpdateUserEventPosition(JSONArray trackEventList) throws JSONException {
int updateUserTrackPosition = -1;
for (int i = 0; i < trackEventList.length(); i++) {
JSONObject trackEvent = trackEventList.getJSONObject(i);
if ((trackEvent.has(IterableConstants.SHARED_PREFS_EVENT_TYPE) &&
trackEvent.getString(IterableConstants.SHARED_PREFS_EVENT_TYPE).equals(IterableConstants.KEY_USER)) &&
trackEvent.has(IterableConstants.KEY_DATA_FIELDS)) {
updateUserTrackPosition = i;
}
}
return updateUserTrackPosition;
}

private void handleTrackFailure(JSONObject data) {
if (data != null && data.has(IterableConstants.HTTP_STATUS_CODE)) {
try {
Expand Down
Loading