Overview
The Nudge Android SDK allows you to integrate the core functionalities provided by Nudge into your Android application. nudgecorev2
is the required package that you would need to integrate to start using Nudge's capabilities.
Installation
Find the detailed instructions to install the latest version of nudgecorev2
:
Add the following to your settings.gradle
according to your package setup:
maven{
url "https://github.com/nudgenow/libraries/raw/main/nudge_android/"
}
maven {
url = uri("https://github.com/nudgenow/libraries/raw/main/nudges/")
}
Once done, add the following to your project build.gradle
plugins {
id("org.jetbrains.kotlin.android") version "1.7.10" apply false
}
plugins {
id("org.jetbrains.kotlin.android") version "1.7.10" apply false
}
Once added, import the nudgecorev2
package module-wide by adding the following to your app's build.gradle
implementation 'com.nudgenow.nudgecorev2:nudgecorev2:1.0.0'
implementation ("com.nudgenow.nudges:nudges:1.0.2")
Handling custom application Class
If you have your custom Application Class extend your application class to NudgeApplicationClass
class YourApplicationClass : NudgeApplicationClass() {
// Your existing implementations
}
class YourApplicationClass extends NudgeApplicationClass {
// Your existing implementations
}
Add the below snippet to your AndroidManifest.xml
file
<application
... rest of your code
tools:replace="android:name">
... rest of your code
</application>
Extend your Activities to NudgeBaseActivity
class YourActivity : NudgeBaseActivity() {}
class YourActivity extends NudgeBaseActivity {}
Incase you do not want to extend your application class, add the below snippet to the onCreate()
of your application class
registerActivityLifecycleCallbacks(NudgeLifecycleCapture())
registerActivityLifecycleCallbacks(NudgeLifecycleCapture())
Usage
Step 1: Usage
- After adding the Nudge package to your project's dependencies in the
build.gradle
file. This will download the - package and its dependencies, allowing you to import and utilize the Nudge package in your code.
import com.nudgenow.sdk.nudgecorev2.core.Nudge;
import com.nudgenow.sdk.nudgecorev2.core.Nudge;
- Initialize the Nudge class with a reusable variable name and access all its functions through it.
Nudge nudge = new Nudge(
apiKey = <YOUR_API_KEY>,
debugMode = true,
getDeviceInfo = true,
getLifecycleInfo = true
);
val nudge: Nudge = Nudge(
apiKey = <YOUR_API_KEY>,
debugMode = true,
getDeviceInfo = true,
getLifecycleInfo = true
)
Note:
apiKey
(required): The secret key obtained from the settings section in the Nudge dashboard, is used for authentication.
Step 2: Initializing user session
- Wherever a distinct user ID that is used to identify users at the client's end is defined, call the
initSession
method to initialize the user session.
nudge.initSession("externalId", properties, new Nudge.InitSessionCallback() {
@Override
public void onSuccess() {
// Handle success
}
@Override
public void onError(Exception e) {
// Handle error
}
});
nudge.initSession(CLIENT_IDENTIFIABLE_USER_ID, null, object : Nudge.InitSessionCallback {
override fun onSuccess() {
// Handle success
}
override fun onError(e: Exception) {
// Handle error
}
})
- You can also send more user attributes along with the user ID for making segments of audiences.
JSONObject userProperties = new JSONObject();
userProperties.put("name","Client User 1");
userProperties.put("age", 27);
userProperties.put("gender", "M");
userProperties.put( "country","US");
nudge.initSession(apiKey: <TOKEN>, externalId:'CLIENT_IDENTIFIABLE_USER_ID',properties: userProperties);
val userProperties = JSONObject().apply {
put("name", "Client User 1")
put("age", 27)
put("gender", "M")
put("country", "US")
}
nudge.initSession(apiKey = "<TOKEN>", externalId = "CLIENT_IDENTIFIABLE_USER_ID", properties = userProperties)
Step 3: Initialize plugins using NudgeProvider
NudgeProvider
- Create a
NudgeProvider
instance. Pass thenudge
instance and a list of plugins for the features that you want to integrate. In place of thecontext
pass your current activity context.
NudgeProvider nudgeProvider = new NudgeProvider(context, nudge, Arrays.asList(/* List of plugins */));
val nudgeProvider = NudgeProvider(context, nudge, listOf(/* List of plugins */))
Step 4: Start Tracking Events
Make sure you have initialized the session before tracking
- To track an event, simply call
Nudge.track()
. Pass the name of the event and its properties that you want to track at the point where the function is called.
Nudge.track("NAME_OF_EVENT", eventProperties, new Nudge.TrackCallback() {
@Override
public void onSuccess(String response) {
// Handle successful tracking
}
@Override
public void onError(Exception e) {
// Handle error in tracking
}
});
Nudge.track(eventType, eventProperties, object : Nudge.TrackCallback {
override fun onSuccess(response: String) {
// Handle successful tracking
}
override fun onError(e: Exception) {
// Handle error in tracking
}
})
- You can also add event properties for analytics and make segments of users based on the properties of their events performed for custom audience experiences.
JSONObject eventProperties = new JSONObject();
eventProperties.put("product", "Fortune Cookies");
eventProperties.put("quantity", 5);
eventProperties.put("gender", "M");
eventProperties.put("countryOfExport","US");
Nudge.track(type:'EVENT_TYPE', eventProperties);
val eventProperties = JSONObject().apply {
put("product", "Fortune Cookies")
put("quantity", 5)
put("gender", "M")
put("countryOfExport", "US")
}
Nudge.track(type = "EVENT_TYPE", properties = eventProperties)
That's it!
Check out the WHAT'S NEXT Section Below to integrate experiences