Skip to content

Commit 0f5159e

Browse files
authored
feat!: register device attributes when set
1 parent e180dea commit 0f5159e

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

app/src/main/java/io/customer/example/MainActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MainActivity : AppCompatActivity() {
2424
}
2525

2626
private fun makeAddCustomDeviceAttributesRequest() {
27-
CustomerIO.instance().deviceAttributes.putAll(mapOf("bingo" to "heyaa"))
27+
CustomerIO.instance().deviceAttributes = mapOf("bingo" to "heyaa")
2828
}
2929

3030
private fun makeRegisterDeviceRequest() {

sdk/src/main/java/io/customer/sdk/CustomerIO.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,12 @@ class CustomerIO internal constructor(
302302
* Use to provide additional and custom device attributes
303303
* apart from the ones the SDK is programmed to send to customer workspace.
304304
*/
305-
val deviceAttributes: MutableMap<String, Any> = mutableMapOf()
305+
var deviceAttributes: CustomAttributes = emptyMap()
306+
set(value) {
307+
field = value
308+
309+
api.addCustomDeviceAttributes(value)
310+
}
306311

307312
private fun recordScreenViews(activity: Activity, attributes: CustomAttributes) {
308313
val packageManager = activity.packageManager

sdk/src/main/java/io/customer/sdk/CustomerIOClient.kt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ internal class CustomerIOClient(
123123
backgroundQueue.queueRegisterDevice(identifiedProfileId, device)
124124
}
125125

126+
override fun addCustomDeviceAttributes(attributes: CustomAttributes) {
127+
logger.debug("adding custom device attributes request made")
128+
129+
val existingDeviceToken = preferenceRepository.getDeviceToken()
130+
131+
if (existingDeviceToken == null) {
132+
logger.debug("no device token yet registered. ignoring request to add custom device attributes")
133+
return
134+
}
135+
136+
registerDeviceToken(existingDeviceToken, attributes)
137+
}
138+
126139
private fun createDeviceAttributes(customAddedAttributes: CustomAttributes): Map<String, Any> {
127140
if (!config.autoTrackDeviceAttributes) return customAddedAttributes
128141

sdk/src/main/java/io/customer/sdk/api/CustomerIOApi.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ internal interface CustomerIOApi {
88
fun track(name: String, attributes: CustomAttributes)
99
fun clearIdentify()
1010
fun registerDeviceToken(deviceToken: String, attributes: CustomAttributes)
11+
fun addCustomDeviceAttributes(attributes: CustomAttributes)
1112
fun deleteDeviceToken()
1213
fun trackMetric(deliveryID: String, event: MetricEvent, deviceToken: String)
1314
fun screen(name: String, attributes: CustomAttributes)

sdk/src/sharedTest/java/io/customer/sdk/CustomerIOClientTest.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,44 @@ class CustomerIOClientTest : BaseTest() {
221221
prefRepository.getDeviceToken() shouldBeEqualTo givenDeviceToken
222222
}
223223

224+
// addCustomDeviceAttributes
225+
226+
@Test
227+
fun addCustomDeviceAttributes_givenNoPushToken_expectDoNotRegisterPushToken() {
228+
val givenAttributes = mapOf(String.random to String.random)
229+
230+
customerIOClient.addCustomDeviceAttributes(givenAttributes)
231+
232+
// no token registered
233+
verifyNoInteractions(backgroundQueueMock)
234+
}
235+
236+
@Test
237+
fun addCustomDeviceAttributes_givenExistingPushToken_expectRegisterPushTokenAndAttributes() {
238+
val givenAttributes = mapOf(String.random to String.random)
239+
val givenDeviceToken = String.random
240+
val givenIdentifier = String.random
241+
prefRepository.saveDeviceToken(givenDeviceToken)
242+
prefRepository.saveIdentifier(givenIdentifier)
243+
244+
customerIOClient.addCustomDeviceAttributes(givenAttributes)
245+
246+
// a token got registered
247+
verify(backgroundQueueMock).addTask(
248+
QueueTaskType.RegisterDeviceToken,
249+
RegisterPushNotificationQueueTaskData(
250+
givenIdentifier,
251+
Device(
252+
token = givenDeviceToken,
253+
lastUsed = dateUtilStub.givenDate,
254+
attributes = deviceStore.buildDeviceAttributes() + givenAttributes
255+
)
256+
),
257+
groupStart = QueueTaskGroup.RegisterPushToken(givenDeviceToken),
258+
blockingGroups = listOf(QueueTaskGroup.IdentifyProfile(givenIdentifier))
259+
)
260+
}
261+
224262
// deleteDeviceToken
225263

226264
@Test

sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.junit.Before
1313
import org.junit.Test
1414
import org.junit.runner.RunWith
1515
import org.mockito.kotlin.mock
16+
import org.mockito.kotlin.verify
1617

1718
@RunWith(AndroidJUnit4::class)
1819
class CustomerIOTest : BaseTest() {
@@ -52,4 +53,13 @@ class CustomerIOTest : BaseTest() {
5253
actual.urlHandler.shouldNotBeNull()
5354
actual.autoTrackScreenViews shouldBeEqualTo true
5455
}
56+
57+
@Test
58+
fun deviceAttributes_givenSetValue_expectMakeRequestToAddAttributes() {
59+
val givenAttributes = mapOf(String.random to String.random)
60+
61+
customerIO.deviceAttributes = givenAttributes
62+
63+
verify(apiMock).addCustomDeviceAttributes(givenAttributes)
64+
}
5565
}

0 commit comments

Comments
 (0)