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

[MOB-7218] impression tracking rework #650

Merged
merged 5 commits into from
Oct 27, 2023
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 @@ -6,11 +6,13 @@ import java.util.Date
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
data class EmbeddedImpressionData(
val messageId: String,
val placementId: Long,
var displayCount: Int = 0,
var duration: Float = 0.0f,
var start: Date? = null
) {
constructor(
messageId: String
) : this(messageId, 0, 0.0f, null)
messageId: String,
placementId: Long
) : this(messageId, placementId,0, 0.0f, null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class EmbeddedSessionManager {
var session: IterableEmbeddedSession = IterableEmbeddedSession(
null,
null,
"0",
null
)

Expand All @@ -30,7 +29,6 @@ public class EmbeddedSessionManager {
session = IterableEmbeddedSession(
Date(),
null,
"0",
null
)
}
Expand All @@ -47,7 +45,6 @@ public class EmbeddedSessionManager {
val sessionToTrack = IterableEmbeddedSession(
session.start,
Date(),
"0",
getImpressionList()
)

Expand All @@ -57,19 +54,18 @@ public class EmbeddedSessionManager {
session = IterableEmbeddedSession(
null,
null,
"0",
null
)

impressions = mutableMapOf()
}
}

fun startImpression(messageId: String) {
fun startImpression(messageId: String, placementId: Long) {
var impressionData: EmbeddedImpressionData? = impressions[messageId]

if (impressionData == null) {
impressionData = EmbeddedImpressionData(messageId)
impressionData = EmbeddedImpressionData(messageId, placementId)
impressions[messageId] = impressionData
}

Expand Down Expand Up @@ -104,6 +100,7 @@ public class EmbeddedSessionManager {
impressionList.add(
IterableEmbeddedImpression(
impressionData.messageId,
impressionData.placementId,
impressionData.displayCount,
impressionData.duration
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,13 +455,13 @@ public void trackEmbeddedSession(@NonNull IterableEmbeddedSession session) {
sessionJson.put(IterableConstants.ITERABLE_EMBEDDED_SESSION_END, session.getEnd().getTime());

requestJSON.put(IterableConstants.ITERABLE_EMBEDDED_SESSION, sessionJson);
requestJSON.put(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID, session.getPlacementId());

if (session.getImpressions() != null) {
JSONArray impressionsJsonArray = new JSONArray();
for (IterableEmbeddedImpression impression : session.getImpressions()) {
JSONObject impressionJson = new JSONObject();
impressionJson.put(IterableConstants.KEY_MESSAGE_ID, impression.getMessageId());
impressionJson.put(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID, impression.getPlacementId());
impressionJson.put(IterableConstants.ITERABLE_EMBEDDED_IMP_DISPLAY_COUNT, impression.getDisplayCount());
impressionJson.put(IterableConstants.ITERABLE_EMBEDDED_IMP_DISPLAY_DURATION, impression.getDuration());
impressionsJsonArray.put(impressionJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public final class IterableConstants {
public static final String KEY_SUB_MESSAGE = "subscribedMessageTypeIds";
public static final String KEY_USER_ID = "userId";
public static final String KEY_USER = "user";
public static final String KEY_USER_KEY = "userKey";
public static final String KEY_USER_TEXT = "userText";
public static final String KEY_INBOX_SESSION_ID = "inboxSessionId";
public static final String KEY_EMBEDDED_SESSION_ID = "id";
Expand Down Expand Up @@ -80,7 +79,7 @@ public final class IterableConstants {
public static final String ENDPOINT_GET_EMBEDDED_MESSAGES = "embedded-messaging/messages";
public static final String ENDPOINT_TRACK_EMBEDDED_RECEIVED = "embedded-messaging/events/received";
public static final String ENDPOINT_TRACK_EMBEDDED_CLICK = "embedded-messaging/events/click";
public static final String ENDPOINT_TRACK_EMBEDDED_SESSION = "embedded-messaging/events/impression";
public static final String ENDPOINT_TRACK_EMBEDDED_SESSION = "embedded-messaging/events/session";

public static final String PUSH_APP_ID = "IterableAppId";
public static final String PUSH_GCM_PROJECT_NUMBER = "GCMProjectNumber";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.json.JSONException
import org.json.JSONObject

data class IterableEmbeddedPlacement(
val placementId: String,
val placementId: Long,
val messages: List<IterableEmbeddedMessage>
) {
companion object {
Expand All @@ -17,13 +17,11 @@ data class IterableEmbeddedPlacement(
try {
embeddedPlacementJson.put(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID, placement.placementId)

if(placement?.messages != null) {
val messagesJson = JSONArray()
for(i in 0 until placement.messages.size) {
messagesJson.put(IterableEmbeddedMessage.toJSONObject(placement.messages[i]))
}
embeddedPlacementJson.put(IterableConstants.ITERABLE_EMBEDDED_MESSAGE, messagesJson)
val messagesJson = JSONArray()
for(i in 0 until placement.messages.size) {
messagesJson.put(IterableEmbeddedMessage.toJSONObject(placement.messages[i]))
}
embeddedPlacementJson.put(IterableConstants.ITERABLE_EMBEDDED_MESSAGE, messagesJson)
} catch(e: JSONException) {
IterableLogger.e(TAG, "Error while serializing flex message", e)
}
Expand All @@ -32,7 +30,7 @@ data class IterableEmbeddedPlacement(
}

fun fromJSONObject(placementJson: JSONObject): IterableEmbeddedPlacement {
val placementId: String = placementJson.getString(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID)
val placementId: Long = placementJson.getLong(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID)

val messagesJson: JSONArray = placementJson.getJSONArray(IterableConstants.ITERABLE_EMBEDDED_MESSAGE)
var messages: MutableList<IterableEmbeddedMessage> = mutableListOf()
Expand Down Expand Up @@ -86,8 +84,7 @@ data class IterableEmbeddedMessage (

class EmbeddedMessageMetadata(
var messageId: String,
//TODO: Remove this once the placementIDs are implemented in the backend
val placementId: String? = "",
val placementId: Long,
val campaignId: Int? = null,
val isProof: Boolean = false
) {
Expand All @@ -111,7 +108,7 @@ class EmbeddedMessageMetadata(

fun fromJSONObject(metadataJson: JSONObject): EmbeddedMessageMetadata {
val messageId: String = metadataJson.getString(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_ID)
val placementId: String = metadataJson.optString(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID)
val placementId: Long = metadataJson.optLong(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID)
val campaignId: Int = metadataJson.optInt(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_CAMPAIGN_ID)
val isProof: Boolean = metadataJson.optBoolean(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_IS_PROOF)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ import java.util.UUID
public data class IterableEmbeddedSession(
val start: Date?,
val end: Date?,
val placementId: String?,
val impressions: List<IterableEmbeddedImpression>?,
val id: String
) {
constructor(
start: Date?,
end: Date?,
placementId: String,
impressions: List<IterableEmbeddedImpression>?
) : this(start, end, placementId, impressions, UUID.randomUUID().toString())
) : this(start, end, impressions, UUID.randomUUID().toString())
}

class IterableEmbeddedImpression(
val messageId: String,
val placementId: Long,
val displayCount: Int,
val duration: Float
)
Original file line number Diff line number Diff line change
Expand Up @@ -704,19 +704,20 @@ public void testEmbeddedSession() throws Exception {
List<IterableEmbeddedImpression> impressions = new ArrayList<>();
impressions.add(new IterableEmbeddedImpression(
"messageId1",
0,
1,
2.0f
));
impressions.add(new IterableEmbeddedImpression(
"messageId2",
0,
3,
6.5f
));

IterableEmbeddedSession session = new IterableEmbeddedSession(
sessionStartTime,
new Date(sessionStartTime.getTime() + 3600),
"0",
impressions);

IterableApi.getInstance().trackEmbeddedSession(session);
Expand All @@ -729,7 +730,6 @@ public void testEmbeddedSession() throws Exception {
JSONObject requestJson = new JSONObject(trackEmbeddedSessionRequest.getBody().readUtf8());

// Check top-level fields
assertEquals("0", requestJson.getString(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID));
verifyDeviceInfo(requestJson);

// Check session data
Expand All @@ -742,6 +742,7 @@ public void testEmbeddedSession() throws Exception {
JSONArray impressionsJsonArray = requestJson.getJSONArray(IterableConstants.ITERABLE_EMBEDDED_IMPRESSIONS);
assertEquals(2, impressionsJsonArray.length());
assertEquals("messageId1", impressionsJsonArray.getJSONObject(0).getString(IterableConstants.KEY_MESSAGE_ID));
assertEquals(0, impressionsJsonArray.getJSONObject(0).getLong(IterableConstants.ITERABLE_EMBEDDED_MESSAGE_PLACEMENT_ID));
assertEquals(1, impressionsJsonArray.getJSONObject(0).getInt(IterableConstants.ITERABLE_EMBEDDED_IMP_DISPLAY_COUNT));
assertEquals(2.0, impressionsJsonArray.getJSONObject(0).getDouble(IterableConstants.ITERABLE_EMBEDDED_IMP_DISPLAY_DURATION));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class IterableEmbeddedPlacementTest {

// THEN we get appropriate embedded message object and associated placement id
assertNotNull(placement)
assertThat("0", `is` (placement.placementId))
assertThat(411L, `is`(placement.placementId))

assertThat("doibjo4590340oidiobnw", `is` (message.metadata.messageId))
assertThat("mbn8489b7ehycy", `is` (message.metadata.placementId))
assertThat(411L, `is` (message.metadata.placementId))
assertThat(2324,`is` (message.metadata.campaignId))
assertThat(true, `is` (message.metadata.isProof))

Expand Down Expand Up @@ -70,10 +70,10 @@ class IterableEmbeddedPlacementTest {

// THEN we get appropriate embedded message object and associated placement id
assertNotNull(placement)
assertThat("0", `is` (placement.placementId))
assertThat(411L, `is` (placement.placementId))

assertThat("doibjo4590340oidiobnw", `is` (message.metadata.messageId))
assertThat("mbn8489b7ehycy", `is` (message.metadata.placementId))
assertThat(411L, `is` (message.metadata.placementId))
assertThat(2324,`is` (message.metadata.campaignId))
assertThat(true, `is` (message.metadata.isProof))

Expand Down Expand Up @@ -117,10 +117,10 @@ class IterableEmbeddedPlacementTest {

// THEN we get appropriate embedded message object and associated placement id
assertNotNull(placement)
assertThat("0", `is` (placement.placementId))
assertThat(411L, `is` (placement.placementId))

assertThat("doibjo4590340oidiobnw", `is` (message.metadata.messageId))
assertThat("mbn8489b7ehycy", `is` (message.metadata.placementId))
assertThat(411L, `is` (message.metadata.placementId))
assertThat(2324,`is` (message.metadata.campaignId))
assertThat(true, `is` (message.metadata.isProof))

Expand Down Expand Up @@ -154,10 +154,10 @@ class IterableEmbeddedPlacementTest {

// THEN we get appropriate embedded message object and associated placement id
assertNotNull(placement)
assertThat("0", `is` (placement.placementId))
assertThat(411L, `is` (placement.placementId))

assertThat("doibjo4590340oidiobnw", `is` (message.metadata.messageId))
assertThat("mbn8489b7ehycy", `is` (message.metadata.placementId))
assertThat(411L, `is` (message.metadata.placementId))
assertThat(2324,`is` (message.metadata.campaignId))
assertThat(true, `is` (message.metadata.isProof))

Expand All @@ -170,7 +170,7 @@ class IterableEmbeddedPlacementTest {
fun embeddedPlacementSerialization_elementsAndCustomPayloadDefined() {
val embeddedMessageMetadata = EmbeddedMessageMetadata(
"doibjo4590340oidiobnw",
"mbn8489b7ehycy",
411L,
2324,
true
)
Expand Down Expand Up @@ -205,7 +205,7 @@ class IterableEmbeddedPlacementTest {

val embeddedMessage = IterableEmbeddedMessage(embeddedMessageMetadata, embeddedMessageElements, customPayload)

val placementId: String = "0"
val placementId: Long = 411L
Copy link
Member

Choose a reason for hiding this comment

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

I hope this 'L' doesnt cause much trouble in future. If it does, it will be wise to typecast all placementId into string.

val messages: List<IterableEmbeddedMessage> = listOf(embeddedMessage)

val embeddedMessagePlacement = IterableEmbeddedPlacement(placementId, messages)
Expand All @@ -229,7 +229,7 @@ class IterableEmbeddedPlacementTest {
fun embeddedPlacementSerialization_noButtons_noText() {
val embeddedMessageMetadata = EmbeddedMessageMetadata(
"doibjo4590340oidiobnw",
"mbn8489b7ehycy",
411L,
2324,
true
)
Expand All @@ -250,7 +250,7 @@ class IterableEmbeddedPlacementTest {

val embeddedMessage = IterableEmbeddedMessage(embeddedMessageMetadata, embeddedMessageElements, customPayload)

val placementId: String = "0"
val placementId: Long = 411L
val messages: List<IterableEmbeddedMessage> = listOf(embeddedMessage)

val embeddedMessagePlacement = IterableEmbeddedPlacement(placementId, messages)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"placements": [
{
"placementId": "0",
"placementId": 411,
"embeddedMessages": [
{
"metadata": {
"messageId": "doibjo4590340oidiobnw",
"placementId": "mbn8489b7ehycy",
"placementId": 411,
"campaignId": 2324,
"isProof": true
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"placements": [
{
"placementId": "0",
"placementId": 411,
Copy link
Member

Choose a reason for hiding this comment

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

We should have a really long value here for testing purpose. I worry if there will be some data loss in json conversion or when requests are to be made and we loose the precision of the long

Copy link
Contributor Author

@evantk91 evantk91 Oct 27, 2023

Choose a reason for hiding this comment

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

Hm true since the json does not allow for longs. We will have to keep this in mind. Merging for now. Will revisit later.

"embeddedMessages": [
{
"metadata": {
"messageId": "doibjo4590340oidiobnw",
"placementId": "mbn8489b7ehycy",
"placementId": 411,
"campaignId": 2324,
"isProof": true
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"placements": [
{
"placementId": "0",
"placementId": 411,
"embeddedMessages": [
{
"metadata": {
"messageId": "doibjo4590340oidiobnw",
"placementId": "mbn8489b7ehycy",
"placementId": 411,
"campaignId": 2324,
"isProof": true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"placements": [
{
"placementId": "0",
"placementId": 411,
"embeddedMessages": [
{
"metadata": {
"messageId": "doibjo4590340oidiobnw",
"placementId": "mbn8489b7ehycy",
"placementId": 411,
"campaignId": 2324,
"isProof": true
},
Expand Down
Loading