Integrating Native Ads

Document created by pubmatic-archivist on Mar 27, 2017
Version 1Show Document
  • View in full screen mode

Integrating Native Ads

 

Native advertising is a form of online advertising where the ad is rendered with the same look and feel like the page or application in which it is displayed.

To display Native Ads in your application:

  1. Initialize PMNativeAd.
  2. Use PMNativeAdDelegate to get the callback and assets required for rendering Native ad.

The following examples show sample integration of a Native ad with your application.

Example: Requesting a Native Ad:

    

//Instantiate Asset to be requested in Native ad request along with attributes

 

//Requesting for Icon image Asset of respective attributes

PMNativeImageAssetRequest *imageAsset = [PMNativeImageAssetRequest new];

imageAsset.assetId=2;

imageAsset.width =50;

imageAsset.height=50;

imageAsset.subtype=PMNativeImageAssetTypeIcon

;

[adRequest addAsset:imageAsset];

 

//Requesting for Main image Asset of respective attributes

imageAsset = [PMNativeImageAssetRequest new];

imageAsset.assetId=4;

imageAsset.width =320;

imageAsset.height=100;

imageAsset.subtype=PMNativeImageAssetTypeMain;

[adRequest addAsset:imageAsset];

 

//Requesting for Title asset in application

PMNativeTitleAssetRequest *titleAsset = [PMNativeTitleAssetRequest new];

titleAsset.assetId=1;

titleAsset.titleLength = 100;

[adRequest addAsset:titleAsset];

 

//Requesting for Data asset of type description in application

PMNativeDataAssetRequest *dataAsset = [PMNativeDataAssetRequest new];

dataAsset.assetId=3;

dataAsset.length = 100;

dataAsset.subtype=PMNativeDataAssetTypeDescription;

[adRequest addAsset:dataAsset]; PMNativeDataAssetRequest

 

//Requesting for Data asset of type CTA Text in application

dataAsset = [PMNativeDataAssetRequest new];

dataAsset.assetId=5;

dataAsset.subtype=PMNativeDataAssetTypeCTAText;

[adRequest addAsset:dataAsset];

 

//Requesting for Data asset of type star rating in application

dataAsset = [PMNativeDataAssetRequest new];

dataAsset.assetId=6;

dataAsset.subtype=PMNativeDataAssetTypeRating;

[adRequest addAsset:dataAsset];

 

//Initialize PMNativeAd

PMNativeAd * nativeAd =[PMNativeAd new] ;

nativeAd.delegate = self;

ad.execute(adRequest);

 

Example: Mocean Ad Request        

    

 

MoceanNativeAdRequest * adRequest = [MoceanNativeAdRequest new];

adRequest.zoneId= @"<zone_id>";

 

 

Example: PubMatic Ad Request                                       

    

 

PubMaticNativeAdRequest * adRequest = [ [PubMaticNativeAdRequest alloc] initWithPublisherId:@"<pub_id>" WithSiteId:@"<site_id>" WithAdTagId:@"<ad_tag_id>"];

 

Native Response Code

Example: Implementing PMNativeAdDelegate native response handling

    

- (void)nativeAdDidRecieveAd:(PMNativeAd *)nativeAd

{

    [self enableIneractions:YES];

    NSLog(@"Received Ad");

    if(nativeAd.jsTrackerString)

    {

        NSLog(@"Received JSTracker as %@",nativeAd.jsTrackerString);

    }

    [self log:[NSString stringWithFormat:@"Response :=>\n %@",nativeAd.nativeAdResponseDictionary]];

 

    for (PMNativeAssetResponse *asset in nativeAd.adAttributes.adAssetResponseArray)

    {       

        @try {

            switch (asset.assetId) {

                case 1:

                {

                    PMNativeTitleAssetResponse *titleAsset = (PMNativeTitleAssetResponse *) asset;

                    self.titleLabelView.text = titleAsset.text;                    

                }

                    break;

                case 2:

                {

                    PMNativeImageAssetResponse *imageAsset = (PMNativeImageAssetResponse *) asset;

                    [self.nativeAd loadInImageView:self.mainImage withURL:[imageAsset.imageURL description]];

                }

                    break;

                case 3:

                {

                    PMNativeDataAssetResponse *dataAsset = (PMNativeDataAssetResponse *) asset;

                    self.descriptionLabel.text = dataAsset.value;

                }

                    break;

                case 4:

                {

                    PMNativeImageAssetResponse *imageAsset = (PMNativeImageAssetResponse *) asset;

                    [self.nativeAd loadInImageView:self.mainImage withURL:[imageAsset.imageURL description]];

                }

                    break;

                case 5:

                {

                    PMNativeDataAssetResponse *dataAsset = (PMNativeDataAssetResponse *) asset;

                    [self.ctaLabel setText:dataAsset.value];

}

                    break;

                   

                case 6:

                {

                    PMNativeDataAssetResponse *dataAsset = (PMNativeDataAssetResponse *) asset;

                    if(dataAsset.value){

                        self.dyrateView.rate=[dataAsset.value floatValue];

                        self.starRatingView.hidden = NO;

                    }

                   

                }

                    break;

                   

                default:

                   

                    NSLog(@"Unexpected Asset found for Asset Id : %ld",(long)asset.assetId);

                   

                    break;

            }

        }

        @catch (NSException *exception) {

           

            NSLog(@"Unexpected Asset found for Asset Id : %ld",(long)asset.assetId);

        }

        @finally {

        }

    }

    /*

     Important: Must call this method when response rendering is complete. This method sets Tap gestures on the ad container layout. This is required for firing click tracker when ad is clicked by the user.

     */

    [self trackNativeAdViewForinteration:nativeAd];

}

 

- (void)nativeAd:(PMNativeAd*)aNativeAd didReceiveThirdPartyRequest:(NSDictionary*)properties withParams:(NSDictionary*)params{

 

}

 

- (void)nativeAd:(PMNativeAd*)nativeAd didFailToReceiveAdWithError:(NSError*)error{

}

- (void)nativeAdDidClick:(PMNativeAd *)nativeAd{      

}

Attachments

    Outcomes