Skip to content

Commit 4d947d5

Browse files
authored
Merge pull request #418 from algolia/fix/events-object-data
2 parents 28f3aac + 107ce11 commit 4d947d5

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

client/src/commonMain/kotlin/com/algolia/search/model/insights/InsightsEvent.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.algolia.search.model.insights
22

3+
import ObjectData
34
import com.algolia.search.endpoint.EndpointInsights
45
import com.algolia.search.model.IndexName
56
import com.algolia.search.model.ObjectID
67
import com.algolia.search.model.QueryID
78
import com.algolia.search.model.filter.Filter
89
import com.algolia.search.model.filter.FilterConverter
10+
import com.algolia.search.serialize.internal.JsonNoDefaults
911
import com.algolia.search.serialize.internal.Key
1012
import com.algolia.search.serialize.internal.asJsonOutput
1113
import kotlinx.serialization.ExperimentalSerializationApi
@@ -39,7 +41,7 @@ public sealed class InsightsEvent {
3941
override val userToken: UserToken? = null,
4042
override val timestamp: Long? = null,
4143
override val queryID: QueryID? = null,
42-
override val resources: Resources? = null
44+
override val resources: Resources? = null,
4345
) : InsightsEvent()
4446

4547
public data class Click(
@@ -49,7 +51,7 @@ public sealed class InsightsEvent {
4951
override val timestamp: Long? = null,
5052
override val queryID: QueryID? = null,
5153
override val resources: Resources? = null,
52-
val positions: List<Int>? = null
54+
val positions: List<Int>? = null,
5355
) : InsightsEvent() {
5456

5557
init {
@@ -64,7 +66,8 @@ public sealed class InsightsEvent {
6466
override val userToken: UserToken? = null,
6567
override val timestamp: Long? = null,
6668
override val queryID: QueryID? = null,
67-
override val resources: Resources? = null
69+
override val resources: Resources? = null,
70+
val objectData: List<ObjectData>? = null
6871
) : InsightsEvent()
6972

7073
public sealed class Resources {
@@ -136,6 +139,17 @@ public sealed class InsightsEvent {
136139
)
137140
}
138141
}
142+
if (value is Conversion) {
143+
value.objectData?.let {
144+
put(
145+
Key.ObjectData,
146+
buildJsonArray {
147+
it.forEach { add(JsonNoDefaults.encodeToJsonElement(ObjectData.serializer(), it)) }
148+
}
149+
)
150+
151+
}
152+
}
139153
}
140154
encoder.asJsonOutput().encodeJsonElement(json)
141155
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import com.algolia.search.model.QueryID
2+
import com.algolia.search.serialize.internal.Key
3+
import kotlinx.serialization.SerialName
4+
import kotlinx.serialization.Serializable
5+
6+
/**
7+
* ObjectData
8+
*
9+
* @param queryID Unique identifier for a search query, used to track purchase events with multiple records that originate from different searches.
10+
* @param price
11+
* @param quantity Quantity of a product that has been purchased or added to the cart. The total purchase value is the sum of `quantity` multiplied with the `price` for each purchased item.
12+
* @param discount
13+
*/
14+
@Serializable
15+
public data class ObjectData(
16+
/**
17+
* Unique identifier for a search query, used to track purchase events with multiple records that originate from different searches.
18+
*/
19+
@SerialName(value = Key.QueryID) val queryID: QueryID? = null,
20+
21+
@SerialName(value = Key.Price) val price: String? = null,
22+
23+
/**
24+
* Quantity of a product that has been purchased or added to the cart. The total purchase value is the sum of `quantity` multiplied with the `price` for each purchased item.
25+
*/
26+
@SerialName(value = Key.Quantity) val quantity: Int? = null,
27+
28+
@SerialName(value = Key.Discount) val discount: String? = null,
29+
)

client/src/commonMain/kotlin/com/algolia/search/serialize/internal/Key.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,4 +440,8 @@ internal object Key {
440440
const val AlgoliaAgent = "X-Algolia-Agent"
441441
const val Extensions = "extensions"
442442
const val DeletedUntil = "deletedUntil"
443+
const val ObjectData: String = "objectData"
444+
const val Price: String = "price"
445+
const val Quantity: String = "quantity"
446+
const val Discount: String = "discount"
443447
}

client/src/commonTest/kotlin/model/insights/TestInsightsEvent.kt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
package model.insights
22

3+
import ObjectData
34
import attributeA
45
import com.algolia.search.helper.toEventName
56
import com.algolia.search.helper.toQueryID
67
import com.algolia.search.model.filter.Filter
78
import com.algolia.search.model.insights.InsightsEvent
9+
import com.algolia.search.serialize.internal.JsonNoDefaults
10+
import com.algolia.search.serialize.internal.Key
811
import indexA
912
import objectIDA
1013
import shouldEqual
1114
import shouldFailWith
1215
import kotlin.test.Test
16+
import kotlinx.serialization.json.buildJsonArray
17+
import kotlinx.serialization.json.buildJsonObject
18+
import kotlinx.serialization.json.jsonObject
19+
import kotlinx.serialization.json.put
1320

1421
internal class TestInsightsEvent {
1522

1623
private val eventName = "eventName".toEventName()
1724
private val filter = Filter.Facet(attributeA, "value")
25+
private val conversionEventWithObjectData = InsightsEvent.Conversion(
26+
eventName = eventName,
27+
indexName = indexA,
28+
objectData = listOf(
29+
ObjectData(price = "10", queryID = "queryID".toQueryID(), quantity = 1),
30+
ObjectData(price = "20", queryID = "queryID2".toQueryID(), quantity = 2)
31+
)
32+
)
1833

1934
@Test
2035
fun positionsAreRequired() {
@@ -48,4 +63,30 @@ internal class TestInsightsEvent {
4863
InsightsEvent.Resources.Filters(equalToTheSizeLimit).filters shouldEqual equalToTheSizeLimit
4964
shouldFailWith<IllegalArgumentException> { InsightsEvent.Resources.Filters(overTheSizeLimit) }
5065
}
66+
67+
@Test
68+
fun objectDataSerializer() {
69+
JsonNoDefaults.encodeToJsonElement(InsightsEvent.serializer(), conversionEventWithObjectData).jsonObject shouldEqual
70+
buildJsonObject {
71+
put(Key.EventType, Key.Conversion)
72+
put(Key.EventName, eventName.raw)
73+
put(Key.Index, indexA.raw)
74+
put(Key.ObjectData, buildJsonArray {
75+
add(
76+
buildJsonObject {
77+
put("price", "10")
78+
put("queryID", "queryID")
79+
put("quantity", 1)
80+
}
81+
)
82+
add(
83+
buildJsonObject {
84+
put("price", "20")
85+
put("queryID", "queryID2")
86+
put("quantity", 2)
87+
}
88+
)
89+
})
90+
}
91+
}
5192
}

0 commit comments

Comments
 (0)