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
22 changes: 12 additions & 10 deletions src/main/java/com/amplitude/api/AmplitudeClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ public void logEventSync(String eventType, JSONObject eventProperties, JSONObjec
*/
public void logEventSync(String eventType, JSONObject eventProperties, JSONObject groups, long timestamp, boolean outOfSession) {
if (validateLogEvent(eventType)) {
logEvent(eventType, eventProperties, null, null, groups, null, timestamp, outOfSession);
logEvent(eventType, eventProperties, null, null, groups, null, timestamp, outOfSession, this.inForeground);
}
}

Expand Down Expand Up @@ -1172,6 +1172,7 @@ protected void logEventAsync(final String eventType, JSONObject eventProperties,
final JSONObject copyUserProperties = userProperties;
final JSONObject copyGroups = groups;
final JSONObject copyGroupProperties = groupProperties;
final boolean isForeground = this.inForeground;
runOnLogThread(new Runnable() {
@Override
public void run() {
Expand All @@ -1180,7 +1181,8 @@ public void run() {
}
logEvent(
eventType, copyEventProperties, copyApiProperties,
copyUserProperties, copyGroups, copyGroupProperties, timestamp, outOfSession, extra
copyUserProperties, copyGroups, copyGroupProperties, timestamp, outOfSession, extra,
isForeground
);
}
});
Expand All @@ -1197,17 +1199,18 @@ public void run() {
* @param groups the groups
* @param timestamp the timestamp
* @param outOfSession the out of session
* @param inForeground in foreground
* @return the event ID if succeeded, else -1.
*/
protected long logEvent(String eventType, JSONObject eventProperties, JSONObject apiProperties,
JSONObject userProperties, JSONObject groups, JSONObject groupProperties,
long timestamp, boolean outOfSession) {
return logEvent(eventType, eventProperties, apiProperties, userProperties, groups, groupProperties, timestamp,outOfSession, null);
long timestamp, boolean outOfSession, boolean inForeground) {
return logEvent(eventType, eventProperties, apiProperties, userProperties, groups, groupProperties, timestamp, outOfSession, null, inForeground);
}

protected long logEvent(String eventType, JSONObject eventProperties, JSONObject apiProperties,
JSONObject userProperties, JSONObject groups, JSONObject groupProperties,
long timestamp, boolean outOfSession, MiddlewareExtra extra) {
long timestamp, boolean outOfSession, MiddlewareExtra extra, boolean inForeground) {

logger.d(TAG, "Logged event to Amplitude: " + eventType);

Expand Down Expand Up @@ -1480,7 +1483,6 @@ void setPreviousSessionId(long timestamp) {
*/
public boolean startNewSessionIfNeeded(long timestamp) {
if (inSession()) {

if (isWithinMinTimeBetweenSessions(timestamp)) {
refreshSessionTime(timestamp);
return false;
Expand Down Expand Up @@ -1565,7 +1567,7 @@ private void sendSessionEvent(final String sessionEvent) {
return;
}

logEvent(sessionEvent, null, apiProperties, null, null, null, lastEventTime, false);
logEvent(sessionEvent, null, apiProperties, null, null, null, lastEventTime, false, false);
}

/**
Expand All @@ -1574,14 +1576,14 @@ private void sendSessionEvent(final String sessionEvent) {
* @param timestamp the timestamp
*/
void onExitForeground(final long timestamp) {
inForeground = false;
runOnLogThread(new Runnable() {
@Override
public void run() {
if (Utils.isEmptyString(apiKey)) {
return;
}
refreshSessionTime(timestamp);
inForeground = false;
if (flushEventsOnClose) {
identifyInterceptor.transferInterceptedIdentify();
updateServer();
Expand All @@ -1603,6 +1605,7 @@ public void run() {
* @param timestamp the timestamp
*/
void onEnterForeground(final long timestamp) {
inForeground = true;
runOnLogThread(new Runnable() {
@Override
public void run() {
Expand All @@ -1618,7 +1621,6 @@ public void onFinished() {
}, serverZone);
}
startNewSessionIfNeeded(timestamp);
Comment thread
justin-fiedler marked this conversation as resolved.
inForeground = true;
}
});
}
Expand Down Expand Up @@ -1707,7 +1709,7 @@ public void logRevenueV2(Revenue revenue, MiddlewareExtra extra) {
return;
}

logEvent(Constants.AMP_REVENUE_EVENT, revenue.toJSONObject(), null, null, null, null, getCurrentTimeMillis(), false, extra);
logEvent(Constants.AMP_REVENUE_EVENT, revenue.toJSONObject(), null, null, null, null, getCurrentTimeMillis(), false, extra, this.inForeground);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/com/amplitude/api/SessionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void testDefaultStartSessionWithTrackingSynchronous() {
amplitude.trackSessionEvents(true);

long timestamp = System.currentTimeMillis();
amplitude.logEvent("test", null, null, null, null, null, timestamp, false);
amplitude.logEventSync("test", null, null, timestamp, false);
Shadows.shadowOf(amplitude.logThread.getLooper()).runToEndOfTasks();
// trackSessions is true, start_session event is added
assertEquals(getUnsentEventCount(), 2);
Expand Down Expand Up @@ -259,14 +259,14 @@ public void testDefaultTriggerNewSessionWithTrackingSynchronous() {

// log 1st event, initialize first session
long timestamp1 = System.currentTimeMillis();
amplitude.logEvent("test1", null, null, null, null, null, timestamp1, false);
amplitude.logEventSync("test1", null, null, timestamp1, false);
looper.runToEndOfTasks();
// trackSessions is true, start_session event is added
assertEquals(getUnsentEventCount(), 2);

// log 2nd event past timeout, verify new session started
long timestamp2 = timestamp1 + sessionTimeoutMillis;
amplitude.logEvent("test2", null, null, null, null, null, timestamp2, false);
amplitude.logEventSync("test2", null, null, timestamp2, false);
looper.runToEndOfTasks();
// trackSessions is true, end_session and start_session events are added
assertEquals(getUnsentEventCount(), 5);
Expand Down Expand Up @@ -370,13 +370,13 @@ public void testDefaultExtendSessionWithTrackingSynchronous() {

// log 3 events all just within session expiration window, verify all in same session
long timestamp1 = System.currentTimeMillis();
amplitude.logEvent("test1", null, null, null, null, null, timestamp1, false);
amplitude.logEventSync("test1", null, null, timestamp1, false);
looper.runToEndOfTasks();
// trackSessions is true, start_session event is added
assertEquals(getUnsentEventCount(), 2);

long timestamp2 = timestamp1 + sessionTimeoutMillis - 1;
amplitude.logEvent("test2", null, null, null, null, null, timestamp2, false);
amplitude.logEventSync("test2", null, null, timestamp2, false);
looper.runToEndOfTasks();
assertEquals(getUnsentEventCount(), 3);

Expand Down