Search code examples
androidandroid-jetpack-composeadsgoogle-ad-manager

Google AdManager viewability measurement on Android and Compose does not work properly


We have the problem with our AdManger viewability measurement that it is only tracked after scrolled out of view and back again.

We brought everything back to a simple ad UI and it still does not work as expected. Does anybody see something wrong in the code?

That’s the code we are using:

    @Composable
    fun AdTestScreen(viewModel: AdTestViewModel = getViewModel()) {
        val adItems by viewModel.adItems.collectAsState(initial = emptyList()) // our ad information comes from the backend
        val context = LocalContext.current
        val webViews = rememberSaveable(adItems) { adItems.map { generateAdManagerAdView(context, it) } }
    
        LazyColumn() {
            items(webViews) { adView ->
                Text(text = "Some Lorem Ipsum text..."
                AndroidView(
                    factory = {
                        adView.removeFromParent()
                        adView
                    }
                )
            }
        }
    }
    
    fun generateAdManagerAdView(context: Context, ad: Ad) =
        AdManagerAdView(context).apply {
            adUnitId = ad.adUnitId
            setAdSizes(ad.adSizes)
            doOnLayout {
                loadAd(AdManagerAdRequest.Builder().apply {
                    // some custom targeting is done
                }.build())
            }
        }

We are using the following versions:

    const val ads = "com.google.android.gms:play-services-ads:21.4.0"
    const val auth = "com.google.android.gms:play-services-auth:20.4.0"
    const val adsIdentifier = "com.google.android.gms:play-services-ads-identifier:18.0.1"
    
    const val kotlinVersion = "1.7.10"
    const val composeVersion = "1.2.1"
    const val composeCompilerVersion = "1.3.0"

Expected behaviour:

When the ad is displayed the measuring should start. For me this is defined as seeing many GMA Debug CONTENT .... "uri":"https://dt.adsafeprotected.com/dt?... requests in the logcat (after you enabled Google Ads Debug Mode https://support.google.com/google-ads/thread/13685676/enable-debug-logging-for-ads?hl=en). As long as the ad is displayed, the measurement calls are sent until a maximum is reached.

Current behaviour:

When the ad is displayed nothing is measured. Even when you scroll up and down (the Ad is always in a visible area), nothing is tracked. BUT: when the ad gets out of visible area and you go back, it starts measuring.


Solution

  • Thanks to @OyaCanli, I found a solution/workaround for this problem: Ad impressions are not count when AdManagerAdView is used with Jetpack Compose :

    I found this workaround proposed in Kotlin slack for NativeAdView in Jetpack Compose, https://kotlinlang.slack.com/archives/CJLTWPH7S/p1664105063510709, and I improved it a bit. Calling adView.rootView.requestLayout() from onAdLoaded solved the issue and I see now onAdImpression is called right after onAdLoaded.

    AdManagerAdView(context).apply {
        adUnitId = ad.id
        setAdSizes(*ad.sizes)
        appEventListener = createAppEventListener()
        adListener = object : AdListener() {
            override fun onAdLoaded() {
                super.onAdLoaded()
                rootView.requestLayout()
            }
        }
        loadAd(bannerAdModule.adRequest(ad))
    }