Skip to content

Commit

Permalink
chore: updated sample to use local.properties. (#405)
Browse files Browse the repository at this point in the history
feat: added secondary analytics instance.
feat: added buttons for secondary analytics.
  • Loading branch information
itsdebs authored Mar 21, 2024
1 parent faa2dd5 commit 546069b
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 91 deletions.
43 changes: 39 additions & 4 deletions samples/sample-kotlin-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import java.io.FileInputStream
import java.util.Properties

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-android")
}
var sampleRudderProperties = Properties()
if (project.rootProject.file("samples/sample-kotlin-android/local.properties").canRead()) {
sampleRudderProperties.apply {
load(
FileInputStream(
File(
rootProject.rootDir, "samples/sample-kotlin-android/local" + ".properties"
)
)
)
}
}

android {
val majorVersion = 0
Expand All @@ -29,15 +44,36 @@ android {
vectorDrawables {
useSupportLibrary = true
}

buildConfigField(
"String", "WRITE_KEY", sampleRudderProperties.getProperty("writeKey")
)
buildConfigField(
"String", "WRITE_KEY_SECONDARY", sampleRudderProperties.getProperty("writeKeySecondary")
)
buildConfigField(
"String", "CONTROL_PLANE_URL", sampleRudderProperties.getProperty("controlplaneUrl")
)
buildConfigField(
"String", "CONTROL_PLANE_URL_SECONDARY",
sampleRudderProperties.getProperty("controlplaneUrlSecondary")
)
buildConfigField(
"String", "DATA_PLANE_URL",
sampleRudderProperties.getProperty("dataplaneUrl")
)
buildConfigField(
"String", "DATA_PLANE_URL_SECONDARY",
sampleRudderProperties.getProperty("dataplaneUrlSecondary")
)
}

buildTypes {
named("release") {
isMinifyEnabled = false
setProguardFiles(
listOf(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
)
}
Expand All @@ -47,8 +83,7 @@ android {
isMinifyEnabled = false
setProguardFiles(
listOf(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rudderstack.android.sampleapp

import android.app.Application
import android.util.Log
import com.rudderstack.android.sampleapp.analytics.RudderAnalyticsUtils

class MyApplication : Application() {
Expand All @@ -11,6 +12,11 @@ class MyApplication : Application() {
}

private fun initAnalyticSdk() {
RudderAnalyticsUtils.initialize(this)
RudderAnalyticsUtils.initialize(
this
) { instanceName, success, message ->
if (success) Log.d("RudderStack", "Analytics initialized for $instanceName")
else Log.e("RudderStack", "Analytics failed to initialize for $instanceName: $message")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ import com.rudderstack.jacksonrudderadapter.JacksonAdapter

object RudderAnalyticsUtils {

private const val PRIMARY_INSTANCE_NAME = "primary"
private const val SECONDARY_INSTANCE_NAME = "secondary"

private var _rudderAnalytics: Analytics? = null
private var _rudderAnalyticsSecondary: Analytics? = null
private var _rudderReporter: RudderReporter? = null
private var _listener: InitializationListener? = null

fun initialize(application: Application) {
fun initialize(application: Application, listener: InitializationListener? = null) {
_rudderAnalytics = RudderAnalytics(
writeKey = WRITE_KEY,
instanceName = PRIMARY_INSTANCE_NAME,
initializationListener = { success, message ->
_listener?.onAnalyticsInitialized(success, message)
listener?.onAnalyticsInitialized(PRIMARY_INSTANCE_NAME, success, message)
},
configuration = ConfigurationAndroid(
application = application,
Expand All @@ -33,36 +37,51 @@ object RudderAnalyticsUtils {
recordScreenViews = true,
)
)
_rudderAnalyticsSecondary = RudderAnalytics(
writeKey = WRITE_KEY_SECONDARY,
instanceName = SECONDARY_INSTANCE_NAME,
initializationListener = { success, message ->
listener?.onAnalyticsInitialized(SECONDARY_INSTANCE_NAME, success, message)
},
configuration = ConfigurationAndroid(
application = application,
GsonAdapter(),
dataPlaneUrl = DATA_PLANE_URL_SECONDARY,
controlPlaneUrl = CONTROL_PLANE_URL_SECONDARY,
trackLifecycleEvents = true,
recordScreenViews = true,
)
)
_rudderReporter = DefaultRudderReporter(
context = application,
baseUrl = BASE_URL,
configuration = Configuration(
context = application, baseUrl = METRICS_BASE_URL, configuration = Configuration(
LibraryMetadata(
name = "android",
sdkVersion = BuildConfig.LIBRARY_PACKAGE_NAME,
versionCode = BuildConfig.LIBRARY_VERSION_NAME,
writeKey = WRITE_KEY
)
),
JacksonAdapter()
), JacksonAdapter()
)
}

fun getInstance(): Analytics? {
return _rudderAnalytics
}
val primaryAnalytics: Analytics
get() = _rudderAnalytics ?: throw IllegalStateException(
"Rudder Analytics Primary not " + "initialized"
)

val secondaryAnalytics: Analytics
get() = _rudderAnalyticsSecondary ?: throw IllegalStateException(
"Rudder Analytics " + "Secondary" + " not initialized"
)


fun getReporter(): RudderReporter? {
return _rudderReporter
}

fun setInitializationListener(listener: InitializationListener) {
_listener = listener
}

fun interface InitializationListener {

fun onAnalyticsInitialized(success: Boolean, message: String?)
fun onAnalyticsInitialized(instanceName: String, success: Boolean, message: String?)
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.rudderstack.android.sampleapp.analytics

const val WRITE_KEY = "WRITE_KEY"
const val DATA_PLANE_URL = "DATA_PLANE_URL"
const val CONTROL_PLANE_URL = "CONTROL_PLANE_URL"
const val BASE_URL = "BASE_URL"
import com.rudderstack.android.sampleapp.BuildConfig

const val WRITE_KEY = BuildConfig.WRITE_KEY
const val DATA_PLANE_URL = BuildConfig.DATA_PLANE_URL
const val CONTROL_PLANE_URL = BuildConfig.CONTROL_PLANE_URL
const val WRITE_KEY_SECONDARY = BuildConfig.WRITE_KEY_SECONDARY
const val DATA_PLANE_URL_SECONDARY = BuildConfig.DATA_PLANE_URL_SECONDARY
const val CONTROL_PLANE_URL_SECONDARY = BuildConfig.CONTROL_PLANE_URL_SECONDARY
const val METRICS_BASE_URL = "BASE_URL"
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class MainActivity : ComponentActivity() {
userScrollEnabled = true,
modifier = Modifier
.fillMaxWidth()
.padding(vertical = 16.dp)
.padding(vertical = 8.dp)
.weight(1f)
) {
items(logCatList.size, null) { index ->
Expand Down Expand Up @@ -102,48 +102,85 @@ class MainActivity : ComponentActivity() {
.padding(8.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(text = "Primary Analytics", modifier = Modifier.padding(2.dp).align(Alignment
.CenterHorizontally), )
CreateRowOfApis(
names = arrayOf(
AnalyticsState.InitializeAnalytics,
AnalyticsState.ShutDownAnalytics,
AnalyticsState.ClearAnalytics,
), weight = .3f, viewModel = viewModel
AnalyticsState.ForceFlush,
AnalyticsState.OptInAnalytics
), weight = .5f, viewModel = viewModel
)
Spacer(modifier = Modifier.height(8.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.AliasEvent, AnalyticsState.TrackEvent, AnalyticsState.ScreenEvent),
names = arrayOf(AnalyticsState.AliasEvent,
AnalyticsState.TrackEvent,
AnalyticsState.ScreenEvent),
weight = .3f,
viewModel = viewModel
)
Spacer(modifier = Modifier.height(8.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.IdentifyEvent, AnalyticsState.GroupEvent),
weight = .5f,
viewModel = viewModel
)
Spacer(modifier = Modifier.height(8.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.OptInAnalytics, AnalyticsState.ForceFlush),
names = arrayOf(AnalyticsState.StartManualSession, AnalyticsState.EndSession),
weight = .5f,
viewModel = viewModel
)
CreateRowOfApis(
names = arrayOf(AnalyticsState.StartManualSession, AnalyticsState.EndSession),
names = arrayOf(AnalyticsState.EnableAutoTracking, AnalyticsState.DisableAutoTracking),
weight = .5f,
viewModel = viewModel
)
Text(text = "Secondary Analytics", modifier = Modifier.padding(8.dp).align(Alignment
.CenterHorizontally), )
Spacer(modifier = Modifier.height(2.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.EnableAutoTracking, AnalyticsState.DisableAutoTracking),
names = arrayOf(
AnalyticsState.ShutDownAnalyticsSecondary,
AnalyticsState.ForceFlushSecondary,
AnalyticsState.OptInAnalyticsSecondary
), weight = .3f, viewModel = viewModel
)
Spacer(modifier = Modifier.height(2.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.AliasEventSecondary,
AnalyticsState.TrackEventSecondary,
AnalyticsState.ScreenEventSecondary),
weight = .3f,
viewModel = viewModel
)
Spacer(modifier = Modifier.height(2.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.IdentifyEventSecondary, AnalyticsState
.GroupEventSecondary),
weight = .5f,
viewModel = viewModel
)
Spacer(modifier = Modifier.height(2.dp))

CreateRowOfApis(
names = arrayOf(AnalyticsState.StartManualSessionSecondary, AnalyticsState
.EndSessionSecondary),
weight = .5f,
viewModel = viewModel
)
Spacer(modifier = Modifier.height(2.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.EnableAutoTrackingSecondary, AnalyticsState
.DisableAutoTrackingSecondary),
weight = .5f,
viewModel = viewModel
)
Spacer(modifier = Modifier.height(8.dp))
CreateRowOfApis(
names = arrayOf(AnalyticsState.SendError),
names = arrayOf(AnalyticsState.SendError,
AnalyticsState.ClearAnalytics,
),
weight = .5f,
viewModel = viewModel
)
Spacer(modifier = Modifier.height(8.dp))
Spacer(modifier = Modifier.height(2.dp))
CreateLogcat(state.logDataList)
}
}
Expand Down
Loading

0 comments on commit 546069b

Please sign in to comment.