Help Center

BlueConic SDK for Android

How to use the BlueConic SDK for Android to develop custom CDP applications for mobile appsThe BlueConic SDK for Android makes it easy for developers to collect profile data from their apps. This section explains how to integrate BlueConic into Android apps. If you are looking for information on iOS apps, see the BlueConic SDK for iOS.

You can use the SDK to do the following:

  • Set and retrieve profile property values
  • Serve (custom) dialogues 
  • Track user behavior using listeners

The BlueConic SDK for Android provides the basis for communication with BlueConic. It provides possibilities to develop your own interactions by creating mobile plugins. Developers can create their own mobile plugins and implement them in their Android app. 


Before you begin

Before you start implementing the SDK, make sure you meet the following requirements:

  • The minimum SDK version supported by the SDK is 21. Make sure that your app is compatible with this.
  • Setup your mobile application in BlueConic and enable it. In order to do that follow the steps described in the article Enabling a mobile app for BlueConic.

Getting started

Note

The latest SDK version is 5.2.1.

Before you start implementing the SDK, perform the following tasks:

  1. Add the SDK dependency under your app build.gradle:
    Groovy Kotlin
    dependencies {    
        implementation 'com.blueconic:blueconic-android-lib:5.2.0'
    }
  2. Add the configuration file for BlueConic:
    Create a file named blueconic_configuration.xml in /res/values/:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <string name="bc_server_url">[BlueConic server url]</string>
    </resources>
    Replace [BlueConic server url] with the protocol and hostname of the BlueConic server. For example "https://example.blueconic.net":
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
      <string name="bc_server_url">https://example.blueconic.net</string>
    </resources>

Using the BlueConic Android SDK

Create a page view event

Typically, you want to create a BlueConic page view event each time the 'onResume' method of an Activity is called. When switching screens without actually switching Activities, you can still call the page view event. Just make sure that a unique screen name is passed in the call. The screen name is shown in the BlueConic Simulator, and it can be used to restrict dialogue interactions to specific screens.

Note

Make sure that you provide the current Activity as argument when invoking "getInstance" on the BlueConicClientFactory. If no Activity is available, pass an empty Activity.

Kotlin Java
// Get the BlueConicClient instance.
val blueConicClient: BlueConicClient = BlueConicClientFactory.getInstance(activity)

// Set screen name to identify the activity.
val properties = mutableMapOf<String, String>().apply {
put("screenName", "Main/Section1")
}

//Trigger the event
blueConicClient.createEvent("PAGEVIEW", properties, activity)

Important

Make sure you trigger a "PAGEVIEW" event in your application before using other functionalities from the SDK.

Triggering a "PAGEVIEW" event ensures that the SDK data collection works correctly.

The "screenName" property sent via a PAGEVIEW event is used by BlueConic to identify the location in the app.

Now the BlueConic user can retrieve the screen name "Main/Section1" as a value like so (using the regular expression (.*)):

Or the BlueConic user can add values when the Mobile Screen name contains certain words. Create a rule of type "URL / Mobile Screen" and configure the Mobile Screen name to listen to:

Once the "PAGEVIEW" event was triggered, the current screen name is saved in the SDK. You can use this value to validate that the SDK is currently registered to the desired screen. To retrieve the value use the following method:

Kotlin Java
val screenName = blueConicClient.screenName

When you first interact with BlueConic using the SDK a profile ID is generated. This profile ID can be used to identify users in BlueConic. In order to retrieve the BlueConic assigned profile id, you can use the following method:

Kotlin Java
val profileId = blueConicClient.profileId

Configure consent

In order to configure the consent information for the BlueConic SDK you can use the following methods:

Kotlin     Java     
// Get the SDK privacy legislation.
val privacyLegislation = blueConicClient.privacyLegislation

// Configure the SDK privacy legislation (e.g. GDPR, CCPA).
blueConicClient.privacyLegislation = "GDPR"

// Add consented objective.
// First, the objectives need to be configured in the BlueConic platform.
blueConicClient.addConsentedObjective("Marketing")

// Add refused objective.
// First, the objectives need to be configured in the BlueConic platform.
blueConicClient.addRefusedObjective("Tracking")

