BlueConic SDK for React Native

The BlueConic SDK for React Native makes it easy for developers to collect profile data from their Android and/or iOS native apps. This section explains how to integrate BlueConic into your React-Native apps. If you are looking for information on Android apps, see the BlueConic SDK for Android. And, if you are looking for information on iOS apps, see the BlueConic SDK for iOS.

You can use the React-Native SDK to do the following:

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

BlueConicClient Framework

The BlueConicClient Framework provides the basis for communication with BlueConic. It provides possibilities to develop your own interactions by creating mobile plugins. BlueConicClient Framework allows you to make use of the standard BlueConic plugins. It contains several plugins that are mobile variants of the standard BlueConic Listeners plugins.  


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, use the following steps to set up your environment:

React-Native

Run the following command in your React Native project's root folder

$ npm install @blueconic/blueconic-react-native

Or include it to your React Native project's package.json 

{
"react": "16.8.6",
"react-native": "0.60.4",
"@blueconic/blueconic-react-native": "^1.0.0"

}

 

iOS implementation of the React Native App

  1. Add the .xcodeproj file of the BlueConicClient library
    1. In Xcode, add the .xcodeproj file of the BlueConicClient library to the Libraries group of the project, you can find the BlueConicClient.xcodeproj in your app's node_modules in 
      node_modules/@blueconic/blueconic-react-native/ios
      To do this, drag and drop the .xcodeproj file to the Libraries group in Xcode
    2. The .xcodeproj will include a Product folder, make sure the libBlueConicClient.a library is added to the Link Binary with Libraries section of the Build Phases of the app's target
    3. The package is dependent on react-native. It searches for the react-native headers both in your project’s node modules/react-native/React directory, as well as in ./ios/Pods/Headers/Public in case react-native is used through CocoaPods. If the react-native headers are saved elsewhere, add the correct path to the Header Search Paths Build Setting of the BlueConicClient target.
  2. Add the configuration key for BlueConic:
    Locate your app's Information Property List file under "Supporting Files".
    Select <project>-Info.plist and add a new row by right clicking the top row and selecting "Add row". Set the following key name:
    bc_server_url
    Double-click the value field and enter the URL for the hostname of your BlueConic server. For example:
    http://example.blueconic.net
  3. Locate your app's Information Property List file under "Supporting Files".
    Select <project>-Info.plist and add a new row by right clicking the top row and selecting "Add row". Set the following key name:
    URL types
    This creates an array which contains more property rows. Open "URL types" by clicking the triangle icon in front of the name to display the rows. Right click "Item 0" and select "Add row". You can also add a new item, and add the new row within that item. Set the following key name:
    URL Schemes
    This creates another array. Add a new row to this array's "Item 0" and enter your apps "Bundle Identifier", for example:
    com.blueconic.testApp
    Full example of the URL Schemes:

  4. Optionally, add a debug key for BlueConic:
    Locate your app's Information Property List file under "Supporting Files".
    Select <project>-Info.plist and add a new row by right clicking the top row and selecting "Add row". Set the following key name:
    bc_debug
    Click the type and select 'Boolean'. Set the value to "YES" if you want to receive debug logs from the BlueConic SDK. Adding a debug key is optional; if you do not want to receive debug logs from the SDK, you do not need to add it.
  5. Import BlueConic:
    The BlueConic SDK for iOS enables you to set and retrieve profile property values for a BlueConic profile and enables interactions specific to this BlueConic profile. These methods can be used anywhere in the app. Make sure that you import the framework and get the instance before using the BlueConicClient methods:
    // Swift:
    import BlueConicClient
    
    let client = BlueConicClient.getInstance(self)
    
    // Objective C:
    #import <BlueConicClient/BlueConicClient-swift.h>
    
    BlueConicClient *client = [BlueConicClient getInstance:self]; 
  6. ** Make sure that you provide the current ViewController as argument when invoking getInstance. If no ViewController is available, pass an empty ViewController.
  7. After you have added the URL Scheme value, select the AppDelegate class and add the following method to enable simulator functionality:

    // Swift: in the AppDelegate.swift file
    func application(_ application: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] ) -> Bool {    BlueConicClient.getInstance(nil).setURL(url)    
        return true
    }
    
    // Objective-C: in the AppDelegate.m file
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
        [[BlueConicClient getInstance:nil] setURL:url];
        return YES;
    }
    

 

