Skip to content

Commit

Permalink
feat: added support for custom track url
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed May 6, 2022
1 parent e0498b6 commit b61a64b
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class BaseTest {

@Before
open fun setup() {
cioConfig = CustomerIOConfig(siteId, "xyz", Region.EU, 100, null, true, true, 10, 30.0, CioLogLevel.DEBUG)
cioConfig = CustomerIOConfig(siteId, "xyz", Region.EU, 100, null, true, true, 10, 30.0, CioLogLevel.DEBUG, null)

// Initialize the mock web server before constructing DI graph as dependencies may require information such as hostname.
mockWebServer = MockWebServer().apply {
Expand Down
18 changes: 15 additions & 3 deletions sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import android.app.Activity
import android.app.Application
import android.content.pm.PackageManager
import io.customer.sdk.api.CustomerIOApi
import io.customer.sdk.data.model.CustomAttributes
import io.customer.sdk.data.communication.CustomerIOUrlHandler
import io.customer.sdk.data.model.CustomAttributes
import io.customer.sdk.data.model.Region
import io.customer.sdk.data.request.MetricEvent
import io.customer.sdk.di.CustomerIOComponent
Expand Down Expand Up @@ -98,6 +98,7 @@ class CustomerIO internal constructor(
private var shouldAutoRecordScreenViews: Boolean = false
private var autoTrackDeviceAttributes: Boolean = true
private var logLevel = CioLogLevel.ERROR
private var trackingApiUrl: String? = null

private lateinit var activityLifecycleCallback: CustomerIOActivityLifecycleCallbacks

Expand Down Expand Up @@ -126,6 +127,15 @@ class CustomerIO internal constructor(
return this
}

/**
* Base URL to use for the Customer.io track API. You will more then likely not modify this value.
* If you override this value, `Region` set when initializing the SDK will be ignored.
*/
fun setTrackingApiURL(trackingApiUrl: String): Builder {
this.trackingApiUrl = trackingApiUrl
return this
}

/**
* Override url/deep link handling
*
Expand Down Expand Up @@ -156,7 +166,8 @@ class CustomerIO internal constructor(
autoTrackDeviceAttributes = autoTrackDeviceAttributes,
backgroundQueueMinNumberOfTasks = 10,
backgroundQueueSecondsDelay = 30.0,
logLevel = logLevel
logLevel = logLevel,
trackingApiUrl = trackingApiUrl
)

val diGraph = CustomerIOComponent(sdkConfig = config, context = appContext)
Expand Down Expand Up @@ -278,7 +289,8 @@ class CustomerIO internal constructor(
* Register a new device token with Customer.io, associated with the current active customer. If there
* is no active customer, this will fail to register the device
*/
override fun registerDeviceToken(deviceToken: String) = api.registerDeviceToken(deviceToken, deviceAttributes)
override fun registerDeviceToken(deviceToken: String) =
api.registerDeviceToken(deviceToken, deviceAttributes)

/**
* Delete the currently registered device token
Expand Down
13 changes: 5 additions & 8 deletions sdk/src/main/java/io/customer/sdk/CustomerIOConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@ data class CustomerIOConfig(
val autoTrackScreenViews: Boolean,
val autoTrackDeviceAttributes: Boolean,
/**
Number of tasks in the background queue before the queue begins operating.
This is mostly used during development to test configuration is setup. We do not recommend
modifying this value because it impacts battery life of mobile device.
* Number of tasks in the background queue before the queue begins operating.
* This is mostly used during development to test configuration is setup. We do not recommend
* modifying this value because it impacts battery life of mobile device.
*/
val backgroundQueueMinNumberOfTasks: Int,
/**
* The number of seconds to delay running queue after a task has been added to it.
* We do not recommend modifying this value because it impacts battery life of mobile device.
*/

val backgroundQueueSecondsDelay: Double,
val logLevel: CioLogLevel,
/**
* Base URL to use for the Customer.io track API. You will more then likely not modify this value.
If you override this value, `Region` set when initializing the SDK will be ignored.
*/
var trackingApiUrl: String? = null
var trackingApiUrl: String?,
) {
internal val trackingApiHostname: String
get() {
Expand Down
42 changes: 39 additions & 3 deletions sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.customer.sdk

import android.net.Uri
import io.customer.common_test.BaseTest
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.customer.common_test.BaseTest
import io.customer.sdk.api.CustomerIOApi
import io.customer.sdk.data.communication.CustomerIOUrlHandler
import io.customer.sdk.data.model.Region
Expand Down Expand Up @@ -35,14 +35,16 @@ class CustomerIOTest : BaseTest() {
fun verifySDKConfigurationSetAfterBuild() {
val givenSiteId = String.random
val givenApiKey = String.random
val client = CustomerIO.Builder(
val builder = CustomerIO.Builder(
siteId = givenSiteId,
apiKey = givenApiKey,
region = Region.EU,
appContext = application
).setCustomerIOUrlHandler(object : CustomerIOUrlHandler {
override fun handleCustomerIOUrl(uri: Uri): Boolean = false
}).autoTrackScreenViews(true).build()
}).autoTrackScreenViews(true)

val client = builder.build()

val actual = client.diGraph.sdkConfig

Expand All @@ -52,6 +54,40 @@ class CustomerIOTest : BaseTest() {
actual.region shouldBeEqualTo Region.EU
actual.urlHandler.shouldNotBeNull()
actual.autoTrackScreenViews shouldBeEqualTo true
actual.trackingApiUrl shouldBeEqualTo null
actual.trackingApiHostname shouldBeEqualTo "https://track-sdk-eu.customer.io/"
}

@Test
fun verifyTrackingApiHostnameUpdateAfterUpdatingTrackingApiUrl() {
val givenSiteId = String.random
val givenApiKey = String.random
val builder = CustomerIO.Builder(
siteId = givenSiteId,
apiKey = givenApiKey,
region = Region.EU,
appContext = application
).setCustomerIOUrlHandler(object : CustomerIOUrlHandler {
override fun handleCustomerIOUrl(uri: Uri): Boolean = false
}).autoTrackScreenViews(true)

val client = builder.build()

val actual = client.diGraph.sdkConfig
actual.region shouldBeEqualTo Region.EU
actual.trackingApiUrl shouldBeEqualTo null
actual.trackingApiHostname shouldBeEqualTo "https://track-sdk-eu.customer.io/"

builder.setTrackingApiURL("https://local/track")

val updatedClient = builder.build()

val updatedConfig = updatedClient.diGraph.sdkConfig

// region stays the same but doesn't effect trackingApiHostname
updatedConfig.region shouldBeEqualTo Region.EU
updatedConfig.trackingApiUrl shouldBeEqualTo "https://local/track"
updatedConfig.trackingApiHostname shouldBeEqualTo "https://local/track"
}

@Test
Expand Down

0 comments on commit b61a64b

Please sign in to comment.