Mars SDK - Native Ad Unit

Prepare the Native Ad Request

The ad request for the Native ad assumes the list of assets that are part of the native ad layout. Only publishers know the exact ad layout and its components. Hence, the app developer should provide a description of the assets by creating the respective objects.

func createNativeParameters() -> MediafyNativeParameters {
    let cta = MediafyNativeAssetData(type: .ctatext)
    cta.length = 15
    
    let title = MediafyNativeAssetTitle(length: 90)
    title.required = true
    
    let icon = MediafyNativeAssetImage(minimumWidth: 50, minimumHeight: 50)
    icon.type = .Icon
    
    let image = MediafyNativeAssetImage()
    image.required = true
    image.width = 1200
    image.height = 627
    image.type = .Main
    
    let description = MediafyNativeAssetData(type: .description)
    description.required = true
    description.length = 150
    
    let rating = MediafyNativeAssetData(type: .rating)
    
    let parameters = MediafyNativeParameters()
    parameters.assets = [cta, title, icon, image, rating, description]
    
    let eventTracker = MediafyNativeEventTracker(
        event: .Impression,
        methods: [.Image, .js]
    )
    
    parameters.eventtrackers = [eventTracker]
    parameters.context = .Social
    parameters.placementType = .FeedContent
    parameters.contextSubType = .Social
    
    return parameters
}

Add Native Ad Unit

To load a native ad, you should initialize and configure MediafyNativeAdUnit object and call the loadAd() method.

private func loadAd() {
    // 1. Create a MediafyNativeAdUnit
    nativeAdUnit = MediafyNativeAdUnit()
    
    // 2. Configure the MediafyNativeAdUnit
    nativeAdUnit.parameters = createNativeParameters()
    
    nativeAdUnit.loadAd { [weak self] ad, error in
        guard let self = self else {
            return
        }
        
        guard let ad = ad, error == nil else {
            return
        }
        
        // 3. Display Native Ad
        createNativeAdView(ad: ad)
    }
}

Apply the received assets to the ad layout

Once the ad is loaded, the SDK provides you with a MediafyNativeAd object in the callback of the loadAd() method. This object contains ad assets you should apply to the native ad layout in your app.

func createNativeAdView(ad: MediafyNativeAd) {
    nativeAd = ad
    nativeAd?.delegate = self
    
    // 3. Render the native ad
    titleLabel.text = ad.title
    bodyLabel.text = ad.text
    sponsoredLabel.text = ad.sponsoredBy
    
    mainImageView.setImage(from: ad.imageUrl, placeholder: UIImage(systemName: "photo.artframe"))
    iconView.setImage(from: ad.iconUrl, placeholder: UIImage(systemName: "photo.artframe"))
    callToActionButton.setTitle(ad.callToAction, for: .normal)
    
    nativeAd?.registerView(view: self.view, clickableViews: [self.callToActionButton])
}

It's important to call the registerView() method. In this case, the SDK will track the impression event properly.

Manage the Ad States

If you need to manage stages of the ad lifecycle you should implement the MediafyNativeAdDelegate protocol.

extension MediafyNativeViewController: MediafyNativeAdDelegate {
    
    func adDidExpire(ad: MediafyNativeAd) {
        // Called when the ad expired
    }
    
    func adWasClicked(ad: MediafyNativeAd) {
        // Called when the ad was clicked
    }
    
    func adDidLogImpression(ad: MediafyNativeAd) {
        // Called when the impression was logged
    }
}

Contact us today

to enjoy endless opportunities!

Contact Us
+

Contact Us