The integration of Mars SDK with Google AdMob assumes that the publisher already has an AdMob account and has previously integrated the Google Mobile Ads SDK (GMA SDK) into their application.
See the Google Integration Documentation for the details of the AdMob integration.
Add custom repository.
repositories { maven { setUrl("https://sdk-cdn.mediafy.io/android/mediafy-sdk") } }
Add Gradle imports.
implementation("com.mediafy:mediafy-sdk:2.4.4") implementation("com.mediafy:mediafy-sdk-google-adapters:2.4.4")
Mediafy.initializeSdk(context, accountSettings)
Parameters:
context
accountSettings
MobileAds.initialize(context)
Banner and Interstitial formats don't require any additional effort. The mediation engine of GMA SDK will manage everything according to the Custom Network and Custom events setup. See the AdOps Guide for the details.
Creating AdMob native ad with Mars adapter
// 1. Create native ad config val nativeConfig = createNativeConfig() val extrasBundle = nativeConfig.toGmaBundle() // 2. Create ad request with MediafyGadMediationAdapter and native ad config val adRequest = AdRequest.Builder() .addNetworkExtrasBundle(MediafyGadMediationAdapter::class.java, extrasBundle) .build() // 3. Configure ad loader val adLoader = AdLoader.Builder(this, adMobAdUnitId) .forNativeAd { ad: NativeAd -> // 5. Render ad createCustomView(ad) } .withAdListener(createListener()) .withNativeAdOptions(NativeAdOptions.Builder().build()) .build() // 4. Load ad adLoader.loadAd(adRequest)
Create MediafyNativeAdConfig with required assets, native event trackers, context and placement types. In the example we request: CTA text, title, icon, image, rating, and description. After creating convert it to Bundle for AdMob.
MediafyNativeAdConfig
private fun createNativeConfig(): MediafyNativeAdConfig { val ctaText = NativeDataAsset(NativeDataAsset.DataType.CTATEXT) ctaText.len = 15 val title = NativeTitleAsset(90) title.isRequired = true val icon = NativeImageAsset() icon.wMin = 50 icon.hMin = 50 icon.imageType = NativeImageAsset.ImageType.ICON val image = NativeImageAsset() image.w = 1200 image.h = 627 image.imageType = NativeImageAsset.ImageType.MAIN image.isRequired = true val rating = NativeDataAsset(NativeDataAsset.DataType.RATING) val description = NativeDataAsset(NativeDataAsset.DataType.DESC) description.isRequired = true description.len = 150 val assets = listOf(ctaText, title, icon, image, rating, description) val methods: ArrayList<EventTrackingMethod> = ArrayList() methods.add(EventTrackingMethod.IMAGE) methods.add(EventTrackingMethod.JS) val tracker = NativeEventTracker(NativeEventTracker.EventType.IMPRESSION, methods) val config = MediafyNativeAdConfig() config.setContextType(NativeContextType.SOCIAL_CENTRIC) config.setPlacementType(NativePlacementType.CONTENT_FEED) config.setContextSubType(NativeContextSubtype.GENERAL_SOCIAL) config.setNativeEventTrackers(listOf(tracker)) config.setNativeAssets(assets) return config }
Create AdMob AdRequest with MediafyGadMediationAdapter to activate the adapter.
AdRequest
MediafyGadMediationAdapter
Create AdLoader with adMobAdUnitId.
AdLoader
adMobAdUnitId
Load ad.
Add native ad to you UI.
private fun createCustomView(ad: NativeAd) { val binding = ViewNativeAdAdMobBinding.inflate(LayoutInflater.from(this)) binding.tvHeadline.setText(ad.headline) binding.tvBody.setText(ad.body) val icon = ad.icon if (icon != null) { binding.imgIco.setImageDrawable(icon.drawable) } if (!ad.images.isEmpty()) { binding.imgMedia.setMediaContent(ad.mediaContent) } val container: NativeAdView = binding.viewNativeWrapper container.headlineView = binding.tvHeadline container.bodyView = binding.tvBody container.iconView = binding.imgIco container.mediaView = binding.imgMedia container.setNativeAd(ad) containerForAd.addView(binding.getRoot()) }
You can create and set custom ad events listener.
private fun createListener(): AdListener { return object : AdListener() { override fun onAdLoaded() { Log.d(TAG, "Ad loaded successfully") } override fun onAdFailedToLoad(loadAdError: LoadAdError) { Log.e(TAG, "Ad failed to load: $loadAdError") } } }