Nucleus logo
Android logo

Android Integration

Include JAR in your project and set up SDK

1. Download and install the JAR file in your project. The file can be found inside https://nucleusanalytics.io/settings once you have created an account and logged in
2. Navigate to your Application class and invoke the Nucleus SDK inside the onCreate method

NucleusSdkInitializer.initialize(this)

3. Inside your activity class/classes where the user session is triggered, include:

a) A private variable to hold the NucleusSDK
b) Assign the private variable a value
c) Initialize the SDK itself

See example below

(1) private var nucleusSdk: NucleusSdk? = null
(2) private fun initNucleus() {
       val nucleusSdk = NucleusSdkInitializer.getSdk() ?: kotlin.run {
          Log.e(TAG, "Nucleus is not initialized!")
          // or your logics
          return
      }
    this.nucleusSdk = nucleusSdk
   }

(3) override fun onCreate(savedInstanceState: Bundle{ 
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)// or your implementation

        initNucleus()        

       // execute the following if you plan to use SDK in current screen

        nucleusSdk.initializeWithActivity(this)
    }
Configure and use SDK

1. Before you can use the SDK you have the option to configure it. This can be done inside the scope of the initNucleus method created earlier

See example below

nucleusSdk.configureParametrized(
        locationMode = LocationMode.Explicit, // or your variable
        adIdMode = AdIdMode.Used, // or your variable
        customUserId = "custom user ID", // or your variable
        publicKey = "your key", // or your variable
)

2. In your activity class/classes you should create a private variable for the current pageSessionId (1), include an implementation of session start (2), session stop (3), and call the appropriate methods in the nucleusSDK (4)

See example below

(1) private var pageSessionId: PageSessionId? = null

(2) override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // initializing and your logics here
        startSession()
    }

(3) // ending session in onDestroy directly
    override fun onDestroy() {
        super.onDestroy()
        pageSessionId?.let { nucleusSdk?.endPageSession(it) }
}

(4) // starting session and call NucleusSDK
    private fun startSession() {
        pageSessionId = nucleusSdk.beginPageSession(
            "pageId", // page identifier here
            "referrerId" // or null
        )
    }

3. You can optionally pass one or more content values that will be included in the NucleusSDK. For this, you need to include a setContentInforForNucleus method (1) inside the onCreate method in the activity class/classes. Create a private method to set the content information (2)

See example below

(1) override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // after initializing
        setContentInfoForNucleus()
    }

(2) private fun setContentInfoForNucleus() {
        // in this example set as default values but you
        // can initialize in a way suitable for your needs
        nucleusSdk.contentInfo = ContentInfo(
            author = null,
            cast = null,
            category = null,
            channelName = null,
            crew = null,
            description = null,
            duration = null,
            genre = null,
            id = null,
            publishedYear = null,
            ratings = null,
            seriesEpisode = null,
            seriesName = null,
            title = null,
            type = null
        )
    }