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
1 change: 1 addition & 0 deletions library/src/main/java/io/constructor/core/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Constants {
const val EVENT = "tr"
const val API_KEY = "key"
const val NUM_RESULTS = "num_results_"
const val CUSTOMER_ID = "customer_ids"
const val GROUP_ID = "group[group_id]"
const val GROUP_DISPLAY_NAME = "group[display_name]"
const val USER_ID = "ui"
Expand Down
14 changes: 14 additions & 0 deletions library/src/main/java/io/constructor/core/ConstructorIo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,18 @@ object ConstructorIo {
}))
}

fun trackPurchase(clientIds: Array<String>, sectionName: String? = null, errorCallback: ConstructorError = null) {
val sessionId = preferenceHelper.getSessionId(sessionIncrementEventHandler)
val sectionNameParam = sectionName ?: preferenceHelper.defaultItemSection
val params = mutableListOf(Constants.QueryConstants.SESSION to sessionId.toString(),
Constants.QueryConstants.AUTOCOMPLETE_SECTION to sectionNameParam)
clientIds.forEach { params.add(Constants.QueryConstants.CUSTOMER_ID to it) }
disposable.add(dataManager.trackPurchase(params.toTypedArray()).subscribeOn(Schedulers.io())
.subscribe({}, { t ->
t.printStackTrace()
errorCallback?.invoke(t)
e("Input focus event error: ${t.message}")
}))
}

}
8 changes: 6 additions & 2 deletions library/src/main/java/io/constructor/data/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,16 @@ constructor(private val constructorApi: ConstructorApi) {
return constructorApi.trackSearchResultClickThrough(term, itemId, position, params.toMap())
}

fun trackSearchResultLoaded(term: String, reultCount: Int, params: Array<Pair<String, String>>): Completable {
return constructorApi.trackSearchResultLoaded(term, reultCount, params.toMap())
fun trackSearchResultLoaded(term: String, resultCount: Int, params: Array<Pair<String, String>>): Completable {
return constructorApi.trackSearchResultLoaded(term, resultCount, params.toMap())
}

fun trackInputFocus(term: String?, params: Array<Pair<String, String>>): Completable {
return constructorApi.trackInputFocus(term, params.toMap())
}

fun trackPurchase(params: Array<Pair<String, String>>): Completable {
return constructorApi.trackPurchase(params.toMap())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ object ApiPaths {
const val URL_CONVERT_EVENT = "autocomplete/{term}/conversion"
const val URL_CLICK_THROUGH_EVENT = "autocomplete/{term}/click_through"
const val URL_BEHAVIOR = "behavior"
const val URL_PURCHASE = "autocomplete/TERM_UNKNOWN/purchase"

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ interface ConstructorApi {

@GET(ApiPaths.URL_BEHAVIOR)
fun trackInputFocus(@Query("term") term: String?, @QueryMap params: Map<String, String>): Completable

@GET(ApiPaths.URL_PURCHASE)
fun trackPurchase(@QueryMap params: Map<String, String>): Completable
}
16 changes: 12 additions & 4 deletions library/src/test/java/io/constructor/core/ConstructorIoTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class ConstructorIoTest {

@Test
fun verifySessionStartUrl() {
val expected = "https://ac.cnstrc.com/behavior?c=cioand-0.1.0&s=1&action=session_start&key=testKey&_dt=1520000000000"
val expected = "https://ac.cnstrc.com/behavior?c=${BuildConfig.CLIENT_VERSION}&s=1&action=session_start&key=testKey&_dt=1520000000000"
val urlBuilder = HttpUrl.Builder().scheme("https")
.host("ac.cnstrc.com")
.addPathSegment("behavior")
Expand All @@ -107,7 +107,7 @@ class ConstructorIoTest {

@Test
fun verifySearchClickThroughEvent() {
val expected = "https://ac.cnstrc.com/autocomplete/term/click_through?c=cioand-0.1.0&s=1&autocomplete_section=Products&key=testKey&_dt=1520000000000"
val expected = "https://ac.cnstrc.com/autocomplete/term/click_through?c=${BuildConfig.CLIENT_VERSION}&s=1&autocomplete_section=Products&key=testKey&_dt=1520000000000"
val urlBuilder = HttpUrl.Builder().scheme("https")
.host("ac.cnstrc.com")
.addPathSegment("autocomplete")
Expand All @@ -124,7 +124,7 @@ class ConstructorIoTest {

@Test
fun verifySearchLoadedEventUrl() {
val expected = "https://ac.cnstrc.com/behavior?c=cioand-0.1.0&s=1&action=search-results&key=testKey&_dt=1520000000000"
val expected = "https://ac.cnstrc.com/behavior?c=${BuildConfig.CLIENT_VERSION}&s=1&action=search-results&key=testKey&_dt=1520000000000"
val urlBuilder = HttpUrl.Builder().scheme("https")
.host("ac.cnstrc.com")
.addPathSegment("behavior")
Expand All @@ -139,7 +139,7 @@ class ConstructorIoTest {

@Test
fun verifyInputFocusEvent() {
val expected = "https://ac.cnstrc.com/behavior?c=cioand-0.1.0&i=user_id&s=1&action=focus&key=testKey&_dt=1520000000000"
val expected = "https://ac.cnstrc.com/behavior?c=${BuildConfig.CLIENT_VERSION}&i=user_id&s=1&action=focus&key=testKey&_dt=1520000000000"
val urlBuilder = HttpUrl.Builder().scheme("https")
.host("ac.cnstrc.com")
.addPathSegment("behavior")
Expand Down Expand Up @@ -269,4 +269,12 @@ class ConstructorIoTest {
verify(exactly = 1) { data.trackInputFocus(any(), any()) }
}

@Test
fun trackPurchase() {
every { pref.defaultItemSection } returns "Products"
every { data.trackPurchase(any()) } returns Completable.complete()
constructorIo.trackPurchase(arrayOf("id1"))
verify(exactly = 1) { data.trackPurchase(any()) }
}

}
7 changes: 7 additions & 0 deletions library/src/test/java/io/constructor/data/DataManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,11 @@ class DataManagerTest {
verify(exactly = 1) { constructorApi.trackInputFocus(any(), any()) }
}

@Test
fun trackPurchase() {
every { constructorApi.trackPurchase(any()) } returns Completable.complete()
dataManager.trackPurchase(arrayOf())
verify(exactly = 1) { constructorApi.trackPurchase(any()) }
}

}