Skip to content

Commit

Permalink
Merge pull request #466 from rudderlabs/feature/sdk-2156-small-refact…
Browse files Browse the repository at this point in the history
…or-in-plugins-setup-analytics-method

refactor: in plugins setup analytics method
  • Loading branch information
ChryssaAliferi authored Jul 19, 2024
2 parents 547585d + 2c1ca71 commit 5784365
Show file tree
Hide file tree
Showing 44 changed files with 546 additions and 601 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
/*
* Creator: Debanjan Chatterjee on 20/11/23, 3:48 pm Last modified: 20/11/23, 3:48 pm
* Copyright: All rights reserved Ⓒ 2023 http://rudderstack.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.rudderstack.android.internal.infrastructure

import android.app.Activity
Expand All @@ -24,13 +10,19 @@ import com.rudderstack.core.InfrastructurePlugin
import java.util.concurrent.atomic.AtomicInteger

/** Tracks the Activities in the application and broadcasts the same */
internal class ActivityBroadcasterPlugin(
) : InfrastructurePlugin {
internal class ActivityBroadcasterPlugin : InfrastructurePlugin {

override lateinit var analytics: Analytics
private val application: Application?
get() = analytics?.currentConfigurationAndroid?.application
private var analytics: Analytics? = null
get() = analytics.currentConfigurationAndroid?.application

private val activityCount = AtomicInteger()

override fun setup(analytics: Analytics) {
super.setup(analytics)
application?.registerActivityLifecycleCallbacks(lifecycleCallback)
}

private val lifecycleCallback by lazy {
object : Application.ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Expand All @@ -53,7 +45,7 @@ internal class ActivityBroadcasterPlugin(
}

override fun onActivityStopped(activity: Activity) {
if (analytics?.currentConfigurationAndroid?.trackLifecycleEvents == true) {
if (analytics.currentConfigurationAndroid?.trackLifecycleEvents == true) {
decrementActivityCount()
if (activityCount.get() == 0) {
broadCastApplicationStop()
Expand All @@ -72,13 +64,13 @@ internal class ActivityBroadcasterPlugin(
}

private fun broadcastActivityStart(activity: Activity) {
analytics?.applyInfrastructureClosure {
analytics.applyInfrastructureClosure {
if (this is LifecycleListenerPlugin) {
setCurrentActivity(activity)
onActivityStarted(activity.localClassName)
}
}
analytics?.applyMessageClosure {
analytics.applyMessageClosure {
if (this is LifecycleListenerPlugin) {
setCurrentActivity(activity)
onActivityStarted(activity.localClassName)
Expand All @@ -96,26 +88,26 @@ internal class ActivityBroadcasterPlugin(


private fun broadCastApplicationStart() {
analytics?.applyInfrastructureClosure {
analytics.applyInfrastructureClosure {
if (this is LifecycleListenerPlugin) {
this.onAppForegrounded()
}
}
analytics?.applyMessageClosure {
analytics.applyMessageClosure {
if (this is LifecycleListenerPlugin) {
this.onAppForegrounded()
}
}
}

private fun broadCastApplicationStop() {
analytics?.applyInfrastructureClosure {
analytics.applyInfrastructureClosure {
if (this is LifecycleListenerPlugin) {
setCurrentActivity(null)
this.onAppBackgrounded()
}
}
analytics?.applyMessageClosure {
analytics.applyMessageClosure {

if (this is LifecycleListenerPlugin) {
setCurrentActivity(null)
Expand All @@ -124,11 +116,6 @@ internal class ActivityBroadcasterPlugin(
}
}

override fun setup(analytics: Analytics) {
this.analytics = analytics
application?.registerActivityLifecycleCallbacks(lifecycleCallback)
}

override fun shutdown() {
application?.unregisterActivityLifecycleCallbacks(lifecycleCallback)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
/*
* Creator: Debanjan Chatterjee on 29/11/23, 4:58 pm Last modified: 29/11/23, 4:58 pm
* Copyright: All rights reserved Ⓒ 2023 http://rudderstack.com
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain a
* copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.rudderstack.android.internal.infrastructure

import com.rudderstack.android.AndroidUtils
Expand All @@ -22,26 +8,30 @@ import com.rudderstack.core.Configuration
import com.rudderstack.core.DataUploadService
import com.rudderstack.core.InfrastructurePlugin

internal class AnonymousIdHeaderPlugin : InfrastructurePlugin{
internal class AnonymousIdHeaderPlugin : InfrastructurePlugin {

override lateinit var analytics: Analytics
private var dataUploadService: DataUploadService? = null
private var _analytics: Analytics? = null

override fun setup(analytics: Analytics) {
_analytics = analytics
super.setup(analytics)
dataUploadService = analytics.dataUploadService
}

override fun shutdown() {
dataUploadService = null
_analytics = null
}

override fun updateConfiguration(configuration: Configuration) {
if(configuration !is ConfigurationAndroid) return
val anonId = configuration.anonymousId?: AndroidUtils.generateAnonymousId(configuration.collectDeviceId, configuration.application).also {
_analytics?.applyConfigurationAndroid {
copy(anonymousId = it)
}
if (configuration !is ConfigurationAndroid) return
val anonId = configuration.anonymousId ?: AndroidUtils.generateAnonymousId(
configuration.collectDeviceId,
configuration.application
).also {
analytics.applyConfigurationAndroid {
copy(anonymousId = it)
}
}
dataUploadService?.addHeaders(mapOf("Anonymous-Id" to anonId))
dataUploadService?.addHeaders(mapOf("Anonymous-Id" to anonId))
}

override fun shutdown() {
dataUploadService = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.rudderstack.android.internal.infrastructure

import android.content.pm.PackageManager
import android.os.Build
import com.rudderstack.android.storage.AndroidStorage
import com.rudderstack.android.utilities.androidStorage
import com.rudderstack.android.utilities.currentConfigurationAndroid
import com.rudderstack.android.storage.AndroidStorage
import com.rudderstack.core.models.AppVersion
import com.rudderstack.core.Analytics
import com.rudderstack.core.InfrastructurePlugin
import com.rudderstack.core.Plugin
import com.rudderstack.core.models.AppVersion
import com.rudderstack.core.models.Message

private const val PREVIOUS_VERSION = "previous_version"
Expand All @@ -31,18 +31,18 @@ private const val DEFAULT_VERSION_NAME = ""
* * */
class AppInstallUpdateTrackerPlugin : Plugin {

private var analytics: Analytics? = null
override lateinit var analytics: Analytics
private lateinit var appVersion: AppVersion
override fun intercept(chain: Plugin.Chain): Message {
// no change made to message
return chain.proceed(chain.message())
}

override fun setup(analytics: Analytics) {
this.analytics = analytics
super.setup(analytics)
this.appVersion = getAppVersion(analytics)
storeVersionNameAndBuild(analytics.androidStorage)
if (this.analytics?.currentConfigurationAndroid?.trackLifecycleEvents == true) {
if (this.analytics.currentConfigurationAndroid?.trackLifecycleEvents == true) {
trackApplicationStatus()
}
}
Expand Down Expand Up @@ -100,7 +100,7 @@ class AppInstallUpdateTrackerPlugin : Plugin {
}

private fun sendApplicationInstalledEvent() {
this.analytics?.logger?.debug(log = "Tracking Application Installed event")
this.analytics.logger.debug(log = "Tracking Application Installed event")
val trackProperties = mutableMapOf<String, Any>()
trackProperties[VERSION] = this.appVersion.currentVersionName
trackProperties[BUILD] = this.appVersion.currentBuild
Expand All @@ -109,26 +109,21 @@ class AppInstallUpdateTrackerPlugin : Plugin {
}

private fun sendApplicationUpdatedEvent() {
this.analytics?.logger?.debug(log = "Tracking Application Updated event")
this.analytics.logger.debug(log = "Tracking Application Updated event")
val trackProperties = mutableMapOf<String, Any>()
trackProperties[PREVIOUS_VERSION] = this.appVersion.previousVersionName
trackProperties[PREVIOUS_BUILD] = this.appVersion.previousBuild
trackProperties[VERSION] = this.appVersion.currentVersionName
trackProperties[BUILD] = this.appVersion.currentBuild

sendEvent(EVENT_NAME_APPLICATION_UPDATED, trackProperties)
}

private fun sendEvent(eventName: String, properties: Map<String, Any>) {
analytics?.track {
analytics.track {
event(eventName)
trackProperties {
add(properties)
}
}
}

override fun onShutDown() {
analytics = null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,34 @@ private const val AUTOMATIC = "automatic"
*
* @property currentMillisGenerator
*/
internal class LifecycleObserverPlugin(val currentMillisGenerator: (() -> Long) = { SystemClock.uptimeMillis() }) :
Plugin, LifecycleListenerPlugin {
internal class LifecycleObserverPlugin(
val currentMillisGenerator: (() -> Long) = { SystemClock.uptimeMillis() }
) : Plugin, LifecycleListenerPlugin {

override lateinit var analytics: Analytics

private var _isFirstLaunch = AtomicBoolean(true)

private var _lastSuccessfulDownloadTimeInMillis = AtomicLong(-1L)
private var analytics: Analytics? = null
private var currentActivityName: String? = null
private val listener = ConfigDownloadService.Listener {
if (it) {
_lastSuccessfulDownloadTimeInMillis.set(currentMillisGenerator())
}
}

override fun setup(analytics: Analytics) {
super.setup(analytics)
analytics.applyInfrastructureClosure {
if (this is ConfigDownloadService) {
addListener(listener, 1)
}
}
}

private fun sendLifecycleStart() {
withTrackLifeCycle {
analytics?.also { analytics ->
analytics.also { analytics ->
analytics.track {
event(EVENT_NAME_APPLICATION_OPENED)
trackProperties {
Expand All @@ -58,7 +69,7 @@ internal class LifecycleObserverPlugin(val currentMillisGenerator: (() -> Long)

private fun sendLifecycleStop() {
withTrackLifeCycle {
analytics?.track {
analytics.track {
event(EVENT_NAME_APPLICATION_STOPPED)
}
}
Expand All @@ -68,23 +79,13 @@ internal class LifecycleObserverPlugin(val currentMillisGenerator: (() -> Long)
return chain.proceed(chain.message())
}

override fun setup(analytics: Analytics) {
this.analytics = analytics
analytics.applyInfrastructureClosure {
if (this is ConfigDownloadService) {
addListener(listener, 1)
}
}
}

override fun onShutDown() {
_lastSuccessfulDownloadTimeInMillis.set(0)
analytics?.applyInfrastructureClosure {
analytics.applyInfrastructureClosure {
if (this is ConfigDownloadService) {
removeListener(listener)
}
}
analytics = null
}

override fun updateConfiguration(configuration: Configuration) {
Expand All @@ -97,22 +98,22 @@ internal class LifecycleObserverPlugin(val currentMillisGenerator: (() -> Long)
}

private fun checkAndDownloadSourceConfig() {
analytics?.takeIf { it.currentConfiguration?.shouldVerifySdk == true }?.apply {
analytics.takeIf { it.currentConfiguration?.shouldVerifySdk == true }?.apply {
if (currentMillisGenerator() - (_lastSuccessfulDownloadTimeInMillis.get()) >= MAX_CONFIG_DOWNLOAD_INTERVAL) {
analytics?.updateSourceConfig()
analytics.updateSourceConfig()
}
}
}

override fun onAppBackgrounded() {
sendLifecycleStop()
analytics?.flush()
analytics.flush()
}

override fun onActivityStarted(activityName: String) {
currentActivityName = activityName
withRecordScreenViews {
analytics?.screen {
analytics.screen {
screenName(activityName)
screenProperties {
add(AUTOMATIC to true)
Expand All @@ -128,7 +129,7 @@ internal class LifecycleObserverPlugin(val currentMillisGenerator: (() -> Long)
override fun onScreenChange(name: String, arguments: Map<String, Any>?) {
val activityName = currentActivityName ?: ""
withRecordScreenViews {
analytics?.screen {
analytics.screen {
screenName(activityName)
this.category(name)
this.screenProperties {
Expand All @@ -139,13 +140,13 @@ internal class LifecycleObserverPlugin(val currentMillisGenerator: (() -> Long)
}

private fun withTrackLifeCycle(body: () -> Unit) {
if (analytics?.currentConfigurationAndroid?.trackLifecycleEvents == true) {
if (analytics.currentConfigurationAndroid?.trackLifecycleEvents == true) {
body()
}
}

private fun withRecordScreenViews(body: () -> Unit) {
if (analytics?.currentConfigurationAndroid?.recordScreenViews == true) {
if (analytics.currentConfigurationAndroid?.recordScreenViews == true) {
body()
}
}
Expand Down
Loading

0 comments on commit 5784365

Please sign in to comment.