Skip to main content

Add the Ad SDK Dependency

Add the following dependency to your app-level build.gradle file.
implementation("com.twinalyze:ads:1+")
📝 Note
If you’re using the Ad Analytics SDK, you do not need to add the Analytics SDK dependency separately. The Ad SDK already includes all analytics features, events, and insights. However, you must initialize the SDK in your Application class.

Initializing Analytics SDK in Android

Initialize the SDK in your Application class to start tracking sessions, events, and insights.
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()

        MyApp.init(
            this,
            "YOUR_API_KEY",
            "YOUR_ORG_ID",
            false
        )
    }
}

application

Android application context.

apiKey

Your Project API key.

orgId

Your organization ID, for example twinalyze_demo_7356297484.

Demo Account

Email: info@twinalyze.comPassword: 123

Github Demo

Open the Ad SDK Android demo project.

Download APK

Download and test the demo APK.

Demo

Open the Twinalyze demo dashboard.
Best Practice
Always call initialize() inside your Application class, not inside an Activity, to ensure complete session tracking.
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()

        MyApp.init(
            this,
            "YOUR_API_KEY",
            "YOUR_ORG_ID"
        )
    }
}

Initialize Ads in the Launch Activity

Run initAds() in the launch activity, typically SplashActivity, StartActivity, or MainActivity.
initAds()

fun initAds() {
    Ads.init(this, object : AdsInitListner {
        override fun onSuccess() {
            try {
                val data = JSONObject(AppUtil.GetOtherData())
                val showAppOpen = data.optBoolean("isShowAppOpenOnSplash", false)

                if (showAppOpen) {
                    AdsManagerAppOpen.initAd(this@SplashActivity, object : AdsInitListner {
                        override fun onSuccess() {
                            val intent = Intent(this@SplashActivity, StartActivity::class.java)
                            startActivity(intent)
                        }

                        override fun failedOnAdsInit(error: ErrorType?, msg: String?) {
                            val intent = Intent(this@SplashActivity, StartActivity::class.java)
                            startActivity(intent)
                        }
                    })
                } else {
                    val intent = Intent(this@SplashActivity, StartActivity::class.java)
                    startActivity(intent)
                }
            } catch (e: JSONException) {
                e.printStackTrace()

                val intent = Intent(this@SplashActivity, StartActivity::class.java)
                startActivity(intent)
            }
        }

        override fun failedOnAdsInit(error: ErrorType?, msg: String?) {
            if (error == ErrorType.NO_INTERNET) {
                runOnUiThread {
                    Toast.makeText(
                        applicationContext,
                        "checkInternet",
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

            val intent = Intent(this@SplashActivity, StartActivity::class.java)
            startActivity(intent)
        }
    })
}

Add Ad View in XML

Place two ads in one Activity using app:position="top" and app:position="bottom".
<com.twinalyze.ads.View.NativeAdView
    android:id="@+id/nativeBottom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/_10sdp"
    app:position="bottom"
    android:layout_alignParentBottom="true"
    android:orientation="vertical" />