Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect interstitial ad is clicked by user?

Is there any way to detect when the user taps on the interstitial ads? I tried but did not find any callbacks for detecting interstitial ad clicks.

Any workaround for detecting the same would also be very helpful.

I want to detect ads click to prevent users from generating fake clicks for interstitial ads.

like image 356
Praveen Avatar asked Oct 29 '25 10:10

Praveen


2 Answers

Google added back the removed interstitialAd callback onAdClicked() on SDK 20.4.0. Looks like they removed it and 6 months later realized they messed up and decided to add it back :)

Added the onAdClicked() callback to FullScreenContentCallback.

See the AdMob SDK release notes for details.

I’ve found that upgrading AdMob SDK to the latest version is a must.

like image 60
Jim Avatar answered Oct 31 '25 00:10

Jim


You can utilize the combination of ActivityLifecycleCallbacks and WindowCallback.

  • The ActivityLifecycleCallbacks enables you to observe every Activity lifecycle event that occurs in your app. All in one place.
  • The WindowCallback enables you to intercept many window events. One of the events that gets fired by the system that we are particularly interested in is dispatchTouchEvent.

Now, here is the strategy:

  1. Register our GlobalActivityLifecycleListener in the Application class
class MyApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        registerActivityLifecycleCallbacks(GlobalActivityLifecycleListener())
    }
}
  1. Check if the currently displayed Activity is an Ad Activity. If yes, register our AdWindowCallbacks
class GlobalActivityLifecycleListener : Application.ActivityLifecycleCallbacks {
    //...
    override fun onActivityResumed(activity: Activity) {
        if (isAdActivity(activity)) {
            registerWindowCallbacks(activity)
        }
    }
    
    private fun registerWindowCallbacks(activity: Activity) {
        val currentWindow = activity.window
        /*This is needed to forward the events from our callback back to the original
        callback after we are done with the processing*/
        val originalCallbacks = currentWindow.callback
        currentWindow.callback = AdWindowCallbacks(originalCallbacks)
    }
}
  1. Intercept/process user touch events
class AdWindowCallbacks(private val originalCallback: Window.Callback) : Window.Callback {
    //...
    override fun dispatchTouchEvent(event: MotionEvent?): Boolean {
        //TODO process user touch event
        return originalCallback.dispatchTouchEvent(event)
    }
}

From there, you can detect the common gestures and act accordingly.

like image 23
Muhammad Youssef Avatar answered Oct 30 '25 23:10

Muhammad Youssef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!