Android implementation of the React Native App

  1. Add React Native Package to the MainApplication
    Open android/app/src/main/java/[...]/MainApplication.java

    At the following import to include the package:

    import com.blueconic.blueconicclientreactnative.BlueConcClientPackage;

    And add the BlueConicClientPackage to the list of getPackages()

    @Override
    protected List<ReactPackage> getPackages() {
    List<ReactPackage> packages = new PackageList(this).getPackages();
    // Add BlueConicClientPackage to the package list
    packages.add(new BlueConicClientPackage());
    return packages;
    }
  2. Include the React Native project to the settings.gradle

    Append the following lines to android/settigns.gradle

    include ':blueconic-react-native'
    project(':blueconic-react-native').projectDir = new File(rootProject.projectDir,
    '../node_modules/@blueconic/blueconic-react-native/android')
  3. Insert the following line to inside the dependencies block in android/app/build.gradle
    implementation project('blueconic-react-native')
  4. 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'

  5. Add permissions to the AndroidManifest.xml:
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    										
  6. 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>
    										
  7. 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>
  8. 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 BlueConicClient SDK in React Native apps:

  1. Import NativeModules in the app
    Import the BlueConicClient module within your app
    // React Native: in the Example.js file
    import {
    NativeModules
    } from "react-native";
    const {BlueConicClient} = NativeModules;
  2. Create a page view event each time the user switches screens 
    To keep track of every page viewed in the app, you are able to do so by creating a page view event. Make sure that a unique screen name is passed in the call. The screen name can be used in listener rules or to restrict interactions to specific screens. 
    // React Native: in the Example.js file
    // Simple screen names, like 'main' pages BlueConicClient.registerPageView("main");

    // Unique screen names, e.g. per article
    BlueConicClient.registerPageView("news/article/" + UID);
  3. Retrieving and storing profile properties:
    The BlueConic SDK for React native 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 page has been opened and stores this number in the BlueConic profile:
    // React native: in the ExampleScreen.js file
    import React, { Component } from 'react';
    import {
    Text,
    View,
    NativeModules
    } from 'react-native';
    const {BlueConicClient} = NativeModules;
    const PROPERTY_ID = "pageCount";

    export default class ExampleScreen extends Component {
    constructor() {
    super();
    this.state {
    pageCount: 0
    };
    this.updatePageCount();
    }
    updatePageCount = async() => {
    const getProfileResult = await BlueConicClient.getProfileValue(PROPERTY_ID);
    let pageCountValue = parseInt(getProfileResult) ?? 0;
    BlueConicClient.setProfileValue(PROPERTY_ID, ++pageCountValue);

    this.setState({
    pageCount: pageCountValue
    });
    }
    }
  4. Register events:
    Calls to register an event can be done in the onLoad of a custom developed mobile Plugin. The code fragment shows how to trigger a click event. Possible events are: "VIEW", "CLICK" and "CONVERSION"
    // React native: in the ExamplePlugin.js file

    // Register Dialogue events
    BlueConicClient.registerDialogueEvent("VIEW", this.interactionId);
    BlueConicClient.registerDialogueEvent("CLICK", this.interactionId);
    BlueConicClient.registerDialogueEvent("CONVERSION", this.interactionId);

    // Register events through 'createEvent'
    BlueConicClient.createEvent("VIEW", {
    "interactionId": this.interactionId
    }

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.


BlueConicClient Methods

Implementation of the BlueConic client, handling profile retrieval and storage. This may be from cache, persistent storage on the client or direct requests to the BlueConic server.

import {
NativeModules
} from "react-native";
const {BlueConicClient} = NativeModules;

getProfileId() -> String

Returns the ID of the BlueConic Profile.

Example

// with promise:
const profileId = await BlueConicClient.getProfileId();

// with callback:
BlueConicClient.getProfileIdWithCallback(profileId => {

});

Return Value

The ID of the BlueConic profile

 

getProfileValue(property: String) -> String

Returns the first value for a given profile property.

Example

// with promise:
const hobby = await BlueConicClient.getProfileValue("hobby");

// with callback:
BlueConicClient.getProfileValueWithCallback("hobby", hobby => {

});

Parameters

property

The profile property to get the values for.

Return Value

The first value

getProfileValues(property: String) -> String[] 

Return the values for a given profile property.

Example

// with promise:
const hobbies = await BlueConicClient.getProfileValues("hobbies"); // with callback:
BlueConicClient.getProfileValuesWithCallback("hobbies", hobbies => {

});

Parameters

property

The profile property to get the values for.

Return Value

A collection containing the values.

 

 

addProfileValue(property: String, value: String)

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.

Example

BlueConicClient.addProfileValue("hobbies", "tennis");

Parameters

property

The profile property to add the values for.

value

The property value to add to the profile.

 

addProfileValues(property: String, values: String[]) 

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.

Example

// Swift:
const hobbyArray = ["tennis", "soccer"]
BlueConicClient.addProfileValues("hobbies", ["tennis", "soccer"]);

Parameters

property

The profile property to add the values for.

values

The property values to add to the profile.

 

setProfileValue(name: String, value: String) 

It sets values on the profile. Passing a property that was already set with values will cause the old values to be removed.

Example

BlueConicClient.setProfileValue("hobby", "tennis");

Parameters

property

The profile property to add the values for.

values

The profile values to store.

 

setProfileValues(name: String, values: String[]) 

It sets values on the profile. Passing a property that was already set with values will cause the old values to be removed.

Example

// Swift: 
BlueConicClient.setProfileValues("hobbies", ["tennis", "soccer"]);

Parameters

property

The profile property to add the values for.

values

The profile values to store.

 

setLocale(locale: String)

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'.

Example

BlueConicClient.setLocale("en_US");

Parameters

locale

The locale, e.g.‘en_US’.

 

createEvent(eventType: String, properties: Object)

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 a “VIEW”, “CLICK” or “CONVERSION” event an interactionId should be passed to register the event for.

Example

BlueConicClient.createEvent("PAGEVIEW", {
"screenName": "Main/HOMETAB"
});
BlueConicCLient.createEvent("VIEW", {
"interactionId": this.interactionId
}):

Parameters

eventType

The event type. (e.g:“PAGEVIEW”,“VIEW”,“CLICK”,“CONVERSION”)

properties

A map with properties for the event.

 

Register Methods

Methods to register events and page views

 

registerPageView(screenName: String)

Registers a page event. For a “PAGEVIEW” event a screen name can be passed, so interactions can be restricted on the where tab in BlueConic.

Example

BlueConicClient.registerPageView("Main/HOMETAB");

Parameters

screenName

The screen name for this page view

   

 

registerDialogueEvent(eventType: String, interactionId: String)

Registers a dialogue event of the specified type with the given properties. For a “VIEW”, “CLICK” or “CONVERSION” event an interactionId should be passed to register the event for.

Example

BlueConicCLient.registerDialogueEvent("VIEW", this.interactionId):

Parameters

eventType

The event type. (e.g:“VIEW”,“CLICK”,“CONVERSION”)

properties

A map with properties for the event.

 

Register user event Methods

Methods to register events and page views

registerClickEvent(selector: String)

Register a user click. For the event, a selector can be passed to trigger BlueConic Listeners, to execute their logic.

Example

BlueConicClient.registerClickEvent("#homeButton");

Parameters

selector

The selector, which can be used in the listener rules, to listen on

   

 

registerClickEventWithContext(selector: String, values: String[])

Register a user click. For the event, a selector can be passed to trigger BlueConic Listeners, to execute their logic.

Example

BlueConicClient.registerClickEventWithContext("#homeButton", ["contextA","contextB"]);

Parameters

selector

The selector, which can be used in the listener rules, to listen on

 values context of the click event 

 

registerUpdateValuesEvent(selector: String, values: String[])

Register a user update values event. For the event, a selector can be passed to trigger BlueConic Listeners, to execute their logic.

Example

BlueConicClient.registerUpdateValuesEvent("#updateHobby", ["tennis","soccer"]);

Parameters

selector

The selector, which can be used in the listener rules, to listen on

 values context of the update values event 

 

registerAdvancedEvent(name: String, values: String[])

Register a user update values event. For the event, a selector can be passed to trigger BlueConic Listeners, to execute their logic.

Example

BlueConicClient.registerAdvancedEvent("facebookLike", ["+1"]);

Parameters

name

The name of the advanced even, which can be used in the listener rules, to listen on

 values context of the advanced event