BlueConic SDK for Android

The 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.
  • Gather statistics from mobile devices for users of an app.
  • Register clicks, views and conversions.
  • Use interactions from BlueConic.
  • Use connections from BlueConic.

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. There is also a tutorial on how to create an Android Plugin, see Tutorial: Mobile plugin for Android.

 


Before You Begin

Before you start implementing the SDK, make sure you have the following:

To test your mobile app you will want to have 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? Then 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.

 

 


Getting started

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

    1. Add the file blueconic-lib-<version>.aar to your classpath:
      In Eclipse:
      Copy the file blueconic-lib-<version>.aar to the libs directory of the Android project.
      Right-click "Build Path" and select "Add to Build Path".

      In Android Studio:
      Copy the file blueconic-lib-<version>.aar to the libs directory of the Android project.
      Add a reference to the libs directory in the top-level build.gradle file:
      allprojects {
          repositories {
              mavenCentral()
              flatDir {
                  dirs 'libs'
              }
          }
      }
      
      In the file build.gradle of the module add a dependency to the BlueConic Android SDK and to the gson library:
      dependencies {
        compile 'com.google.code.gson:gson:1.5'
        compile(name: 'blueconic-lib-<version>', ext: 'aar')
      }
      
      Alternatively install the .aar file in a repository and add a dependency in the build.gradle file like compile 'com.blueconic:blueconic-lib:<version>@aar'
    2. Add permissions to the AndroidManifest.xml:
      <uses-permission android:name="android.permission.INTERNET" />
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
      										
    3. 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".
      <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>
      										
    4. 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 "http://example.blueconic.net":
      <?xml version="1.0" encoding="utf-8"?>
      <resources>
        <string name="bc_server_url">http://example.blueconic.net</string>
      </resources>
    5. Optionally, enable debug mode:
      Add the following node to the configuration file
      <bool name="bc_debug">true</bool>

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

 


Using the BlueConic Android SDK

  1. Retrieve an instance of the BlueConicClient in the onResume() of every Activity and create a PAGEVIEW event:
    On the instance call createEvent with a properties map that holds the screenName. The screen name is shown in the Journey Simulator and it can be used to restrict interactions to specific screens. 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.
    import android.app.Activity;
    import android.os.Bundle;
    
    import com.blueconic.BlueConicClient;
    import com.blueconic.BlueConicClientFactory;
    
    public class ExampleActivity extends Activity {
      private BlueConicClient myBlueConicClient;
    
      @Override
      public void onResume() {
        super.onResume();
        // Get the BlueConicClient instance.
        myBlueConicClient = BlueConicClientFactory.getInstance(this);
    	
        // Set screen name to identify the activity.
        final Map<String, String> properties = new HashMap<>();
        properties.put("screenName", "Main/Section1");
        myBlueConicClient.createEvent("PAGEVIEW", properties);
      }
    }
    									
  2. Make sure that you provide the current ViewController as argument when invoking "getInstance" on the BlueConicClient. If no ViewController is available, pass an empty ViewController.
  3. Make sure that you provide the current ViewController as argument when invoking "getInstance" on the BlueConicClient. If no ViewController is available, pass an empty ViewController.
  4. Retrieving and storing profile properties:
    The BlueConic SDK for Android enables you to set and retrieve profile property values for a BlueConic profile. These methods can be used anywhere in the app. The following example counts the number of times a specific activity is opened and stores this number in the BlueConic profile:
    import android.app.Activity;
    import android.os.Bundle;
    
    import com.blueconic.BlueConicClient;
    import com.blueconic.BlueConicClientFactory;
    
    public class ExampleActivity extends Activity {
      // Id of the property to store the number of activities in.
      private final static String PROPERTY_ID = "activityviews";
      private BlueConicClient myBlueConicClient;
    
      @Override
      public void onResume() {
        super.onResume();
    
        // Get an instance of the BlueConicClient.
        myBlueConicClient = BlueConicClientFactory.getInstance(this);
    	
        // Get the current value from the profile, this value is returned as a String.
        final String profileValue = myBlueConicClient.getProfileValue(PROPERTY_ID);
    
        int newValue = 1;
        if (profileValue != null && !"".equals(profileValue)) {
          try {
            // Increase the current value in the profile.
            newValue = Integer.parseInt(profileValue) + 1;
          } catch (NumberFormatException e) {
            // Start counting at 1.
          }
        }
    
        // Set the new value in the profile.
        myBlueConicClient.setProfileValue(PROPERTY_ID, String.valueOf(newValue));
      }
    
      @Override
      public void onPause() {
        super.onPause();
    	
        // This call is only required, when supporting Android API versions below 14.
        myBlueConicClient.destroyPlugins();
      }
    }
    
  5. Register events:
    Calls to register an event can be done in the onLoad of a Plugin. The code fragment shows how to trigger a click event. Possible events are: "CLICK", "VIEW" and "CONVERSION"
    public class ExamplePlugin implements Plugin {
      private BlueConicClient myBlueConicClient;
      private BlueConicClient.InteractionContext myContext;
    
      @Override
      public void init(BlueConicClient client, BlueConicClient.InteractionContext context) {
        myBlueConicClient = client;
        myContext = context;
      }
    
      @Override
      public void onLoad() {
        // Returns the View on the position that is configured at the interaction.
        final View component = myContext.getView();
        final Button button = (Button) component;
    		
        button.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
            final Map<String, String> properties = new HashMap<>();
            properties.put("interactionId", myContext.getInteractionId());
            myBlueConicClient.createEvent("CLICK", properties);
         } 
       });
      }
      
      @Override
      public void onDestroy() {
      }
    }
    

