Examples

Below is a full example implementation of the Playtime SDK. It uses Jetpack Compose to set up a button that leads the user to the Playtime Offerwall. To run the example:

  1. Add the dependency

  2. Add your SDK hash

  3. If you're using an emulator, add it as a test device.

In the future we will update this page with examples for our other wrappers. Please check back soon.

package com.example.sdk_integration_activity

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import io.adjoe.sdk.Playtime
import io.adjoe.sdk.Playtime.getUserId
import io.adjoe.sdk.PlaytimeException
import io.adjoe.sdk.PlaytimeExtensions
import io.adjoe.sdk.PlaytimeGender
import io.adjoe.sdk.PlaytimeInitialisationListener
import io.adjoe.sdk.PlaytimeNotInitializedException
import io.adjoe.sdk.PlaytimeCatalogListener
import io.adjoe.sdk.PlaytimeParams
import io.adjoe.sdk.PlaytimeUserProfile
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.Calendar
import java.util.Date

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initPlaytime()
        setupPlaytimeCatalogListener()
    }

// the initPlaytime() function has the playtime params, but if your params have
// changed, or if you don't yet have a param, it is recommended that you take a
// different approach. For example, if you don't get the user ID until after the
// user has signed in. You should then send these values in the OnResume.
private fun initPlaytime()
{
    // pass in the user id. These values should come from your backend.
    // If you don't get the userID until after the sign-in, this value can be passed-in onResume.
    val userId = "f0b9d695-405d-4b13-8a79-5fe21dc901e6"

    // set up the parameters, these values should also come from your backend.
    val playtimeParams = PlaytimeParams.Builder()
        .setUaNetwork("tiktok")
        .setUaChannel("video")
        .setUaSubPublisherCleartext("Example: Game 2")
        .setUaSubPublisherEncrypted("8bb1e7911818be32449f6726ff7ecd102ba1862b")
        .setPlacement("Main Screen")
        .build()

    // these values are additional options that you can pass-in to identify users. These data will be sent back in the S2S URL. Example data below.
    val playtimeExtensions = PlaytimeExtensions.Builder()
        .setSubId1("Target Group 1")
        .setSubId2("Target Group 2")
        .build()

    //Set the options
    val options = Playtime.Options()
        .setUserId(userId)
        .setParams(playtimeParams)
        .setUserProfile(getUserProfile())
        .setExtensions(playtimeExtensions)

    // Initialize the adjoe Playtime SDK, passing in the Playtime Options
    try {
        Playtime.init(this, "your_sdk_hash", options, object : PlaytimeInitialisationListener {
            override fun onInitialisationFinished() {
                // The adjoe Playtime SDK was initialized successfully
                //("initialized successfully")
            }

            override fun onInitialisationError(exception: Exception?) {
                // An error occurred while initializing the Playtime SDK.
                // Note that exception might be null
            }
        })
    } catch (e: PlaytimeNotInitializedException) {
        // Handle initialization exception
    }
    setContent {
        ExampleApp(this)
    }
}

//Get the user profile information. These data should come from your backend. 
    fun getSampleUserGender(): String {
        return "male"
    }

    //Pass in the user's birthday information if it's available
    //and if they've consented to sharing it
    fun getSampleUserBirthday(): Date {
        val birthday = Calendar.getInstance()
        birthday.set(Calendar.YEAR, 1995)
        birthday.set(Calendar.MONTH, Calendar.JANUARY)
        birthday.set(Calendar.DAY_OF_MONTH, 30)

        return birthday.time
    }

    val birthday = getSampleUserBirthday()

    fun getUserProfile(): PlaytimeUserProfile {
        val gender: PlaytimeGender = when (getSampleUserGender()) {
            "male" -> PlaytimeGender.MALE
            "female" -> PlaytimeGender.FEMALE
            else -> PlaytimeGender.UNKNOWN
        }
        return PlaytimeUserProfile(gender, birthday)
    }

//Set up the listener for when the catalog is opened or closed. 
    private fun setupPlaytimeCatalogListener() {
        Playtime.setCatalogListener(object : PlaytimeCatalogListener {
            override fun onCatalogOpened(type: String) {
                Log.d("ADJOE", "Catalog of type '" + type + "' was opened")
            }

            override fun onCatalogClosed(type: String) {
                Log.d("ADJOE", "Catalog of type '" + type + "' was closed")
            }
        })
    }

    //Whenever your app comes back into the foreground,
    //you should init the SDK again and send the updated catalog Params.
    override fun onResume() {
        super.onResume()
        initPlaytime()
    }
}

//Set up the params to send with the teaser. Use this in case you have multiple catalog
// placements in your app and want to evaluate the effectiveness of each.
val teaserPlaytimeParams: PlaytimeParams = PlaytimeParams.Builder()
    .setPlacement("Main Screen")
    .build()

//Set up the additional methods
private suspend fun additionalChecks(context: Context) {
    withContext(Dispatchers.IO) {
        getVersion()
        getVersionName()
        val tosAccepted = hasAcceptedTOS(context)
        val permissionsAccepted = hasAcceptedUsagePermission(context)
        val userIdRetreived = getUserId(context)
    }
}
suspend fun getVersion() {
    val version: Int? = Playtime.getVersion()
    Log.d("ADJOE", "Playtime SDK version is $version")
}

suspend fun getVersionName() {
    val versionName: String? = Playtime.getVersionName()
    Log.d("ADJOE", "Playtime SDK version name is $versionName")
}

suspend fun hasAcceptedTOS(context: Context) {
    val accepted: Boolean? = Playtime.hasAcceptedTOS(context)
    Log.d("ADJOE", "TOS accepted: $accepted")
}

suspend fun hasAcceptedUsagePermission(context: Context) {
    val accepted: Boolean? = Playtime.hasAcceptedUsagePermission(context)
    Log.d("ADJOE", "Usage permission accepted: $accepted")
}

suspend fun getUserId(context: Context) {
    val userId: String? = Playtime.getUserId(context)
    Log.d("ADJOE", "User ID is \"$userId\"")
}

// Set up the teaser event function
private fun sendTeaser(context:Context) {
            Playtime.sendUserEvent(context, Playtime.EVENT_TEASER_SHOWN, null, teaserPlaytimeParams)
        }

@Composable
fun ExampleApp(activity: ComponentActivity) {
    Surface(color = MaterialTheme.colorScheme.background) {
        Column(
            modifier = Modifier.fillMaxSize(),
            verticalArrangement = Arrangement.Center,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            ShowPlaytimeCatalogButton(activity)
        }
    }
}

@Composable
fun ShowPlaytimeCatalogButton(activity: ComponentActivity) {
    val coroutineScope = rememberCoroutineScope()

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        // Button to navigate to the catalog screen
        Button(
            onClick = {
                coroutineScope.launch {
                    try {
                        val catalogCatalogIntent = Playtime.getCatalogIntent(activity)
                        activity.startActivity(playtimeCatalogIntent)
                    } catch(notInitializedException: PlaytimeNotInitializedException) {
                        // Handle not initialized exception
                    } catch(exception: PlaytimeException) {
                        // Handle other exceptions
                    }
                    sendTeaser(context = activity.applicationContext) // send the teaser when the user clicks the Catalog Button. The teaser can also be sent when the user views it.
                    additionalChecks(context = activity.applicationContext) // run the additional checks. 
                }
            },
            modifier = Modifier.size(width = 250.dp, height = 75.dp)
        ) {
            Text("Show Playtime Catalog")
        }
    }
}

@Preview(showBackground = true)
@Composable
fun DefaultPreview() {
    ExampleApp(ComponentActivity())
}

Last updated