Skip to content

Commit

Permalink
feat: add device and profile attribute setters
Browse files Browse the repository at this point in the history
  • Loading branch information
levibostian authored May 9, 2022
1 parent f90f50f commit 789f09f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 2 deletions.
15 changes: 14 additions & 1 deletion sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface CustomerIOInstance {
val sdkVersion: String
// For security reasons, do not expose the SDK config as anyone can get the API key from the SDK including 3rd parties.

var profileAttributes: CustomAttributes
var deviceAttributes: CustomAttributes

fun identify(identifier: String)

fun identify(
Expand Down Expand Up @@ -310,11 +313,21 @@ class CustomerIO internal constructor(
deviceToken = deviceToken
)

/**
* Use to provide attributes to the currently identified profile.
*
* Note: If there is not a profile identified, this request will be ignored.
*/
override var profileAttributes: CustomAttributes = emptyMap()
set(value) {
api.addCustomProfileAttributes(value)
}

/**
* Use to provide additional and custom device attributes
* apart from the ones the SDK is programmed to send to customer workspace.
*/
var deviceAttributes: CustomAttributes = emptyMap()
override var deviceAttributes: CustomAttributes = emptyMap()
set(value) {
field = value

Expand Down
13 changes: 13 additions & 0 deletions sdk/src/main/java/io/customer/sdk/CustomerIOClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,19 @@ internal class CustomerIOClient(
registerDeviceToken(existingDeviceToken, attributes)
}

override fun addCustomProfileAttributes(attributes: CustomAttributes) {
logger.debug("adding profile attributes request made")

val currentlyIdentifiedProfileId = preferenceRepository.getIdentifier()

if (currentlyIdentifiedProfileId == null) {
logger.debug("no profile is currently identified. ignoring request to add attributes to a profile")
return
}

identify(currentlyIdentifiedProfileId, 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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ internal interface CustomerIOApi {
fun clearIdentify()
fun registerDeviceToken(deviceToken: String, attributes: CustomAttributes)
fun addCustomDeviceAttributes(attributes: CustomAttributes)
fun addCustomProfileAttributes(attributes: CustomAttributes)
fun deleteDeviceToken()
fun trackMetric(deliveryID: String, event: MetricEvent, deviceToken: String)
fun screen(name: String, attributes: CustomAttributes)
Expand Down
26 changes: 25 additions & 1 deletion sdk/src/sharedTest/java/io/customer/sdk/CustomerIOClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ class CustomerIOClientTest : BaseTest() {

customerIOClient.addCustomDeviceAttributes(givenAttributes)

// a token got registered
verify(backgroundQueueMock).queueRegisterDevice(
givenIdentifier,
Device(
Expand All @@ -254,6 +253,31 @@ class CustomerIOClientTest : BaseTest() {
)
}

// addCustomProfileAttributes

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

customerIOClient.addCustomProfileAttributes(givenAttributes)

// do not identify profile
verifyNoInteractions(backgroundQueueMock)
}

@Test
fun addCustomProfileAttributes_givenExistingProfileIdentified_expectAddAttributesToProfile() {
val givenAttributes = mapOf(String.random to String.random)
val givenIdentifier = String.random
prefRepository.saveIdentifier(givenIdentifier)
whenever(backgroundQueueMock.queueIdentifyProfile(anyOrNull(), anyOrNull(), anyOrNull())).thenReturn(QueueModifyResult(true, QueueStatus(siteId, 1)))

customerIOClient.addCustomProfileAttributes(givenAttributes)

// assert that attributes have been added to a profile
verify(backgroundQueueMock).queueIdentifyProfile(givenIdentifier, givenIdentifier, givenAttributes)
}

// deleteDeviceToken

@Test
Expand Down
9 changes: 9 additions & 0 deletions sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,13 @@ class CustomerIOTest : BaseTest() {

verify(apiMock).addCustomDeviceAttributes(givenAttributes)
}

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

customerIO.profileAttributes = givenAttributes

verify(apiMock).addCustomProfileAttributes(givenAttributes)
}
}

0 comments on commit 789f09f

Please sign in to comment.