In this article, We are going to implement Analytics class for Google Ads. The idea was to track the ads that display in the app.
We have two types of ads in the app.
- Interstitial ads are full-screen ads that cover the interface of their host app. They’re typically displayed at natural transition points in the flow of an app, such as between activities or during the pause between levels in a game. When an app shows an interstitial ad, the user has the choice to either tap on the ad and continue to its destination or close it and return to the app.
- Banner ads are a rectangular image or text ads that occupy a spot within an app’s layout. They stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you’re new to mobile advertising, they’re a great place to start.
For the best practice, we have implemented separate enums. One for the events and second for the type of events. Following enum adEvent we have added two events as well one method inside the enum which will return the events string.
For instance, if we use the following code
It will return you String “view_ad_banner”
The second enum we are using the adType
Finally, AdAnalyticsModel Class which use above enum adType with the method trackAd
You can track your ads as following
let adAnalyticsModel = AdAnalyticsModel()adAnalyticsModel.trackAd(typeOfAd: .banner, adUnitId: "123")adAnalyticsModel.trackAd(typeOfAd: .interstitial, adUnitId: "456")
adType view_ad_banneradUnitId 456
We can replace the print() with any analytic code i.e Firebase etc.
You can try to run the full program below to see the results.
Thank you for the reading :)