Home » Android » android – Track Admob Event in the Google Analytics

android – Track Admob Event in the Google Analytics

Posted by: admin June 15, 2020 Leave a comment

Questions:

Is it possible to track Admob event that the user clicked on the ads in the Google Analytics .

I use AdMob for showing ads. I want to track every click on ads in Google Analytics.
How can I set up Event?

How to&Answers:

I found the solution.

Implement the AdMob interface AdListener for your Activity.

public interface AdListener {
  public void onReceiveAd(Ad ad);
  public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error);
  public void onPresentScreen(Ad ad);
  public void onDismissScreen(Ad ad);
  public void onLeaveApplication(Ad ad);
}

Then set listener for AdView element.

adView.setAdListener(this);

And override onPresentScreen method for tracking events if user clicks on Ads.

onPresentScreen – Called when an Activity is created in front of your
app, presenting the user with a full-screen ad UI in response to their
touching ad.

private GoogleAnalyticsTracker tracker;
...
@Override
public void onPresentScreen(Ad arg0) {
    tracker.trackEvent(
            "AdMob",    // Category
            "AdView",   // Action
            "Clicked",  // Label
            1);         // Value
}

Answer:

For the new google play services api:

    // from google-play-services.jar
    import com.google.android.gms.ads.*;

    AdListener adListener = new AdListener() {
        @Override
        public void onAdOpened() {
                tracker.trackEvent(
                "AdMob",    // Category
                "AdView",   // Action
                "Clicked",  // Label
                1);  
        }
    };

In new API AdListener is no longer an interface – it is abstract class:

    public abstract class AdListener {
      public void onAdLoaded() {}
      public void onAdFailedToLoad(int errorCode) {}
      public void onAdOpened() {}
      public void onAdClosed() {}
      public void onAdLeftApplication() {}
    }

Answer:

Currently AdMob provides default integration with google analytics. We just need to link analytics account with Admob.

Reference: https://support.google.com/admob/answer/3508177?hl=en-GB