Get Started with Avocarrot

Overview

Examples

Android

Overview

- Avocarrot Exchange SDK

Setup Exchange SDK

Stream

Native Assets

Exchange Troubleshooting

- Avocarrot Mediation SDK

Setup Mediation

Stream Ads Mediation

Native ads Mediation

Native Assets Mediation

Banner Mediation

Interstitial Mediation

Video Mediation

Supported Networks & Ad Types

Mediation Troubleshooting

iOS

Overview

- Avocarrot Exchange SDK

Setup SDK

Stream

Native Assets

Exchange Troubleshooting

- Avocarrot Mediation

Getting Started With Mediation

Stream Ads Mediation

Native Ads Mediation

Banner Ads Mediation

Interstitial Ads Mediation

Video Ads Mediation

Supported Networks & Ad Types

Mediation Troubleshooting

API Documentation

Reporting API

Help

FAQ

Updates

SDK Change Log

Android Change Log

iOS Change Log

Native Assets Mediation

Updated 7 days ago ​by Chris

Overview

This tutorial helps you setup native assets mediation.

Prerequisites

You need an Avocarrot account to complete the tutorial. Create an account
You need to setup the Mediation SDK. Setup Mediation SDK

Setup Native Assets Ads

Our native assets ad is a raw ad data without any pre-defined wrapping UI. The layout for the native assets ad is configured and rendered by developers.

Use Native type in Avocarrot UI to configure Native Assets Ad networks. Currently, the Native Assets do not supports client mediation networks such as AdMob, Facebook, MoPub, etc.
  1. Use the common algorithm to implement a Native Assets Ad: 

    a. Create your own subclass of View which will be used as ad view container.

    b. Initialize NativeAssetsAd by NativeAssetsAdPool.load(context, "YOUR_NATIVE_AD_UNIT_ID", nativeAssetsAdListener); call to start loading of assets

    c. Get NativeAssets in onAdLoaded(@NonNull NativeAssetsAd nativeAssetsAd, @NonNull NativeAssets nativeAssets) callback of nativeAssetsAdListener

    d. Render the Ad view container

    e. Register the Ad views for impressions and clicks:
    • call nativeAssetsAd.registerViewForImpression(adContainer) to register your Ad container view for impression;
    • to register ad views for click events call .registerViewsForClick(clickableViewList);
    • call .registerAdChoiceViewForClick(adChoiceView) to register AdChoice view for clicks.

    f. Unregister the Ad views by calling .unregisterViews() when you are no longer displaying them.

    g. Destroy the NativeAssetsAd by calling .destroy() when you’re not going to use it anymore.

    For example:


    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            ...
            adContainer = (LinearLayout) findViewById(R.id.native_assets_ad_container);
            title = (TextView) findViewById(R.id.native_assets_ad_title);
            body = (TextView) findViewById(R.id.native_assets_ad_body);
            adChoiceText = (TextView) findViewById(R.id.native_assets_ad_choices_caption);
            cta = (TextView) findViewById(R.id.native_assets_ad_call_to_action);
            icon = (ImageView) findViewById(R.id.native_assets_ad_icon);
            image = (ImageView) findViewById(R.id.native_assets_ad_image);
            adChoiceIcon = (ImageView) findViewById(R.id.native_assets_ad_choices_icon);
            rating = (RatingBar) findViewById(R.id.native_assets_ad_star_rating);
            final NativeAssetsConfig.Builder config = new NativeAssetsConfig.Builder()
                            .prefetchIcon(true)
                            .prefetchImage(true)
                            .prefetchAdChoiceIcon(true);
            NativeAssetsAd nativeAssetsAd = NativeAssetsAdPool.load(context, "YOUR_NATIVE_AD_UNIT_ID", config, nativeAssetsAdListener);
            ...
        }
        
        @Override
        protected void onDestroy() {
            super.onDestroy();
            if (nativeAssetsAd != null) {
                nativeAssetsAd.destroy();
            }
        }
        
        @Override
        public void onAdLoaded(@NonNull NativeAssetsAd nativeAssetsAd, @NonNull NativeAssets nativeAssets) {            
            title.setText(nativeAssets.getTitle());
            body.setText(nativeAssets.getText());
            renderImageView(icon, nativeAssets.getIcon());
            renderImageView(image, nativeAssets.getImage());
            cta.setText(nativeAssets.getCallToAction());
            AdChoice adChoice = nativeAssets.getAdChoice();
            if (adChoice != null) {                       
                adChoiceIcon.setImageDrawable(adChoice.getIcon().getDrawable());
                nativeAssetsAd.registerAdChoiceViewForClick(adChoiceIcon);
            }
            renderRatingBarView(rating, nativeAssets.getRating());
            nativeAssetsAd.registerViewsForClick(Arrays.asList(title, icon, image, cta));
            nativeAssetsAd.registerViewForImpression(adContainer);
        }
        
        private static void renderImageView(@NonNull ImageView view, Image image) {
            if (image != null) {
                view.setVisibility(View.VISIBLE);
                view.setAdjustViewBounds(true);
                int width = image.getWidth();
                int height = image.getHeight();
                if (width > 0 && height > 0) {
                view.setMaxWidth(width);
                    view.setMaxHeight(height);
                }
                view.setImageDrawable(image.getDrawable());
            } else {
                view.setImageDrawable(null);
                view.setVisibility(GONE);
            }
        }
        
        private static void renderRatingBarView(@NonNull RatingBar view, @Nullable Rating starRating) {
            view.setStepSize(0.1F);
            view.setIsIndicator(true);
            if (starRating != null) {
                view.setNumStars((int) starRating.getScale());
                view.setRating((float) starRating.getValue());
                view.setVisibility(View.VISIBLE);
            } else {
                view.setVisibility(GONE);
            }
        }
    Activity lifecycle events handling 

    onPause()onResume() and onDestroy() methods should be called depending on the activity lifecycle events.

    Example:
    @Override
    protected void onPause() {
        super.onPause();
        nativeAd.onActivityPaused();
    }
    @Override
    protected void onResume() {
        super.onResume();
        nativeAd.onActivityResumed();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        nativeAd.destroy();
    }
    
  2. Congratulations! Compile and Run your project . You can now customize your Native Assets Ad to match the look and feel of your app.

Advanced Customization


Was this article useful?