To configure consent information in BlueConic read more about it in the following article: Privacy management in BlueConic.

Clean up BlueConic interactions 

Creating a page view event will dynamically load interactions (dialogues and listeners) in the app. These interactions will be active until the 'onPause' method of the Activity is called, at which point the interactions are destroyed. For apps where page views are called without switching Activities, it is necessary to explicitly call 'destroyPlugins' on the BlueConicClient when the user switches screens.

Kotlin Java
blueConicClient.destroyPlugins()

Create a timeline event

The BlueConic SDK supports the ability to send timeline events using the following dedicated methods:

Kotlin Java
// Set the properties of the timeline event.
val properties = mutableMapOf<String, Any>().apply {
put("propertyKey", "propertyValue")
put("propertyNestedMap", mapOf(
"nestedKey" to "nestedValue",
"nestedKey2" to 123,
"nestedKey3" to true
))
}

// Trigger the timeline event.
// Note that when using this method,
// the ID of the timeline event will be an automatically generated UUID.
blueConicClient.createTimelineEvent("MY_TIMELINE_EVENT", Date(), properties)

//
Trigger the timeline event by ID.
blueConicClient.createTimelineEventById("MY_TIMELINE_ID",
"MY_TIMELINE_EVENT", Date(), properties)

To learn more about timeline events and how to configure them in BlueConic, check out the following article: Overview: Timeline events in BlueConic.

Use custom selectors to track page elements automatically

Some screens of our mobile app contain a view ID that we would like to pick up in BlueConic. As an example, create a new Behavior Listener in BlueConic and add a content rule. Define the value as "Value from Page (custom selector)" with a human readable name, e.g. "Chapter".

Use the ID of a resource, replacing @+id/ or @id/ with a # sign. Only textview or button components will be recognized. For example, use #chapterNumber in BlueConic when your app code defines a textview component like this:

<TextView
    android:id="@+id/chapterNumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1" />

This allows the BlueConic user to define values:

It allows the BlueConic user to define a custom area that should contain words:

The BlueConic user can listen for appearance of words on the mobile screen:

When the mobile screen is refreshed, the BlueConic SDK will retrieve the components present on the page and automatically scan their values for a match. Take into account that you can only detect text from TextView and Button components.

Retrieve and store profile properties

The BlueConic SDK supports the ability to get or set a profile property directly from inside the app. Ask your BlueConic users for the ID of the profile property in BlueConic and use one or more of the following BlueConic methods from the SDK:

Kotlin Java
// Get profile property value
val profileProperty: String? = blueConicClient.getProfileValue("propertyKey")
// Get profile property values (for multi-value properties) val profileProperties: Collection = blueConicClient.getProfileValues("propertyKey") // Set profile property value // Passing a property that was already set with values
// will cause for the old values to be removed. blueConicClient.setProfileValue("propertyKey", "propertyValue")
// Set profile property values (for multi-value properties) // Passing a property that was already set with values
// will cause for the old values to be removed. blueConicClient.setProfileValues("propertyKey", listOf("propertyValue1", "propertyValue2"))
// Add profile property value // If there are already values for a property the new value will be added. blueConicClient.addProfileValue("propertyKey", "propertyValue")
// Add profile property values (for multi-value properties) // If there are already values for a property the new values will be added. blueConicClient.addProfileValues("propertyKey", listOf("propertyValue1", "propertyValue2"))
// Increment profile property value // If there are already values for a property the new value
// will be the sum of all values. blueConicClient.incrementProfileValue("propertyKey", 1.toString())

// Update the profile to sync over the data from the
// Mobile app to the BlueConic servers and;
// also pull in the data that has changed on the BlueConic side.
blueConicClient.updateProfile {
// This callback is called when the update is finished.
// You can now retrieve the new values from the profile.
}

A common use case for setting profile properties directly is to store attributes of the user, especially those that can be used for profile merging across devices. To do this, you would first need to note the ID of the user and email address profile properties you wish to populate (e.g. user_id and email). Then, the setProfileValue method should be used to populate these profile properties accordingly.

Track Events

The BlueConic SDK allows you to send events to BlueConic. This allows you to keep BlueConic informed of what is happening in your app, even when it happens outside the scope of the basic operations.

