Skip to content

Commit

Permalink
feat!: register device attributes when set
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian committed May 6, 2022
1 parent e180dea commit 0f5159e
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/io/customer/example/MainActivity.kt
Expand Up @@ -24,7 +24,7 @@ class MainActivity : AppCompatActivity() {
}

private fun makeAddCustomDeviceAttributesRequest() {
CustomerIO.instance().deviceAttributes.putAll(mapOf("bingo" to "heyaa"))
CustomerIO.instance().deviceAttributes = mapOf("bingo" to "heyaa")
}

private fun makeRegisterDeviceRequest() {
Expand Down
7 changes: 6 additions & 1 deletion sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Expand Up @@ -302,7 +302,12 @@ class CustomerIO internal constructor(
* Use to provide additional and custom device attributes
* apart from the ones the SDK is programmed to send to customer workspace.
*/
val deviceAttributes: MutableMap<String, Any> = mutableMapOf()
var deviceAttributes: CustomAttributes = emptyMap()
set(value) {
field = value

api.addCustomDeviceAttributes(value)
}

private fun recordScreenViews(activity: Activity, attributes: CustomAttributes) {
val packageManager = activity.packageManager
Expand Down
13 changes: 13 additions & 0 deletions sdk/src/main/java/io/customer/sdk/CustomerIOClient.kt
Expand Up @@ -123,6 +123,19 @@ internal class CustomerIOClient(
backgroundQueue.queueRegisterDevice(identifiedProfileId, device)
}

override fun addCustomDeviceAttributes(attributes: CustomAttributes) {
logger.debug("adding custom device attributes request made")

val existingDeviceToken = preferenceRepository.getDeviceToken()

if (existingDeviceToken == null) {
logger.debug("no device token yet registered. ignoring request to add custom device attributes")
return
}

registerDeviceToken(existingDeviceToken, attributes)
}

private fun createDeviceAttributes(customAddedAttributes: CustomAttributes): Map<String, Any> {
if (!config.autoTrackDeviceAttributes) return customAddedAttributes

Expand Down
1 change: 1 addition & 0 deletions sdk/src/main/java/io/customer/sdk/api/CustomerIOApi.kt
Expand Up @@ -8,6 +8,7 @@ internal interface CustomerIOApi {
fun track(name: String, attributes: CustomAttributes)
fun clearIdentify()
fun registerDeviceToken(deviceToken: String, attributes: CustomAttributes)
fun addCustomDeviceAttributes(attributes: CustomAttributes)
fun deleteDeviceToken()
fun trackMetric(deliveryID: String, event: MetricEvent, deviceToken: String)
fun screen(name: String, attributes: CustomAttributes)
Expand Down
38 changes: 38 additions & 0 deletions sdk/src/sharedTest/java/io/customer/sdk/CustomerIOClientTest.kt
Expand Up @@ -221,6 +221,44 @@ class CustomerIOClientTest : BaseTest() {
prefRepository.getDeviceToken() shouldBeEqualTo givenDeviceToken
}

// addCustomDeviceAttributes

@Test
fun addCustomDeviceAttributes_givenNoPushToken_expectDoNotRegisterPushToken() {
val givenAttributes = mapOf(String.random to String.random)

customerIOClient.addCustomDeviceAttributes(givenAttributes)

// no token registered
verifyNoInteractions(backgroundQueueMock)
}

@Test
fun addCustomDeviceAttributes_givenExistingPushToken_expectRegisterPushTokenAndAttributes() {
val givenAttributes = mapOf(String.random to String.random)
val givenDeviceToken = String.random
val givenIdentifier = String.random
prefRepository.saveDeviceToken(givenDeviceToken)
prefRepository.saveIdentifier(givenIdentifier)

customerIOClient.addCustomDeviceAttributes(givenAttributes)

// a token got registered
verify(backgroundQueueMock).addTask(
QueueTaskType.RegisterDeviceToken,
RegisterPushNotificationQueueTaskData(
givenIdentifier,
Device(
token = givenDeviceToken,
lastUsed = dateUtilStub.givenDate,
attributes = deviceStore.buildDeviceAttributes() + givenAttributes
)
),
groupStart = QueueTaskGroup.RegisterPushToken(givenDeviceToken),
blockingGroups = listOf(QueueTaskGroup.IdentifyProfile(givenIdentifier))
)
}

// deleteDeviceToken

@Test
Expand Down
10 changes: 10 additions & 0 deletions sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt
Expand Up @@ -13,6 +13,7 @@ import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify

@RunWith(AndroidJUnit4::class)
class CustomerIOTest : BaseTest() {
Expand Down Expand Up @@ -52,4 +53,13 @@ class CustomerIOTest : BaseTest() {
actual.urlHandler.shouldNotBeNull()
actual.autoTrackScreenViews shouldBeEqualTo true
}

@Test
fun deviceAttributes_givenSetValue_expectMakeRequestToAddAttributes() {
val givenAttributes = mapOf(String.random to String.random)

customerIO.deviceAttributes = givenAttributes

verify(apiMock).addCustomDeviceAttributes(givenAttributes)
}
}

0 comments on commit 0f5159e

Please sign in to comment.