Skip to content
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

fix: sessionId in first open and session start events #68

Merged
merged 1 commit into from
Mar 4, 2024
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 @@ -96,22 +96,26 @@ public Session(final String sessionID, final Long startTime, final Long pauseTim
* Get the session object from preferences, if exists and not expired return it.
* otherwise create a new session.
*
* @param context The {@link ClickstreamContext}.
* @param context The {@link ClickstreamContext}.
* @param previousSession The previous session for check whether is new session.
* @return new Session object.
*/
public static Session getInstance(final ClickstreamContext context) {
// get session from preference
Session storedSession = PreferencesUtil.getSession(context.getSystem().getPreferences());
int sessionIndex = 1;
if (storedSession != null) {
if (System.currentTimeMillis() - storedSession.getPauseTime() <
context.getClickstreamConfiguration().getSessionTimeoutDuration()) {
return storedSession;
public static Session getInstance(final ClickstreamContext context, Session previousSession) {
Session session = previousSession;
if (session == null) {
session = PreferencesUtil.getSession(context.getSystem().getPreferences());
}
if (session != null) {
if (session.getPauseTime() == null ||
System.currentTimeMillis() - session.getPauseTime() <
context.getClickstreamConfiguration().getSessionTimeoutDuration()) {
return session;
} else {
sessionIndex = storedSession.sessionIndex + 1;
return new Session(context, session.getSessionIndex() + 1);
}
} else {
return new Session(context, 1);
}
return new Session(context, sessionIndex);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public SessionClient(@NonNull final ClickstreamContext clickstreamContext) {
throw new IllegalArgumentException("A valid AnalyticsClient must be provided!");
}
this.clickstreamContext = clickstreamContext;
session = Session.getInstance(clickstreamContext);
session = Session.getInstance(clickstreamContext, null);
this.clickstreamContext.getAnalyticsClient().setSession(session);
}

Expand All @@ -58,7 +58,7 @@ public SessionClient(@NonNull final ClickstreamContext clickstreamContext) {
* @return is new session.
*/
public synchronized boolean initialSession() {
session = Session.getInstance(clickstreamContext);
session = Session.getInstance(clickstreamContext, session);
this.clickstreamContext.getAnalyticsClient().setSession(session);
if (session.isNewSession()) {
final AnalyticsEvent event =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
import software.aws.solution.clickstream.util.ReflectUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

Expand Down Expand Up @@ -131,6 +133,40 @@ public void testUserEngagementSuccess() throws Exception {
}
}


/**
* test first open screen view and session start events will have the same sessionId.
*
* @throws Exception exception.
*/
@Test
public void testEventsHaveSameSessionId() throws Exception {
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START);
Activity activity = mock(Activity.class);
Bundle bundle = mock(Bundle.class);
callbacks.onActivityCreated(activity, bundle);
callbacks.onActivityStarted(activity);
callbacks.onActivityResumed(activity);
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP);
try (Cursor cursor = dbUtil.queryAllEvents()) {
List<String> eventList = new ArrayList<>();
Set<String> sessionIdSet = new HashSet<>();
while (cursor.moveToNext()) {
String eventString = cursor.getString(2);
JSONObject jsonObject = new JSONObject(eventString);
String eventName = jsonObject.getString("event_type");
eventList.add(eventName);
sessionIdSet.add(jsonObject.getJSONObject("attributes").getString("_session_id"));
}
assertEquals(Event.PresetEvent.FIRST_OPEN, eventList.get(0));
assertEquals(Event.PresetEvent.APP_START, eventList.get(1));
assertEquals(Event.PresetEvent.SESSION_START, eventList.get(2));
assertEquals(Event.PresetEvent.SCREEN_VIEW, eventList.get(3));
assertEquals(Event.PresetEvent.APP_END, eventList.get(4));
assertEquals(1, sessionIdSet.size());
}
}

/**
* test record user engagement event after view screen less than 1 second.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ public void testDelete() {
@Test
public void testGetTotalDbSize() {
AnalyticsEvent analyticsEvent = AnalyticsEventTest.getAnalyticsClient().createEvent("testEvent");
int eventLength = analyticsEvent.toJSONObject().toString().length();
int eventLength1 = analyticsEvent.toJSONObject().toString().length();
Uri uri1 = dbUtil.saveEvent(analyticsEvent);
int eventLength2 = analyticsEvent.toJSONObject().toString().length();
Uri uri2 = dbUtil.saveEvent(analyticsEvent);
int idInserted1 = Integer.parseInt(uri1.getLastPathSegment());
int idInserted2 = Integer.parseInt(uri2.getLastPathSegment());
Expand All @@ -127,7 +128,7 @@ public void testGetTotalDbSize() {
assertNotNull(c);
assertEquals(c.getCount(), 2);
c.close();
Assert.assertEquals(dbUtil.getTotalSize(), eventLength * 2);
Assert.assertEquals(dbUtil.getTotalSize(), eventLength1 + eventLength2);
}

/**
Expand Down
Loading