Events are sent using the BlueConicEventManager's publish() function. Several kinds of events can be published:

ClickEvent
This event signals to BlueConic that a component with a specific ID has been clicked in your app.
UpdateContentEvent
This event signals to BlueConic that the content of a component with a specific ID has updated its content and also passes on the updated content.
UpdateValuesEvent
This event signals to BlueConic that a component with a specific ID has received an updated list of values and passes on these updated values.
FormSubmitEvent
This event signals to BlueConic that the form with a specific ID has been submitted.
AdvancedEvent
This allows you to send a custom event to BlueConic with its own specific name and a list of values.

 

Click event

BlueConic enables users to indicate the button that defines the click area in a Click / Tap rule as follows:

To make this work for your BlueConic users, add the following code to your app:

Kotlin Java
// Define click event for selector "#nextButton"
val event = ClickEvent("#nextButton")
val eventWithContext = ClickEvent("#nextButton", listOf("value"))

// Use EventManager to publish the event to BlueConic
val eventManager = BlueConicEventFactory.getInstance()
eventManager.publish(event)
eventManager.publish(eventWithContext)

Updating content

BlueConic enables you to detect words on the mobile screen:

The BlueConic SDK will scan the mobile screen upon loading the view for the first time. But what if you change the contents of the screen without reloading the view?

To enable this for the BlueConic users at your site, publish an UpdateContentEvent. Add the following code to your app:

Kotlin Java
// Replace with the real new content 
val newContent = "content"
// Define update content event for textview or button component "body" 
val event = UpdateContentEvent(newContent, "#body")

// Use EventManager to publish the event to BlueConic 
val eventManager = BlueConicEventFactory.getInstance()
eventManager.publish(event)

Updating values

BlueConic enables you to create a form listening rule that updates a profile property when a form field changes its value:

"TextView" and "Spinner" widgets will automatically pick up value changes. All other field types have to be facilitated with code within your app.

To make this work for your BlueConic users for non-supported fields, add the following code to your app:

Kotlin Java
// Retrieve this from the actual component instead 
val email = "test@test.com"
// Define update values event for selector "#emailAddress"
val event = UpdateValuesEvent("#emailAddress", listOf(email))

// Use EventManager to publish the event to BlueConic 
val eventManager = BlueConicEventFactory.getInstance()
eventManager.publish(event)

Now, when the user enters an email address in your app, it will be picked up in BlueConic as well.

Form submit

BlueConic enables you to listen to in-app form submits as follows:

To enable this for your BlueConic users, add the following code to your app:

Kotlin Java
// Define form submit event for selector "#loginForm"
val event = FormSubmitEvent("#loginForm")
val eventWithContext = FormSubmitEvent("#loginForm", listOf("value"))

// Use EventManager to publish the event to BlueConic
val eventManager = BlueConicEventFactory.getInstance()
eventManager.publish(event)
eventManager.publish(eventWithContext)

Custom-defined event

BlueConic enables you to set profile property values when a custom-defined event is received. It even allows you to check for specific values in the context of the event:

To enable this feature for your BlueConic users, add the following code to your app:

Kotlin Java
val valueList = mutableListOf().apply {
    add("blueberry")
    add("sushi")§
    add("pancakes")
}

// Define advanced event with the name "video_started"
val event = AdvancedEvent("video_started", valueList)

// Use EventManager to publish the event to BlueConic
val eventManager = BlueConicEventFactory.getInstance()
eventManager.publish(event)

The app publishes an event with the name "video_started" and a list of three values ("blueberry," "sushi," and "pancakes") is added as context. Adding values is optional; it provides extra information to allow BlueConic users at your site to make more specific rules.

On the BlueConic side, the example rule in the screenshot says "... if event video_started is received with a context that contains blueberry, raspberry for any value". The published example event contains one of those values (blueberry) for the event, which means there is a match. As a result, BlueConic will add the value "fruit" to the profile property "Food Preferences" of the profile for this particular app user.

Adding context

