HomeGuidesAPI ReferenceGraphQL
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Manual Integration

This topic describes how to set up a manual integration between your Android app and Optimizely Data Platform (ODP).

📘

Note

The ODP Android SDK collects and associates push tokens with customers and tracks app opens corresponding to push interactions by sending the necessary events through the ODP API from your app.

Update build.gradle

You need to add the ODP maven repository and add the ODP SDK as a dependency. If you want to receive and display push notifications, you also need to include Google's firebase-messaging package.

repositories {
  maven {
    url  "http://maven.zaius.com"
  }
}

dependencies {
  ...
  implementation 'com.zaius:androidsdk:<version>' // Add the ODP SDK as a dependency
  implementation 'com.google.firebase:firebase-messaging:<version>' // Only required if you want to use mobile push messaging
}

Initialize

Call Zaius.start from your Application implementation’s onCreate method:

public void onCreate() {
  
  String trackerId = "abc123"; // your ODP tracker ID  
  boolean managePushTokens = true; // send push tokens to Zaius
  boolean collectTokensWhenAnonymous = true; // collect push tokens without customer ID set
  int flushIntervalSeconds = 60; // how frequently (in seconds) to send events to ODP
  boolean enableLogging = false; // enable SDK logs for troubleshooting
    
  zaiusConfig = new Zaius.config(managePushTokens, collectTokensWhenAnonymous, flushIntervalSeconds)
    
  Zaius.start(this, trackerId, zaiusConfig);
  
}

Token Collection

To allow ODP to start collecting push tokens, you need to add the following lines to your AndroidManifest.xml file under the <application ...> tag:

<service
    android:name="com.zaius.androidsdk.ZaiusRegistrationIntentService"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:exported="false" />

Optionally, the ODP Android SDK allows you to register a receiver that you can call when ODP retrieves a push token.

To do so, you simply need to implement a class extending ZaiusReceiver:

public class MyReceiver extends ZaiusReceiver {
  @Override
  public void onTokenRegistration(Context context, String token) {
    // Do something with the token (e.g. fire an event)
  }
}

Then inside your Application implementation in onCreate you should register the receiver to handle the action:

@Override
  public void onCreate() {
  // ...
  registerReceiver(
    new MyReceiver(),
    new IntentFilter(ZaiusReceiver.TOKEN_REGISTRATION_ACTION)
  );
  // ...
}

Setup Push

After you have sorted out initialization and token collection, the next step is to set up the actual Push Notifications. To do so, you need to extend the FirebaseMessagingService class, override the onMessageReceived method, and do the majority of the logic for displaying notifications from there.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sendNotification(remoteMessageToBundle(remoteMessage));
}

/**
 * Convert an FCM RemoteMessage into a Bundle
 *
 * @param remoteMessage message containing data to be translated
 * @return a Bundle containing Strings for each key in the data of the RemoteMessage
 */
private Bundle remoteMessageToBundle(RemoteMessage remoteMessage) {
    Bundle bundle = new Bundle();
    Map<String, String> data = remoteMessage.getData();
    for (String key : data.keySet()) {
        bundle.putString(key, data.get(key));
    }
    return bundle;
}

/**
 * Create and show a simple notification containing the received push data.
 *
 * @param data a Bundle of push data
 */
private void sendNotification(Bundle data) {
    Intent intent = new Intent(this, MyActivity.class); // Open this activity when the notification is clicked
    intent.putExtras(data); // Put the bundle of data from the message in here
    PendingIntent pendingIntent = PendingIntent.getActivity(this, MY_REQUEST_CODE, intent, PendingIntent.FLAG_ONE_SHOT);
    // ... etc, rest of notification code ...
}

For ODP to properly record and attribute open events, you need to pass along the data in the RemoteMessage available in FirebaseMessagingService.onMessageReceived as extras in the Intent corresponding the push open itself, as shown above.

To make sure the actual open events are tracked, you need to call Zaius.pushOpened from the response activity to the above notification display. An example of an activity is shown below:

public class PushActivity extends Activity {
    // ...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // ...
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            try {
                Zaius.getInstance().pushOpened(bundle);
            } catch (ZaiusException e) {
                e.printStackTrace();
            }
        }
        // ...
    }
    // ...
}

It is absolutely essential that you call Zaius.getInstance().pushOpened(bundle) as shown above and with the extra information passed from the bundle to make sure that we can track open events accurately.

Zaius.pushOpened is required to track push opens

❗️

Warning

`Zaius.pushOpened is required to track push opens.

Zaius.pushOpenedaccepts either a FirebaseRemoteMessageor an AndroidBundle` corresponding to the notification so that metadata associated with the push can be linked to the open event for analytics and reporting in ODP(for example, the ODP campaign that sent the notification).

📘

Note

Pass along the Bundle to Zaius.pushOpened in the YourActivity.onCreate method.


Next