Android Native
Initialization
To create ease for partners, we have divided the SDK into multiple modules. Each module represents a
specific app niche that partners can use based on their app category.For this step, we will only focus on the core
sdk
To use the SDK, Core is mandatory to include as it contains the elements regarding the basic app navigation and workflow including nudges. Moreover, all the modules require the core to be integrated to successfully log the events.
The easiest way to integrate Causal Foundry Android SDK in your Android project is with Maven Central. Add dependencies of Causal Foundry Android SDK and Android Lifecycle Components in the app/build.gradle file.
dependencies {
// Causal Foundry SDK
implementation 'ai.causalfoundry.android.sdk:core:<version-number>'
//LifecycleComponents
implementation 'androidx.lifecycle:lifecycle-process:2.5.1'
}
For
version-number
please refer to Maven Repository
As SDK requires app events to auto-manage the logging of user sessions you also need to include android lifecycle dependency in your android app/build.gradle file. Lifecycle components are required, if you already have them in your project you may skip them.
Create an Application class in your project and include LifecycleEventObserver Components. Initialize Android SDK with your SDK Token from the onCreate callback of your Application class as shown below.
class MyApplication : Application(), LifecycleEventObserver, NudgeOnClickInterface {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get()
.lifecycle.addObserver(this) // to observe Application lifecycle events
}
/*
In onStateChanged() function include the following function to auto-track events related to the app and activity lifecycle.
*/
override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
CFLog.Builder()
.init(this) // pass in the context
.setSdkKey(getString(R.string.sdk_key))// [OPTIONAL] to set SDK KEY if you don't want to pass SDK key using manifest.
.setLifecycleEvent(event)
.build()
}
override fun onNudgeOpened(ctaType: String, nudgeCallToActionResourceObject: NudgeCallToActionResourceObject) {
Log.d("Notification Opened", "Notification Opened: $ctaType \n $nudgeCallToActionResourceObject")
Toast.makeText(this,
"Notification Opened: $ctaType \n $nudgeCallToActionResourceObject", Toast.LENGTH_LONG).show()
}
}
Causal Foundry Android SDK requires an SDK key to log events. To do so, you need to create an SDK
key from the Causal Foundry Platform and pass it to the SDK using the app's Manifest file
as meta-data
like below:
<meta-data android:name="ai.causalfoundry.android.sdk.APPLICATION_KEY"
android:value="<SDK_KEY_HERE>" />
Causal Foundry Android SDK also allows you to update track events after the user’s usage session is over. SDK sends the events whenever the app goes into the background. This helps in not overloading the user’s phone while in use. SDK handles the events even in offline mode and ensures that they get uploaded to the portal whenever the user connects to the internet. SDK also auto logs the App Open, close, and background events so, you don't have to implement those.
Don't forget to include android:name=".MyApplication"
in your manifest's application tag.
Causal Foundry Android SDK automatically starts tracking user data (e.g., Upload/download speed, OS version, device IDs) and engagement with the basic setup above. This data can be stored on the device and uploaded in batches to reduce network and power usage, and to increase the likelihood of successful uploads while you can also upload the events as soon as they happen. The upload is done when the user device session ends. SDK also starts tracking user sessions with this basic setup.
Each event can further be understood in the context of its attributes which includes details like time, upload/download speed, device details, locale, online/offline usage, screen time, interactions, and so on. This enables you to gain in-depth insights into user interactions across your app. You can also leverage this data to segment users, personalize messages and configure campaign targeting.