diff --git a/sdk/analytics/src/main/java/com/klaviyo/analytics/DeviceProperties.kt b/sdk/analytics/src/main/java/com/klaviyo/analytics/DeviceProperties.kt index ab5795799..07a00439b 100644 --- a/sdk/analytics/src/main/java/com/klaviyo/analytics/DeviceProperties.kt +++ b/sdk/analytics/src/main/java/com/klaviyo/analytics/DeviceProperties.kt @@ -71,7 +71,8 @@ internal object DeviceProperties { } val userAgent: String by lazy { - "$applicationLabel/$appVersion ($applicationId; build:$appVersionCode; $platform $osVersion) klaviyo-android/$sdkVersion" + val sdkAgent = "klaviyo-${sdkName.replace("_","-")}" + "$applicationLabel/$appVersion ($applicationId; build:$appVersionCode; $platform $osVersion) $sdkAgent/$sdkVersion" } private val packageInfo: PackageInfo by lazy { diff --git a/sdk/analytics/src/test/java/com/klaviyo/analytics/DevicePropertiesTest.kt b/sdk/analytics/src/test/java/com/klaviyo/analytics/DevicePropertiesTest.kt index 4eb930234..1c475098b 100644 --- a/sdk/analytics/src/test/java/com/klaviyo/analytics/DevicePropertiesTest.kt +++ b/sdk/analytics/src/test/java/com/klaviyo/analytics/DevicePropertiesTest.kt @@ -9,6 +9,7 @@ import io.mockk.mockk import io.mockk.mockkObject import io.mockk.unmockkObject import io.mockk.verify +import org.junit.After import org.junit.Assert.assertEquals import org.junit.Test @@ -52,6 +53,12 @@ internal class DevicePropertiesTest : BaseTest() { versionCode = mockVersionCode } + @After + override fun cleanup() { + unmockDeviceProperties() + super.cleanup() + } + @Test fun `getVersionCodeCompat detects platform properly`() { setFinalStatic(Build.VERSION::class.java.getField("SDK_INT"), 23) @@ -66,4 +73,24 @@ internal class DevicePropertiesTest : BaseTest() { mockPackageInfo.longVersionCode } } + + @Test + fun `User agent reflects SDK name override`() { + mockDeviceProperties() + // Use some more realistic values for this, so the expected user agent string actually matches our regexes + every { DeviceProperties.applicationLabel } returns "MockApp" + every { DeviceProperties.appVersion } returns "1.0.0" + every { DeviceProperties.appVersionCode } returns "2" + every { DeviceProperties.sdkVersion } returns "3.0.0" + every { DeviceProperties.applicationId } returns "com.mock.app" + every { DeviceProperties.platform } returns "Android" + every { DeviceProperties.osVersion } returns "4" + every { DeviceProperties.sdkName } returns "cross_platform" + every { DeviceProperties.userAgent } answers { callOriginal() } + + assertEquals( + "MockApp/1.0.0 (com.mock.app; build:2; Android 4) klaviyo-cross-platform/3.0.0", + DeviceProperties.userAgent + ) + } }