Search code examples
androidadmobads

How to add onAdClick callback in rewarded-ads? - AdMob - Android


I am trying to implement ads in my App. I go though the official documentation and implemented interstitial ads into my app. It is working fine. As Described Here

As I also know that admob will block my admob account if any malicious user clicks on my ads again and again. So to protect that, I had setup a threshold of 3 clicks i.e. If a user try to click on my ads more than 3 times in a single day, I will not show ads for next 48 hours. So, to implement this functionality with interstitial ads I use onAdClick() callback in interstitial ads. As shown here

But, when I try to implement the same thing with rewarded ads, I find that their is no such callback which can help me to detect clicks on rewarded ads. See here.

Can someone help me to achieve this functionality. Is there any alternative ways? Please help me to come out from this scenario. If it is not possible than what I can do to protect my admob account from malicious users?


Solution

  • Method 1

    There is legacy API available for the rewarded ad Check here for more information... Rewarded Video Ads (Legacy API)

    So how it works? Check the code below

    public class MainActivity extends AppCompatActivity {
    
    
        private RewardedVideoAd mRewardedVideoAd;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mRewardedVideoAd= MobileAds.getRewardedVideoAdInstance(this);
    
            mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());     // You need to pass your rewared video ad Id here...
    
            mRewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
                @Override
                public void onRewardedVideoAdLoaded() {
    
                }
    
                @Override
                public void onRewardedVideoAdOpened() {
    
                }
    
                @Override
                public void onRewardedVideoStarted() {
    
                }
    
                @Override
                public void onRewardedVideoAdClosed() {
    
                }
    
                @Override
                public void onRewarded(RewardItem rewardItem) {
                    //Reward your user here....
                    Toast.makeText(MainActivity.this, "Congrats you got reward", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onRewardedVideoAdLeftApplication() {
                    //User clicked on ad here write your caching code here....
                    Toast.makeText(MainActivity.this, "Clicked on Ad", Toast.LENGTH_SHORT).show();
    
                }
    
                @Override
                public void onRewardedVideoAdFailedToLoad(int i) {
    
                }
    
                @Override
                public void onRewardedVideoCompleted() {
    
                }
            });
    
    
    
            findViewById(R.id.my_button).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mRewardedVideoAd.isLoaded()) {
                        mRewardedVideoAd.show();
                    } else {
                        Toast.makeText(MainActivity.this, "Please Wait..", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    
    
        }
    }
    

    Pros Using this method: It does your work you can get callbacks on clicking ad

    Cons Google recommends new RewardedAPI ..(Although legacy API still works fine .... )

    Why does Google recommend new RewardedAPI over Legacy?

    The new rewarded API is an improvement over the legacy API because

    1. it allows you to Cache multiple ads.

    2. The legacy rewarded API only lets you load one ad at a time, and you can't load a second ad until the first one finishes.

    3. The new rewarded API lets you cache multiple ads at the same time. Access the reward value at ad load time. The legacy implementation only provides access to the reward when it's time to grant the user the reward.

    4. For apps that use the reward value from the AdMob UI, this improvement enables you to show the user their reward before they watch the ad. Initialize mediation partners. If you use rewarded mediation, the newly rewarded APIs support initialization of mediation adapters before the first ad load. The legacy rewarded API does not initialize adapters, and mediation adapters were more likely to time out on the first request of a session. Migrating from the legacy rewarded API? Make sure to update your mediation adapters to the latest version, as the adapters had to migrate as well.

    Method 2:

    Since the new Rewarded API doesn't have any callbacks for click events on the ad I recommend you to cache the total number of times the user requested for ads ... limit the number of times they can request that ads

    For example: In 24 hrs they can only request 3 times

    Pros: As listed above Google recommends it for certain reasons Cons: It doesn't solve your problem ...