Kotlin integration

Learn how to integrate and manage Capping in your Kotlin applications.

This page explains how to integrate Capping with the Kotlin SDK.

Install the SDK

Latest version: 1.2 (see all versions). Requires minSdkVersion 26.

Add to your build.gradle file:

implementation "fr.lemonde:capping:1.2"

Your app must declare the android.permission.INTERNET permission.

Initialize the SDK

Initialize the Capping SDK at application launch and use the getInstance() method when you need to access the shared instance later.

Make sure to set the user ID or the tracking will not work.

val capping = Capping.getInstance(applicationContext)
capping.initialize("https://capping.sirius.press", "<api-key>")
capping.setUserId("<user-id>")

You can change the user ID at runtime with the same setUserId(id: String) method.

Start and stop tracking

By default, the SDK does nothing. The component responsible for displaying the Capping UI must call startSession() and stopSession() in order to start and stop the tracking, for example in onResume() and onPause().

override fun onResume() {
    super.onResume()
    Capping.getInstance(this).startSession()
}

override fun onPause() {
    super.onPause()
    Capping.getInstance(this).stopSession()
}

Each startSession() must be matched by a stopSession().

You can either call startSession() when the app is open and stopSession() when it is closed, or you can fine-tune Capping sessions, for example by calling startSession() when an article is open and stopSession() when it is closed.

Display a blocking message

You are responsible for building the UI that blocks content. Attach a listener to be notified of the lock status changes:

import fr.lemonde.capping.internal.CappingLock
import fr.lemonde.capping.internal.CappingLockStatusListener

this.listener = object : CappingLockStatusListener {
   override fun onLockChange(cappingLock: CappingLock) {
       when (cappingLock) {
           is CappingLock.Blocked -> {
               // Block content
           }
           is CappingLock.Unblocked -> {
               // Unblock content
           }
       }
   }
}
this.capping.attachListener(this.listener)

Store the listener in a property, as the SDK does not keep it alive. Don’t forget to call detachListener(this.listener) when you don’t need it anymore or when your UI component is destroyed.

Track events

In order to track events on Android, you can use the trackEvent method.

Available events:

enum class CappingEvent(val nameEvent: String) {
    CONVERSION("conversion"),
    POPIN_DISPLAYED("popinDisplayed")
}

Example with the conversion event:

import fr.lemonde.capping.internal.CappingEvent

this.capping.trackEvent(CappingEvent.CONVERSION)

Send POPIN_DISPLAYED when you display the blocking message.

Deactivate Capping

If you need to disable the Capping service at runtime, you can use setActive(false). By default, the Capping service will be active.

Handle errors

Errors encountered during the Capping operations are only informative and no action is expected. To be notified of them, attach a listener:

import fr.lemonde.capping.internal.CappingOperationFailedListener

this.operationFailedListener = object : CappingOperationFailedListener {
    override fun cappingOperationFailed(localizedDescription: String) {
        // Report the error
    }
}
this.capping.attachOperationFailedListener(this.operationFailedListener)

Store it in a property and call detachOperationFailedListener(this.operationFailedListener) when you don’t need it anymore.

License

This project is licensed under the software license of Société Éditrice Du Monde, Version 1.0 - see the LICENSE.txt file for details.