Initialize Android SDK

Best practice: Initialize the Experiences SDK as early as possible when your app loads (for example, in your Application class onCreate). Early initialization improves performance and keeps analytics stable.

This guide outlines how to kickstart your application with the Experiences SDK using an API key. The setup includes initializing the SDK in the App class with optional delegates for global and player entry point configurations.

To initialize the SDK with an API key, add the following code to your Application class.

import com.blaze.blazesdk.BlazeSDK

    override fun onCreate() {
        super.onCreate()
        ...
        BlazeSDK.init(
           apiKey = { API_KEY },
          // Any other optional arguments
          ...
        )
        ...
    }

Optional additional params are here with their default values if not passed to init.

        externalUserId: String? = null,
        cachingSize: Int? = null,
        cachingLevel: BlazeCachingLevel = BlazeCachingLevel.DEFAULT,
        geoLocation: String? = null,
        forceLayoutDirection: BlazeLayoutDirection? = null,
        sdkDelegate: BlazeSDKDelegate? = null,
        playerEntryPointDelegate: BlazePlayerEntryPointDelegate? = null,
        externalUserToken: String? = null,
        restoreExternalUserId: Boolean = true, // starting in v2 (2.0.1). Until v2, this parameter is not in the signature.
        completionBlock: () -> Unit = { },
        errorBlock: (failure: BlazeResult.Error) -> Unit = { }

Initialization errors:

The errorBlock receives a BlazeResult.Error. These errors can be returned, listed by reason and statusCode. Unless noted, the domain is ErrorDomain.INITIALIZATION:

  • SDK_ALREADY_INITIALIZED (1102): init was already called.
  • APPLICATION_NOT_INITIALIZED (1103): the SDK couldn't get your Application instance.
  • INVALID_API_KEY (1100): the API key is blank, or the token request returns 401 or 403.
  • NO_INTERNET_CONNECTION (1105): an initialization request failed and the device has no internet connection.
  • TIME_OUT (1107): the token request timed out.
  • NETWORK_FAILURE (1101): the token request failed with a network error, or an exception was thrown during init.
  • INVALID_APP_CONFIG (1106): the app configuration request failed.
  • INVALID_GEO_RESTRICTION_CODE (1104): the geoLocation value isn't a valid ISO 3166 country code.
  • Any other failure of the token request, like a server error, returns an error with the domain ErrorDomain.GENERAL and no reason or statusCode.
📘

Overview

The externalUserId parameter identifies the viewer when you initialize the SDK. Starting in v2 (2.0.1), the SDK stores that ID and keeps it across app launches until you call logout. Until v2, pass null to setExternalUserId to log a viewer out.

Overriding Behavior
Starting in v2 (2.0.1), if you initialize the SDK with a different externalUserId, the SDK replaces the stored viewer ID. The new externalUserId is used for all subsequent activities and tracking. The previous viewer's unsynced activity is sent to WSC Sports, and their activity is cleared from the device. The previous identifier stays valid: set it again and that viewer's activity is restored.

Restoring the Previous Viewer
Starting in v2 (2.0.1), if you initialize the SDK without an externalUserId, the viewer ID stored at the previous launch is restored. To start a launch with an anonymous viewer instead, pass restoreExternalUserId = false: the stored viewer ID is logged out as part of initialization.

Recommended Practice
Keep the SDK viewer ID aligned with your app's login state. Pass or set the viewer ID when a viewer logs in. Starting in v2 (2.0.1), call logout when they log out. Until v2, pass null to setExternalUserId.


Did this page help you?