Skip to content

Commit

Permalink
Merge branch 'master' into documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyklaviyo committed Dec 14, 2023
2 parents 1f7e3c5 + deb39b7 commit ce24fca
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 32 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ send them timely push notifications via FCM.
```kotlin
// build.gradle.kts
dependencies {
implementation("com.github.klaviyo.klaviyo-android-sdk:analytics:1.3.4")
implementation("com.github.klaviyo.klaviyo-android-sdk:push-fcm:1.3.4")
implementation("com.github.klaviyo.klaviyo-android-sdk:analytics:1.3.5")
implementation("com.github.klaviyo.klaviyo-android-sdk:push-fcm:1.3.5")
}
```
```groovy
// build.gradle
dependencies {
implementation "com.github.klaviyo.klaviyo-android-sdk:analytics:1.3.4"
implementation "com.github.klaviyo.klaviyo-android-sdk:push-fcm:1.3.4"
implementation "com.github.klaviyo.klaviyo-android-sdk:analytics:1.3.5"
implementation "com.github.klaviyo.klaviyo-android-sdk:push-fcm:1.3.5"
}
```

Expand Down Expand Up @@ -351,4 +351,4 @@ This feature is supported in version 1.3.1 and above of the Klaviyo Android SDK.
[//]: # (REMINDER: Update documentation branch with new dokka docs when updating version number)

Browse complete code documentation autogenerated with
Dokka [here](https://klaviyo.github.io/klaviyo-android-sdk/1.3.4)
Dokka [here](https://klaviyo.github.io/klaviyo-android-sdk/1.3.5)
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,18 @@ internal object KlaviyoApiClient : ApiClient {
initBatch()
}

for (request in requests) {
var addedRequest = false
requests.forEach { request ->
if (!apiQueue.contains(request)) {
apiQueue.offer(request)
Registry.dataStore.store(request.uuid, request.toString())
broadcastApiRequest(request)
addedRequest = true
}
Registry.dataStore.store(request.uuid, request.toString())
broadcastApiRequest(request)
}

persistQueue()
if (addedRequest) {
persistQueue()
}
}

/**
Expand All @@ -135,9 +138,7 @@ internal object KlaviyoApiClient : ApiClient {
* Reset the in-memory queue to the queue from data store
*/
override fun restoreQueue() {
while (apiQueue.isNotEmpty()) {
apiQueue.remove()
}
apiQueue.clear()

// Keep track if there's any errors restoring from persistent store
var wasMutated = false
Expand Down Expand Up @@ -233,7 +234,8 @@ internal object KlaviyoApiClient : ApiClient {
/**
* Stop all jobs on our handler thread
*/
private fun stopBatch() = handler?.removeCallbacksAndMessages(null).also {
private fun stopBatch() {
handler?.removeCallbacksAndMessages(null)
Registry.log.info("Stopped background handler")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.klaviyo.analytics.DeviceProperties
import com.klaviyo.analytics.model.Event
import com.klaviyo.analytics.model.Profile
import com.klaviyo.core.Registry
import org.json.JSONObject

/**
* Defines the content of an API request to track an [Event] for a given [Profile]
Expand Down Expand Up @@ -40,6 +41,17 @@ internal class EventApiRequest(

override val successCodes: IntRange get() = HTTP_ACCEPTED..HTTP_ACCEPTED

override var body: JSONObject? = null
get() {
// Update body to include Device metadata whenever the body is retrieved (typically during sending) so the latest data is included
field?.getJSONObject(DATA)?.getJSONObject(ATTRIBUTES)?.getJSONObject(PROPERTIES)?.apply {
DeviceProperties.buildEventMetaData().forEach { entry ->
put(entry.key, entry.value)
}
}
return field
}

constructor(event: Event, profile: Profile) : this() {
body = jsonMapOf(
DATA to mapOf(
Expand All @@ -54,7 +66,7 @@ internal class EventApiRequest(
),
VALUE to event.value,
TIME to Registry.clock.isoTime(queuedTime),
PROPERTIES to event.toMap() + DeviceProperties.buildEventMetaData(),
PROPERTIES to event.toMap(),
allowEmptyMaps = true
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ internal class ProfileApiRequest(
extract(ProfileKey.ORGANIZATION),
extract(ProfileKey.TITLE),
extract(ProfileKey.IMAGE),

LOCATION to filteredMapOf(
extract(ProfileKey.ADDRESS1),
extract(ProfileKey.ADDRESS2),
Expand All @@ -55,7 +54,6 @@ internal class ProfileApiRequest(
extract(ProfileKey.ZIP),
extract(ProfileKey.TIMEZONE)
),

PROPERTIES to properties // Any remaining custom keys are properties
)
)
Expand All @@ -79,13 +77,6 @@ internal class ProfileApiRequest(
override val successCodes: IntRange get() = HTTP_ACCEPTED..HTTP_ACCEPTED

constructor(profile: Profile) : this() {
// Create a mutable copy of the profile
// We'll pop off all the enumerated keys as we build the body
// Then any remaining pairs are custom keys
val properties = profile.toMap().toMutableMap()
fun extract(key: ProfileKey): Pair<String, Serializable?> =
key.name to properties.remove(key.name)

body = jsonMapOf(*formatBody(profile))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.klaviyo.analytics.networking.requests
import com.klaviyo.analytics.DeviceProperties
import com.klaviyo.analytics.model.Profile
import com.klaviyo.core.Registry
import org.json.JSONObject

/**
* Defines the content of an API request to append a push token to a [Profile]
Expand Down Expand Up @@ -55,20 +56,49 @@ internal class PushTokenApiRequest(

override val successCodes: IntRange get() = HTTP_ACCEPTED..HTTP_ACCEPTED

override var body: JSONObject? = null
get() {
// Update body to include Device metadata whenever the body is retrieved (typically during sending) so the latest data is included
field?.getJSONObject(DATA)?.getJSONObject(ATTRIBUTES)?.apply {
put(
ENABLEMENT_STATUS,
if (DeviceProperties.notificationPermission) NOTIFICATIONS_ENABLED else NOTIFICATIONS_DISABLED
)
put(
BACKGROUND,
if (DeviceProperties.backgroundData) BG_AVAILABLE else BG_UNAVAILABLE
)
put(METADATA, JSONObject(DeviceProperties.buildMetaData()))
}
return field
}

private lateinit var initialBody: String

constructor(token: String, profile: Profile) : this() {
body = jsonMapOf(
DATA to mapOf(
TYPE to PUSH_TOKEN,
ATTRIBUTES to filteredMapOf(
PROFILE to mapOf(*ProfileApiRequest.formatBody(profile)),
TOKEN to token,
PLATFORM to DeviceProperties.platform,
VENDOR to VENDOR_FCM,
ENABLEMENT_STATUS to if (DeviceProperties.notificationPermission) NOTIFICATIONS_ENABLED else NOTIFICATIONS_DISABLED,
BACKGROUND to if (DeviceProperties.backgroundData) BG_AVAILABLE else BG_UNAVAILABLE,
METADATA to DeviceProperties.buildMetaData(),
PROFILE to mapOf(*ProfileApiRequest.formatBody(profile))
VENDOR to VENDOR_FCM
)
)
)
).also {
initialBody = it.toString()
}
}

override fun equals(other: Any?): Boolean {
return when (other) {
is PushTokenApiRequest -> initialBody == other.initialBody
else -> super.equals(other)
}
}

override fun hashCode(): Int {
return initialBody.hashCode()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,21 @@ internal class KlaviyoApiClientTest : BaseRequestTest() {
assertEquals(1, KlaviyoApiClient.getQueueSize())
}

@Test
fun `Enqueuing the same push token API call multiple times only queues the first`() {
assertEquals(0, KlaviyoApiClient.getQueueSize())

repeat(5) {
KlaviyoApiClient.enqueuePushToken(
PUSH_TOKEN,
Profile().setAnonymousId(ANON_ID)
)
}

assertEquals(1, KlaviyoApiClient.getQueueSize())
verify(exactly = 1) { logSpy.info("Persisting queue") }
}

@Test
fun `Enqueues an event API call`() {
assertEquals(0, KlaviyoApiClient.getQueueSize())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ internal class PushTokenApiRequestTest : BaseRequestTest() {
compareJson(requestJson, revivedRequest.toJson())
}

@Test
fun `Requests are equal if the token and profile are equal`() {
val aRequest = PushTokenApiRequest(PUSH_TOKEN, stubProfile)
val bRequest = PushTokenApiRequest(PUSH_TOKEN, stubProfile)
assertEquals(aRequest, bRequest)
}

@Test
fun `Builds body request`() {
val expectJson = """
Expand Down
4 changes: 2 additions & 2 deletions versions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ ext {
targetSDKVersion = 33

// project versioning
versionCode = 6
versionName = '1.3.4' // Reminder: Update version in README samples and update documentation branch
versionCode = 7
versionName = '1.3.5' // Reminder: Update version in README samples and update documentation branch

// dependencies
coreKTXVersion = '1.9.0'
Expand Down

0 comments on commit ce24fca

Please sign in to comment.