Quick-start
Getting Started
The following example assumes that a campaign has already been configured beforehand on the platform and that the Android SDK has been successfully installed in your project.
See how to configure a toggle feature campaign on ouruser documentation .
See how to install the Flagship Android SDK in your project:installation.
Step 1 : Initialize the SDK
At the most appropriate location of your Android application, call the start function from the Flagship class, in order to initialize the Flagship SDK:
import com.abtasty.flagship.main.Flagship
import com.abtasty.flagship.main.FlagshipConfig
suspend fun startFlagshipSDK() {
// Start Flagship SDK
Flagship.start(
application,
ENV_ID,
API_KEY,
FlagshipConfig.DecisionApi()
)
}
The SDK starts in DECISION-API. More options are available in the SDK configuration.
Environment ID and API Key
Environment id (ENV_ID) and API key (API_KEY) can be found on your Flagship account, in Parameters > Environment & Security.
Step 2 : Create a visitor
Creating a visitor using the newVisitor function from the Flagship instance allows you to set relevant data for Flagship to make a decision, including: Visitor ID, Visitor Context, GDPR Consent, and Authentication status.
For example, if you want to enable a specific feature for all your VIP visitors, you'll need to add this data as an attribute into the visitor context (key-value pair): isVIP: true
.
Based on your targeting criteria defined in your use-case (isVIP == true
), Flagship will make the decision and show your feature to visitors when they have isVIP: true
in their context.
import com.abtasty.flagship.main.Flagship
import com.abtasty.flagship.main.FlagshipConfig
suspend fun startFlagshipSDK() {
// Start Flagship SDK
Flagship.start(
application,
ENV_ID,
API_KEY,
FlagshipConfig.DecisionApi()
)
// Create visitor with context
val visitor = Flagship.newVisitor("visitor_uuid", true)
.context(hashMapOf("isVIP" to true))
.build()
}
Step 3 : Manage flags
Once a visitor is created with a given context, you have to fetch flags, using the fetchFlags method, to assign campaigns and its flags to this visitor.
When flag fetching completes, you are ready to process visitor's flags and read their values from the returned Flag object based on the desired flag key.
This object includes methods to retrieve the flag value, its metadata, expose the flag, verify the flag's existence, and get the flag status.
import com.abtasty.flagship.main.Flagship
import com.abtasty.flagship.main.FlagshipConfig
suspend fun startFlagshipSDK() {
// Start Flagship SDK
Flagship.start(
application,
ENV_ID,
API_KEY,
FlagshipConfig.DecisionApi()
)
// Create visitor with context
val visitor = Flagship.newVisitor("visitor_uuid", true)
.context(hashMapOf("isVIP" to true))
.build()
// Retrieve visitor assigned Flags according to its context.
visitor.fetchFlags().await()
// Get the current Flag value for 'isVipFeatureEnabled' Flag.
val isVipFeatureEnabled = visitor.getFlag("isVipFeatureEnabled", false).value()
// Process the Flag value in your code
when (isVipFeatureEnabled) {
true -> {
// display your vip feature
}
else -> {
// hide your vip feature
}
}
}
Flag exposure
The SDK assumes that the visitor has been exposed to the flag by default when
flag.getValue()
is called, so it sends the flag exposure hit on Flagship servers. This behavior can be changed and exposure be sent manually, Click here for more details.
Step 4 : Send tracking hit events
Finally, send hits to Flagship using the sendHit method of the visitor instance. These hits help validate your objectives (KPIs) set up in your campaign.
import com.abtasty.flagship.main.Flagship
import com.abtasty.flagship.main.FlagshipConfig
import com.abtasty.flagship.hits.Event
suspend fun startFlagshipSDK() {
// Start Flagship SDK
Flagship.start(
application,
ENV_ID,
API_KEY,
FlagshipConfig.DecisionApi()
)
// Create visitor with context
val visitor = Flagship.newVisitor("visitor_uuid", true)
.context(hashMapOf("isVIP" to true))
.build()
// Retrieve visitor assigned Flags according to its context.
visitor.fetchFlags().await()
// Get the current Flag value for 'isVipFeatureEnabled' Flag.
val isVipFeatureEnabled = visitor.getFlag("isVipFeatureEnabled", false).value()
// Process the Flag value in your code
when (isVipFeatureEnabled) {
true -> {
// display your vip feature
}
else -> {
// hide your vip feature
}
}
// Send tracking event when a visitor click on the vip feature
visitor.sendHit(
Event(Event.EventCategory.ACTION_TRACKING, "VIP_FEATURE_CTA")
.withEventLabel("click_on_vip_feature")
)
}
Step 5 : Reporting
Congrats, you have successfully implemented the Flagship Android SDK.
Check your campaign reporting.
Updated about 2 months ago