For more information, be sure to check out the following:


Configuring BlueConic

This section describes how to add the channel for your mobile application to your BlueConic universe as well as how to define your custom profile properties so that you can use them in BlueConic segments.

Add the mobile channel to your universe

Before you can start measuring activity in your mobile applications and maintain values in visitor profile properties, the channel for your mobile application must be added to a domain in your BlueConic universe. The very first time that you start your application, BlueConic will detect it. It must then be added as a channel of the type "Mobile App" in a domain. See Managing channels for complete information.

Custom profile properties

If you want your mobile application to be able to add custom properties to visitor profiles and use them in BlueConic segments, those custom profile properties must be added to your plugin. For complete information on writing a custom plugin, see Plugin Types. For information about adding custom profile properties, see profile properties.

See also:

 


BlueConicClientFactory

  • getInstance

    public static BlueConicClient getInstance(android.app.Activity context)
    Get an instance of the BlueConic client.
    Parameters:
    context - The application context.
    Returns:
    The BlueConic client instance.
    Throws:
    NullPointerException - when the context is null.

BlueConicClient

  • addProfileValue

    void addProfileValue(String property,
                       String value)
    Adds a single property value to the profile. If there are already values for a property the new value will be added. Values for a property need to be unique; passing the same value multiple times will have no effect.
     // Add "tennis" to the values for the "hobbies" profile property
     myBlueConicClient.addProfileValue("hobbies", "tennis");
     
    Parameters:
    property - The profile property to add the values for.
    value - The property value to add to the profile.

  • addProfileValues

    void addProfileValues(String property,
                        Collection<String> values)
    Adds property values to the profile. The values from the collection are added to the profile. If there are already values for a property the new values will be added. Values for a property need to be unique; passing the same values multiple times will have no effect.
     // Add "tennis" and "soccer" to the values for the "hobbies" profile property
     myBlueConicClient.addProfileValues("hobbies", Arrays.asList(new String[]{"tennis", "soccer"}));
     
    Parameters:
    property - The profile property to add the values for.
    values - The property values to add to the profile.

  • createEvent

    void createEvent(String eventType,
                   Map<String,String> properties)
    Registers an event of the specified type with the given properties.

     

    For a "PAGEVIEW" event a screen name can be passed, so interactions can be restricted on the where tab in BlueConic. For example:
     properties.put("screenName", "Main/Test");
     myBlueConicClient.createEvent("PAGEVIEW", properties);
     

     

    For a "VIEW", "CLICK" or "CONVERSION" event an interactionId should be passed to register the event for. For example:
     properties.put("interactionId", myContext.getInteractionId());
    myBlueConicClient.createEvent("CLICK", properties);
    Parameters:
    eventType - The event type. e.g {"PAGEVIEW", "VIEW", "CLICK", "CONVERSION"}
    properties - A map with properties for the event.

  • destroyPlugins

    void destroyPlugins()

    Triggers a call to onDestroy on every plugin that was retrieved at the last pageview.

    Note: When it is needed to support Android API versions before API 14, this method must be called in the onPause of every Activity.
    Parameters:
    activity - The activity the plugins where loaded for.

  • getActivity

    android.app.Activity getActivity()
    Returns the current activity.
    Returns:
    the current activity.

  • getProfileValue

    String getProfileValue(String property)
    Returns the first value for a given profile property.
     // Returns the first value in the list of hobbies, e.g. "tennis"
     final String oneHobby = myBlueConicClient.getProfileValue("hobbies");
     
    Parameters:
    property - The profile property to get the values for.
    Returns:
    The first value.

  • getProfileValues

    Collection<String> getProfileValues(String property)
    Returns the values for a given profile property.
     // Returns the all values in the list of hobbies, e.g. {"tennis", "soccer"}
     final Collection<String> allHobbies = myBlueConicClient.getProfileValues("hobbies");
     
    Parameters:
    property - The profile property to get the values for.
    Returns:
    A collection containing the values.

  • getScreenName

    String getScreenName()
    Returns the screenName either set in the createEvent or the Activity's title. E.g.
    Map<String, String> properties = new HashMap<>();
    properties.put("screenName", "MAIN/Activity1");
    String screenName = myBlueConicClient.getScreenName();
    Returns:
    The screen name.

  • getView

    android.view.View getView(String expression)
    Returns a view component based on the given identifier or null is no match is found.
    Parameters:
    expression - the identifier, e.g. '#button1".
    Returns:
    The view or null.

  • setLocale

    void setLocale(String locale)
    Setter for the locale to get the parameters for. By default, the default locale configured in BlueConic is used. Note: the only valid locales currently are 'en_US' and 'nl_NL'.
    Parameters:
    locale - the locale, e.g. 'en_US'.

  • setProfileValue

    void setProfileValue(String property,
                       String value)
    Sets a value on the profile. Passing a property that was already set with values will cause for the old values to be removed.
     // Set "tennis" as the value for the "hobbies" profile property.
     myBlueConicClient.setProfileValue("hobbies", "tennis");
     
    Parameters:
    property - The profile property to add the value for.
    value - The profile value to store.

  • setProfileValues

    void setProfileValues(String property,
                        Collection<String> values)
    Sets values on the profile. Passing a property that was already set with values will cause for the old values to be removed.
     // Set "tennis" and "soccer" as the values for the "hobbies" profile property
     myBlueConicClient.setProfileValues("hobbies", Arrays.asList(new String[]{"tennis", "soccer"}));
     
    Parameters:
    property - The profile property to add the values for.
    values - The profile values to store.

BlueConicClient.InteractionContext

  • getInteractionId

    String getInteractionId()
    Returns the interaction id.
    Returns:
    the interaction id.

  • getParameters

    Map<String,List<String>> getParameters()
    Returns the interaction parameters in a map.
    Returns:
    the parameters.

  • getConnection

    BlueConicClient.Connection getConnection(String id)
    Returns a connected connection.
    Parameters:
    id - The UUID of the connection.
    Returns:
    The connection when found, otherwise null.

  • getView

    android.view.View getView()
    Returns a view component for the interaction.
    Returns:
    The view component matching the selector or the position of the interaction or null if no match is found.

  • getPositionIdentifier

    String getPositionIdentifier()
    Returns the 'selector' of the position.
    Returns:
    the selector, e.g. "#position_1"

BlueConicClient.Plugin

  • onLoad

    void onLoad()
    Invoked when the interaction should execute.
  • onDestroy

    void onDestroy()
    Invoked when the interaction should close.

BlueConicClient.Connection

  • getId

    String getId()
    Returns the connection id.
    Returns:
    The connection id.
  • getParameters

    Map<String,List<String>> getParameters()
    Returns the parameters for the connection. The parameters are returned as a map from parameter ids to the values.
    Returns:
    The parameters.