Skip to content

Commit 19afb08

Browse files
committed
Add session_time to OutcomeEvent
* Add `sessionTime` as an additional property to an `OutcomeEvent` object * This is for the influenced session ending outcomes that send session_time to the outcomes endpoint * For `OutcomeEventParams` constructor, don't make the parameter `timestamp` optional, so not to confuse with other parameters when instantiating an instance
1 parent 5d04aeb commit 19afb08

File tree

7 files changed

+27
-9
lines changed

7 files changed

+27
-9
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/IOutcomeEvent.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ interface IOutcomeEvent {
88
val notificationIds: JSONArray?
99
val name: String
1010
val timestamp: Long
11+
val sessionTime: Long // in seconds
1112
val weight: Float
1213
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomeConstants.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ internal object OutcomeConstants {
66
const val OUTCOME_SOURCES = "sources"
77
const val WEIGHT = "weight"
88
const val TIMESTAMP = "timestamp"
9+
const val SESSION_TIME = "session_time"
910

1011
// OSOutcomeSource Constants
1112
const val DIRECT = "direct"

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomeEvent.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ internal class OutcomeEvent(
1111
override val notificationIds: JSONArray?,
1212
override val name: String,
1313
override val timestamp: Long,
14+
override val sessionTime: Long,
1415
override val weight: Float,
1516
) : IOutcomeEvent {
1617
@Throws(JSONException::class)
@@ -20,6 +21,7 @@ internal class OutcomeEvent(
2021
json.put(NOTIFICATION_IDS, notificationIds)
2122
json.put(OUTCOME_ID, name)
2223
json.put(TIMESTAMP, timestamp)
24+
json.put(SESSION_TIME, sessionTime)
2325
json.put(WEIGHT, weight)
2426
return json
2527
}
@@ -28,11 +30,11 @@ internal class OutcomeEvent(
2830
if (this === o) return true
2931
if (o == null || this.javaClass != o.javaClass) return false
3032
val event = o as OutcomeEvent
31-
return session == event.session && notificationIds == event.notificationIds && name == event.name && timestamp == event.timestamp && weight == event.weight
33+
return session == event.session && notificationIds == event.notificationIds && name == event.name && timestamp == event.timestamp && sessionTime == event.sessionTime && weight == event.weight
3234
}
3335

3436
override fun hashCode(): Int {
35-
val a = arrayOf(session, notificationIds, name, timestamp, weight)
37+
val a = arrayOf(session, notificationIds, name, timestamp, sessionTime, weight)
3638
var result = 1
3739
for (element in a) result = 31 * result + (element?.hashCode() ?: 0)
3840
return result
@@ -44,6 +46,7 @@ internal class OutcomeEvent(
4446
", notificationIds=" + notificationIds +
4547
", name='" + name + '\'' +
4648
", timestamp=" + timestamp +
49+
", sessionTime=" + sessionTime +
4750
", weight=" + weight +
4851
'}'
4952
}
@@ -53,6 +56,7 @@ internal class OutcomeEvent(
5356
private const val NOTIFICATION_IDS = "notification_ids"
5457
private const val OUTCOME_ID = "id"
5558
private const val TIMESTAMP = "timestamp"
59+
private const val SESSION_TIME = "session_time"
5660
private const val WEIGHT = "weight"
5761

5862
/**
@@ -76,6 +80,7 @@ internal class OutcomeEvent(
7680
notificationId,
7781
outcomeEventParams.outcomeId,
7882
outcomeEventParams.timestamp,
83+
outcomeEventParams.sessionTime,
7984
outcomeEventParams.weight,
8085
)
8186
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomeEventParams.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ internal class OutcomeEventParams constructor(
77
val outcomeId: String,
88
val outcomeSource: OutcomeSource?, // This field is optional
99
var weight: Float, // This field is optional.
10-
var timestamp: Long = 0,
10+
var sessionTime: Long, // This field is optional
11+
var timestamp: Long,
1112
) {
1213
@Throws(JSONException::class)
1314
fun toJSONObject(): JSONObject {
@@ -18,6 +19,7 @@ internal class OutcomeEventParams constructor(
1819
}
1920
if (weight > 0) json.put(OutcomeConstants.WEIGHT, weight)
2021
if (timestamp > 0) json.put(OutcomeConstants.TIMESTAMP, timestamp)
22+
if (sessionTime > 0) json.put(OutcomeConstants.SESSION_TIME, sessionTime)
2123
return json
2224
}
2325

@@ -29,6 +31,7 @@ internal class OutcomeEventParams constructor(
2931
", outcomeSource=" + outcomeSource +
3032
", weight=" + weight +
3133
", timestamp=" + timestamp +
34+
", sessionTime=" + sessionTime +
3235
'}'
3336
}
3437
}

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomeEventsBackendService.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ internal class OutcomeEventsBackendService(private val _http: IHttpClient) :
3535
jsonObject.put("timestamp", event.timestamp)
3636
}
3737

38+
if (event.sessionTime > 0) {
39+
jsonObject.put("session_time", event.sessionTime)
40+
}
41+
3842
val response = _http.post("outcomes/measure", jsonObject)
3943

4044
if (!response.isSuccess) {

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomeEventsController.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ Outcome event was cached and will be reattempted on app cold start""",
8888

8989
override suspend fun sendOutcomeEvent(name: String): OutcomeEvent? {
9090
val influences: List<Influence> = _influenceManager.influences
91-
return sendAndCreateOutcomeEvent(name, 0f, influences)
91+
return sendAndCreateOutcomeEvent(name, 0f, 0, influences)
9292
}
9393

9494
override suspend fun sendOutcomeEventWithValue(name: String, weight: Float): OutcomeEvent? {
9595
val influences: List<Influence> = _influenceManager.influences
96-
return sendAndCreateOutcomeEvent(name, weight, influences)
96+
return sendAndCreateOutcomeEvent(name, weight, 0, influences)
9797
}
9898

9999
/**
@@ -134,7 +134,7 @@ Outcome event was cached and will be reattempted on app cold start""",
134134
// Return null to determine not a failure, but not a success in terms of the request made
135135
return null
136136
}
137-
return sendAndCreateOutcomeEvent(name, 0f, uniqueInfluences)
137+
return sendAndCreateOutcomeEvent(name, 0f, 0, uniqueInfluences)
138138
} else {
139139
// Make sure unique outcome has not been sent for current unattributed session
140140
if (unattributedUniqueOutcomeEventsSentOnSession.contains(name)) {
@@ -150,13 +150,14 @@ Outcome event was cached and will be reattempted on app cold start""",
150150
return null
151151
}
152152
unattributedUniqueOutcomeEventsSentOnSession.add(name)
153-
return sendAndCreateOutcomeEvent(name, 0f, influences)
153+
return sendAndCreateOutcomeEvent(name, 0f, 0, influences)
154154
}
155155
}
156156

157157
private suspend fun sendAndCreateOutcomeEvent(
158158
name: String,
159159
weight: Float,
160+
sessionTime: Long, // Note: this is optional
160161
influences: List<Influence>,
161162
): OutcomeEvent? {
162163
val timestampSeconds: Long = _time.currentTimeMillis / 1000
@@ -186,7 +187,7 @@ Outcome event was cached and will be reattempted on app cold start""",
186187
}
187188

188189
val source = OutcomeSource(directSourceBody, indirectSourceBody)
189-
val eventParams = OutcomeEventParams(name, source, weight, 0)
190+
val eventParams = OutcomeEventParams(name, source, weight, sessionTime, 0)
190191

191192
try {
192193
requestMeasureOutcomeEvent(eventParams)

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/session/internal/outcomes/impl/OutcomeEventsRepository.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ internal class OutcomeEventsRepository(
8888
put(OutcomeEventsTable.COLUMN_NAME_NAME, eventParams.outcomeId)
8989
put(OutcomeEventsTable.COLUMN_NAME_WEIGHT, eventParams.weight)
9090
put(OutcomeEventsTable.COLUMN_NAME_TIMESTAMP, eventParams.timestamp)
91+
put(OutcomeEventsTable.COLUMN_NAME_SESSION_TIME, eventParams.sessionTime)
9192
}.also { values ->
9293
_databaseProvider.os.insert(OutcomeEventsTable.TABLE_NAME, null, values)
9394
}
@@ -128,6 +129,8 @@ internal class OutcomeEventsRepository(
128129
cursor.getFloat(OutcomeEventsTable.COLUMN_NAME_WEIGHT)
129130
val timestamp =
130131
cursor.getLong(OutcomeEventsTable.COLUMN_NAME_TIMESTAMP)
132+
val sessionTime =
133+
cursor.getLong(OutcomeEventsTable.COLUMN_NAME_SESSION_TIME)
131134

132135
try {
133136
val directSourceBody = OutcomeSourceBody()
@@ -147,7 +150,7 @@ internal class OutcomeEventsRepository(
147150
it,
148151
)
149152
} ?: OutcomeSource(null, null)
150-
OutcomeEventParams(name, source, weight, timestamp).also {
153+
OutcomeEventParams(name, source, weight, sessionTime, timestamp).also {
151154
events.add(it)
152155
}
153156
} catch (e: JSONException) {

0 commit comments

Comments
 (0)