Skip to content

Commit

Permalink
feat: rename amplitude-core to analytics-connector (#13)
Browse files Browse the repository at this point in the history
* fix: rename core to analytcs-connector

* rename core classes to connector

* connector v1.0.0
  • Loading branch information
bgiori authored Feb 2, 2022
1 parent 95addb3 commit da7a11c
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 86 deletions.
File renamed without changes.
6 changes: 3 additions & 3 deletions core/build.gradle → analytics-connector/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ plugins {
}

ext {
PUBLISH_NAME = 'Amplitude SDK Core'
PUBLISH_DESCRIPTION = 'Core package for Amplitide SDKs'
PUBLISH_NAME = 'Amplitude Analytics SDK Connector'
PUBLISH_DESCRIPTION = 'Connector for seamlessly integrating with the Amplitude Analytics SDK'
PUBLISH_GROUP_ID = 'com.amplitude'
PUBLISH_VERSION = '1.0.0'
PUBLISH_ARTIFACT_ID = 'amplitude-core'
PUBLISH_ARTIFACT_ID = 'analytics-connector'
}

apply from: "${rootDir}/gradle/publish-module.gradle"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.amplitude.core">
package="com.amplitude.analytics">

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.amplitude.analytics.connector

class AnalyticsConnector private constructor() {

companion object {

private val instancesLock = Any()
private val instances = mutableMapOf<String, AnalyticsConnector>()

@JvmStatic
fun getInstance(instanceName: String): AnalyticsConnector {
return synchronized(instancesLock) {
instances.getOrPut(instanceName) {
AnalyticsConnector()
}
}
}
}

val identityStore: IdentityStore = IdentityStoreImpl()
val eventBridge: EventBridge = EventBridgeImpl()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.amplitude.core
package com.amplitude.analytics.connector

import java.util.concurrent.ArrayBlockingQueue

Expand All @@ -10,13 +10,13 @@ data class AnalyticsEvent(
val userProperties: Map<String, Map<String, Any>>? = null,
)

interface AnalyticsConnector {
interface EventBridge {

fun logEvent(event: AnalyticsEvent)
fun setEventReceiver(receiver: AnalyticsEventReceiver?)
}

internal class AnalyticsConnectorImpl : AnalyticsConnector {
internal class EventBridgeImpl : EventBridge {

private val lock = Any()
private var receiver: AnalyticsEventReceiver? = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.amplitude.core
package com.amplitude.analytics.connector

import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@file:JvmName("JSONUtil")

package com.amplitude.core.util
package com.amplitude.analytics.connector.util

import org.json.JSONArray
import org.json.JSONObject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.amplitude.core
package com.amplitude.analytics.connector

import com.amplitude.analytics.connector.AnalyticsEvent
import com.amplitude.analytics.connector.EventBridgeImpl
import org.junit.Assert
import org.junit.Test

class AnalyticsConnectorTest {
class EventBridgeTest {

@Test
fun `test addEventListener, logEvent, listener called`() {
val testEvent = AnalyticsEvent("test")
val analyticsConnector = AnalyticsConnectorImpl()
val analyticsConnector = EventBridgeImpl()
analyticsConnector.setEventReceiver {
Assert.assertEquals(testEvent, it)
}
Expand All @@ -20,7 +22,7 @@ class AnalyticsConnectorTest {
val testEvent0 = AnalyticsEvent("test0")
val testEvent1 = AnalyticsEvent("test1")
val testEvent2 = AnalyticsEvent("test2")
val analyticsConnector = AnalyticsConnectorImpl()
val analyticsConnector = EventBridgeImpl()
analyticsConnector.logEvent(testEvent0)
analyticsConnector.logEvent(testEvent1)
var eventCount = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.amplitude.core
package com.amplitude.analytics.connector

import com.amplitude.api.Identify
import com.amplitude.core.util.toUpdateUserPropertiesMap
import com.amplitude.analytics.connector.util.toUpdateUserPropertiesMap
import org.json.JSONArray
import org.json.JSONObject
import org.junit.Assert
Expand Down Expand Up @@ -85,11 +85,13 @@ class IdentityStoreTest {
@Test
fun `test updateUserProperties, unset`() {
val identityStore = IdentityStoreImpl()
identityStore.setIdentity(Identity(userProperties = mapOf(
identityStore.setIdentity(
Identity(userProperties = mapOf(
"key" to "value",
"other" to true,
"final" to 4.2
)))
))
)
identityStore.editIdentity()
.updateUserProperties(
mapOf("\$unset" to mapOf("other" to "-", "final" to "-"))
Expand All @@ -104,12 +106,14 @@ class IdentityStoreTest {
@Test
fun `test updateUserProperties, clearAll`() {
val identityStore = IdentityStoreImpl()
identityStore.setIdentity(Identity(userProperties = mapOf(
identityStore.setIdentity(
Identity(userProperties = mapOf(
"key" to listOf(-3, -2, -1, 0),
"key2" to mapOf("wow" to "cool"),
"key3" to 3,
"key4" to false,
)))
))
)
identityStore.editIdentity()
.updateUserProperties(
mapOf("\$clearAll" to mapOf())
Expand Down
22 changes: 0 additions & 22 deletions core/src/main/java/com/amplitude/core/AmplitudeCore.kt

This file was deleted.

5 changes: 3 additions & 2 deletions sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ android {
}

dependencies {
implementation 'com.amplitude:amplitude-core:1.0.0'
implementation 'com.amplitude:android-sdk:2.26.1'
implementation 'com.amplitude:analytics-connector:1.0.0'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'

implementation 'com.amplitude:android-sdk:2.26.1'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.json:json:20201115'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package com.amplitude.experiment

import com.amplitude.core.AnalyticsConnector
import com.amplitude.core.AnalyticsEvent
import com.amplitude.analytics.connector.EventBridge
import com.amplitude.analytics.connector.AnalyticsEvent
import com.amplitude.experiment.analytics.ExperimentAnalyticsEvent
import com.amplitude.experiment.analytics.ExperimentAnalyticsProvider

internal class CoreAnalyticsProvider(
private val analyticsConnector: AnalyticsConnector
internal class ConnectorAnalyticsProvider(
private val eventBridge: EventBridge
): ExperimentAnalyticsProvider {

override fun track(event: ExperimentAnalyticsEvent) {
val eventProperties: Map<String, String> = event.properties
.filterValues { it != null }
.mapValues { it.value!! }
analyticsConnector.logEvent(
eventBridge.logEvent(
AnalyticsEvent(
eventType = event.name,
eventProperties = eventProperties
Expand All @@ -23,7 +23,7 @@ internal class CoreAnalyticsProvider(

override fun setUserProperty(event: ExperimentAnalyticsEvent) {
val variant = event.variant.value ?: return
analyticsConnector.logEvent(
eventBridge.logEvent(
AnalyticsEvent(
"\$identify",
null,
Expand All @@ -37,7 +37,7 @@ internal class CoreAnalyticsProvider(
}

override fun unsetUserProperty(event: ExperimentAnalyticsEvent) {
analyticsConnector.logEvent(
eventBridge.logEvent(
AnalyticsEvent(
"\$identify",
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.amplitude.experiment

import android.content.Context
import com.amplitude.core.Identity
import com.amplitude.core.IdentityStore
import com.amplitude.analytics.connector.Identity
import com.amplitude.analytics.connector.IdentityStore
import com.amplitude.experiment.util.Lock
import com.amplitude.experiment.util.LockResult
import java.util.concurrent.TimeoutException
import kotlin.jvm.Throws

internal class CoreUserProvider(
internal class ConnectorUserProvider(
context: Context,
private val identityStore: IdentityStore,
): ExperimentUserProvider {
Expand Down Expand Up @@ -66,3 +66,7 @@ private fun IdentityStore.getIdentityOrWait(ms: Long): Identity {
removeIdentityListener(callback)
return result
}

private fun Identity.isUnidentified(): Boolean {
return deviceId.isNullOrBlank() && userId.isNullOrBlank()
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ internal class DefaultExperimentClient internal constructor(
@Throws(IllegalStateException::class)
private fun getUserMergedWithProviderOrWait(ms: Long): ExperimentUser {
val safeUserProvider = userProvider
val providedUser = if (safeUserProvider is CoreUserProvider) {
val providedUser = if (safeUserProvider is ConnectorUserProvider) {
try {
safeUserProvider.getUserOrWait(ms)
} catch (e: TimeoutException) {
Expand Down
10 changes: 5 additions & 5 deletions sdk/src/main/java/com/amplitude/experiment/Experiment.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.amplitude.experiment

import android.app.Application
import com.amplitude.core.AmplitudeCore
import com.amplitude.analytics.connector.AnalyticsConnector
import com.amplitude.experiment.storage.SharedPrefsStorage
import com.amplitude.experiment.util.AndroidLogger
import com.amplitude.experiment.util.Logger
Expand Down Expand Up @@ -82,16 +82,16 @@ object Experiment {
): ExperimentClient = synchronized(instances) {
val instanceName = config.instanceName
val instanceKey = "$instanceName.$apiKey"
val core = AmplitudeCore.getInstance(instanceName)
val connector = AnalyticsConnector.getInstance(instanceName)
val instance = when (val instance = instances[instanceKey]) {
null -> {
Logger.implementation = AndroidLogger(config.debug)
val configBuilder = config.copyToBuilder()
if (config.userProvider == null) {
configBuilder.userProvider(CoreUserProvider(application, core.identityStore))
configBuilder.userProvider(ConnectorUserProvider(application, connector.identityStore))
}
if (config.analyticsProvider == null) {
configBuilder.analyticsProvider(CoreAnalyticsProvider(core.analyticsConnector))
configBuilder.analyticsProvider(ConnectorAnalyticsProvider(connector.eventBridge))
}
val newInstance = DefaultExperimentClient(
apiKey,
Expand All @@ -102,7 +102,7 @@ object Experiment {
)
instances[instanceKey] = newInstance
if (config.automaticFetchOnAmplitudeIdentityChange) {
core.identityStore.addIdentityListener {
connector.identityStore.addIdentityListener {
newInstance.fetch()
}
}
Expand Down
Loading

0 comments on commit da7a11c

Please sign in to comment.