Adding context to an event makes it a lot easier to develop and configure which values a mobile developer is able to use and send through the event to BlueConic, where the event context can be used with BlueConic Listeners. Previously, this had to be configured by using a public UI element in order to retrieve data from a mobile app. Now mobile developers can use private or dynamic values and apply them directly as context to an event. This gives you more control and flexibility to publish BlueConic events within an app, and the BlueConic users at your site can use them to define BlueConic Listeners, without having to know about the internal UI IDs of the mobile app.

Mobile developers can develop these events in their code, for example to publish a ClickEvent whenever a customer clicks on their favorite sport (code shown below). In addition, you can define a BlueConic Listener to keep track of a customer's favorite category or sport.

You can add the ClickEvent to a function in your app, for example, by adding a click handler a customer interacts with in the UI or when they navigate to a different page. 

Kotlin Java
val eventWithContext = ClickEvent("#favoriteSport", listOf("soccer"))
  
// Use EventManager to publish the event to BlueConic
val eventManager = BlueConicEventFactory.getInstance()
eventManager.publish(event)
eventManager.publish(eventWithContext)

Once the developer adds the events within the app, BlueConic users at your site can define BlueConic Listeners to keep track of these events and store data to the profile. To create listeners for these events, they just have to know the selectors the developer used in their events (e.g. “#favoriteSport”).

BlueConic users can use the event feature in any of the following BlueConic Listeners:

  • Behavior listener
  • Interest Ranker listener
  • Rule Based Form listener
  • Scoring listener

mobile-listeners-set.png

Inside these listeners, BlueConic users can define the following rules with the event option:

  • Click/tab rule
  • Form Submit rule (only accessible in the Rule-Based Form Listener)
  • Advanced rule

When setting up the BlueConic Listener, under “Expert Settings,” you select the “Value from context of an event” option, as shown here:

behavior-listener.png

Additional functionality

The SDK locale can be configured in the following way:

Kotlin Java
blueConicClient.setLocale("en_US")

To retrieve the segments the current user is currently part of, use the following methods:

Kotlin Java
val segments: MutableList = blueConicClient.segments
val segmentId: String = segments[0].id
val segmentName: String = segments[0].name

Create a Custom Mobile Plugin

BlueConic offers the possibility to build your own custom plugins for mobile, that can be integrated into your application, providing additional functionality. 

To find out more about custom mobile plugins see: Custom Mobile Plugin for Android


Test the SDK integration

Enable debug mode

Add the following node to the configuration file:

<bool name="bc_debug">true</bool>

Note

Setting Debug to true will provide more logging and enable the use of the BlueConic Simulator.

Warning

When you publish your apps, Debug should always be set false.

Example configuration file:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="bc_server_url">https://example.blueconic.net</string>
<bool name="bc_debug">true</bool>
</resources>

Add intent filters to the AndroidManifest.xml for connecting to the Simulator

Configure your appId at the android:scheme attribute in the code fragment below. For example "com.test.testapp". For more information on how to configure a connection to the BlueConic Simulator see: Testing a native mobile app in BlueConic.

<activity android:name=".MainActivity" android:label="@string/app_name">
  <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>
  <!-- Filter added for connecting to the Simulator through mail .-->
  <intent-filter>
    <action android:name="android.intent.action.SEND"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <data android:mimeType="text/plain"/>
  </intent-filter>
  <!-- Filter added for connecting to the Simulator through a QR scan. -->
  <intent-filter>
    <action android:name="android.intent.action.VIEW"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
    <data android:scheme="<Your appId>"/>
  </intent-filter>
</activity>

Note

The use of the BlueConic Simulator is restricted to Debug mode only.

To test your mobile app, you will need access to a BlueConic environment. There are two options:

  • Are you working on behalf of a BlueConic Customer that has a BlueConic environment set up? Ask a representative of the BlueConic Customer to request a Sandbox environment in which you can test your mobile app.
  • Alternatively, request a free Pyxis environment yourself, and test your mobile app with that instance of BlueConic.

For more information on how to configure connecting to the BlueConic simulator see: Testing a native mobile app in BlueConic.


Release Notes

To see a list of updates and bug fixes for the SDK, see the BlueConic SDK for Android Release Notes

Was this article helpful?
0 out of 0 found this helpful