Android SDK
This page provides a comprehensive guide on the HCL CDP Android SDK to send data from your website to your destinations via HCL CDP.
Overview of the Android SDK?
The HCL CDP Android SDK is designed to track user event data from your Android app. The SDK can be easily imported into any Android app. Based on the data it receives from the user activity, it sends real-time personalized push notifications to the users about the services and products that our clients provide.
Installing and using the HCL CDP Android SDK
Prerequisites
- HCL CDP WriteKey
- HCL CDP Server URL
- Getting the SDKIn your
build.gradlefile, add the following dependency:implementation 'com.hclcdp.app.android:HCLCDP:1.0.0' - Add Google Play Services to your app
Apps in the Google Play Store use the Google Advertising ID to uniquely identify devices. Therefore, to allow the HCL CDP SDK to use the Google Advertising ID, you need to integrate the Google Play Services.
Open your
app/build.gradleand add the below dependency underdependencies:implementation 'com.google.android.gms:play-services-base:17.6.0' implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0' - Some Additional dependencies
Open your
app/build.gradleand add the below dependency underdependencies://No need to add below two firebase dependencies if hcl push notifications are not required. implementation platform('com.google.firebase:firebase-bom:28.2.0') implementation 'com.google.firebase:firebase-messaging' implementation group: 'joda-time', name: 'joda-time', version: '2.10.10' implementation "androidx.lifecycle:lifecycle-extensions:2.2.0" implementation 'com.google.code.gson:gson:2.3.1' implementation 'org.apache.commons:commons-text:1.9' implementation "androidx.work:work-runtime:2.7.1"Manifest File Changes
In the Package Explorer, open theAndroidManifest.xmlfile and make the following changes:<meta-data android:name="HCLCDP.WRITE_KEY" android:value="{WRITE_KEY}" /> <meta-data android:name="HCLCDP.SERVER_URL" android:value="{SERVER_URL}" /> <meta-data android:name="HCLCDP.DEBUG_MODE" android:value="{true/false}" /> <meta-data android:name="HCLCDP.ENABLE_PUSH" android:value="{true/false}" /> <meta-data android:name="HCLCDP.ENABLE_ANALYTICS" android:value="{true/false}" />Replace
WRITE_KEYandSERVER_URLwith original values that you receive from our HCL CDP Account Manager.DEBUG_MODEis used to show HCL CDP debug/error/info logs. Set this tofalsewhen going to production. - Initialize the Android App SDKImport the library for the classes you desire to use
HCLCDPHelperlibrary:import com.hclcdp.app.android.HCLCDPHelper;Add the below code to theonCreatemethod in yourApplicationclass:@Override protected void onCreate(Bundle savedInstanceState) { //your code //Initialize with HCLCDP write key HCLCDPHelper.getInstance(getApplicationContext()).init(); //your code } - Add Proguard RulesIf your application uses Proguard, add following rule in
proguard-rules.proof your application.-keep class androidx.lifecycle.DefaultLifecycleObserver
Android SDK Methods
identify() method
The identify() records user-specific information. In the Android
SDK, we capture deviceID and use that as
anonymousID to identify the user.
Usage:
hclClient.identify(userId, traits, otherIds);
HCLCDPHelper hclClient = HCLCDPHelper hclClient = HCLCDPHelper.getInstance(getApplicationContext());
AttributeBuilder traits = new AttributeBuilder.Builder()
.addAttribute("key1", "value1")
.addAttribute("key2", "value2")
.addAttribute("key3", "value3")
.build();
AttributeBuilder otherIds = new AttributeBuilder.Builder()
.addAttribute("otherId", "otherId_value")
.build();
hclClient.identify("user_id", traits, otherIds);screen() method
The screen() method is used to record the screen details
whenever a user sees any screen on the application.
Usage:
hclClient.screen(screenName, properties, otherIds);
Example:
HCLCDPHelper hclClient = HCLCDPHelper.getInstance(getApplicationContext());
AttributeBuilder properties = new AttributeBuilder.Builder()
.addAttribute("key1", "value1")
.addAttribute("key2", "value2")
.addAttribute("key3", "value3")
.build();
AttributeBuilder otherIds = new AttributeBuilder.Builder()
.addAttribute("otherId", "otherId_value")
.build();
hclClient.screen("MainActivity", properties, otherIds);
Track() method
Every action of the user can be tracked through the track
method. Each of these activity is called an event.
Usage:
hclClient.track(name, properties, otherIds);
Example:
HCLCDPHelper hclClient = HCLCDPHelper.getInstance(getApplicationContext());
AttributeBuilder properties = new AttributeBuilder.Builder()
.addAttribute("product_id", "product_001")
.build();
AttributeBuilder otherIds = new AttributeBuilder.Builder()
.addAttribute("otherId", "otherId_value")
.build();
hclClient.track("Product Added", properties, otherIds);
App Events
| Event Name | Description | Manual Firing |
|---|---|---|
| Application Installed | This event is automatically fired when the user installs an application. | NO |
| Application Opened | This event is automatically fired when the user opens an application. | NO |
| Application Updated | This event is automatically fired when the application gets updated. | NO |
| Application Backgrounded | This event is automatically when a user backgrounds the application. | NO |
| Application Uninstalled | This event should be sent when you receive a user uninstalls the application. | NO |
| Application Session Started | This event is automatically fired when a user spends over 10 seconds on the app. | NO |
| Application Session Concluded | This event is automatically fired when a user is inactive for 20 minutes on the app. | NO |
| Application Crashed | This event should be sent when your application crashes. | YES |
SDK Methods
trackAppEventCrashed() method
The
trackAppEventCrashed() method is used to send crash report
data whenever the application crashes.
Usage
hclClient.trackAppEventCrashed(crashReport)Example
HCLCDPHelper hclClient = HCLCDPHelper.getInstance(getApplicationContext());
String crashReport = "Exception:java.lang.RuntimeException in SampleActivity";
hclClient.trackAppCrashedEvent(crashReport);Push Notifications
HCL CDP sends push notifications to Android devices using Firebase Cloud Messaging.
Add Firebase SDK
- HCL CDP account manager will provide google-services.json. This google-services.json needs to be placed at root level in the app module.
- Gradle changes
Project-level build.gradle (/build.gradle):
buildscript {
dependencies {
// Add this line classpath
'com.google.gms:google-services:4.3.3'
}
}
App-level build.gradle (<project>/<app-module>/build.gradle):
dependencies {
// Add this line compile
'com.google.firebase:firebase-core:17.4.2'
} ...
// Add to the bottom of the fileapply plugin:
'com.google.gms.google-services'
Manifest Changes
In the Package Explorer open the AndroidManifest.xml of your Android project and make the following changes:
<meta-data
android:name="Hcl.NOTIFICATION_ICON"
android:resource="{NOTIFICATION_ICON}" />
<meta-data
android:name="Hcl.NOTIFICATION_ICON_SMALL"
android:resource="{NOTIFICATION_ICON_SMALL}" />-
NOTIFICATION_ICONis displayed at the left of a notification -
NOTIFICATION_ICON_SMALLis displayed in the notification bar. This should be white icon on a transparent background
Refer to Android Notifications and Style Icons for more details.
<service
android:name="com.hclcdp.app.android.push.HCLFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name="com.hclcdp.app.android.HCLFirebaseInstanceIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service><service
android:name=com.hclcdp.app.android.push.HCLIntentService"
android:exported="false"
>
</service>To support app-based deep link from Android 11
and onwards, following permission needs to be added to
AndroidManifest.xml of the
app.
AndroidManifest.xml
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"/>OPT-IN for Push Notifications
HCLCDPHelper hclClient = HCLCDPHelper.getInstance(getApplicationContext());
//you need to provide the Activity in which you want to ask for permission.
hclClient.registerForPushNotifications(MainActivity.this, "title", "message");
//if you don't provide the title and message default values will be given.
hclClient.registerForPushNotifications(MainActivity.this);Running Marketing Channels on WebView
HCLCDPHelper.getInstance(this).setDeviceIdToWebview(webView,domain);| Parameter | Data Type | Description |
|---|---|---|
webView |
WebView instance | Webview instance you want to use |
domain |
String | Domain of the website where you want to run the marketing channels |
Example:
HCLCDPHelper.getInstance(this).setDeviceIdToWebview(myWebsiteWebView,"https://xyz.com");
FAQs
1. What is the Android version required to set up the HCL CDP Android App SDK?
Minimum Android 5.0 (minAppSDKVersion = API level 21)
2. Can HCL CDP Android App SDK be used with Kotlin?
Yes, our Android App SDK can be used with Kotlin as well.
3. Can I use the library with Maven?
No, you can use JitPack to use the SDK.
4. I need to check the logs. Where should I look?
We can filter with the string "HCL CDP" among the application logs.
5. How does the SDK handle different client/server errors?
It prints error messages for both client/server in the logs, but nothing will be visible to the end-user.
6. Which all Android permissions are required for the SDK to run smoothly?
android.permission.INTERNET
7. Does the SDK also support tracking events from wearables and TV that run on Android?
SDK is not yet tested for wearables. We have it in our Product Roadmap.