Android でネイティブ広告を表示する
注:
ネイティブインプレッションカウント手法は、バナークリエイティブに使用されるカウントオンレンダリング手法に従います。画面の長さに関係なく、ネイティブ広告がレンダリングされるとすぐにインプレッションが発生します。 これにより、精度が向上し、成果物が向上するため、全体的な歩留まりが向上します。
ネイティブ広告を使用すると、アプリケーションの残りの部分の外観に合わせてカスタマイズされた広告を作成できます。 このページでは、ネイティブ広告 API について大まかに説明し、使用例を示します。
仲介によってサポートされるネイティブ ネットワーク:
- AdMob と DFP
ネイティブ広告を配信するには、ネイティブ広告リクエストを送信し、ネイティブ広告応答を受け取ります。 Android 9 以降と API v. 28 以降では、視認性を正確に追跡するために、要求は既定で HTTPS である必要があります。 HTTPS は で useHttps(true)
有効にすることができます。
次のコード例では、次の手順を実行します。
要求オブジェクトを設定し、次のいずれかを指定します。
配置 ID (次のコード例に示すように)、OR
インベントリ コードとメンバー ID の組み合わせ:
public NativeAdRequest nativeAdRequest= new NativeAdRequest(context, "PLACEMENT_ID"); //public NativeAdRequest nativeAdRequest= new NativeAdRequest(context, "INVENTORY_CODE", MEMBER_ID);
必要に応じて、この
NativeAdRequest
の をrenderer_id
設定できます。 (詳細については、ネイティブ レイアウト サービスに関renderer_id
するページを参照してください)。vastxmlrenderer_id
、likes、downloads、saleprice、phone、address、display URL を でNativeAdResponse
返すには、 を指定する必要があります。nativeAdRequest.setRendererId(RENDERER_ID);
クリック (NativeAdEventListener) などのネイティブ広告イベントを通知するリスナーを登録します。
リスナーを登録して、ネイティブ要求の状態 (成功または失敗) を通知します。 リスナーはインターフェイスを実装する
NativeAdRequestListener
必要があります。要求が成功した場合 (つまり、起動)、
NativeAdListener.onAdLoaded()
ネイティブ広告アセットがオブジェクトにNativeAdResponse
読み込まれます。これは、アプリのネイティブな外観に一致するビューで使用できます。 次に、これらのビューの親ビューまたはコンテナー ビューを登録して、インプレッションとクリックの追跡を有効にします。ワークフローを完了した後、ネイティブ広告ビューの登録を解除します。 unregister メソッドが呼び出されると、Xandr Mobile SDK の OMID ビューアビリティ スクリプトによって、worflow 中に OMID セッションの一括レポートが生成されます。 そのため、パブリッシャーは、この API を実装することが重要です。これにより、視認性に関する正確な測定が容易になります。
注:
ネイティブ ビューとネイティブ応答オブジェクトへの参照を維持します。 ネイティブ ビューとネイティブ応答オブジェクトへの参照を維持します。
必要に応じて、ネイティブ広告ビューと NativeAdResponse
オブジェクトへの参照を保持する必要があります。
public class MyActivity extends Activity {
Context activityContext;
NativeAdResponse nativeAdResponse;
LinearLayout container;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityContext = this;
// Create a NativeAdRequest object
NativeAdRequest adRequest = new NativeAdRequest(activityContext, "123456"); // Placement ID
// Optionally set the renderer_id
//adRequest.setRendererId(123);
// Create a listener for ad events
NativeAdEventListener adEventListener = new
NativeAdEventListener() {
@Override
public void onAdWasClicked() {
// Do something when the view is clicked
}
@Override
public void onAdWillLeaveApplication() {
// Do something when the ad is taking user away from current app
}
@Override
public void onAdWasClicked(String clickUrl, String fallbackURL) {
// Handle Click URL
}
};
// Whether to pre-load the native ad's icon and main image
adRequest.shouldLoadIcon(true);
adRequest.shouldLoadImage(true);
adRequest.setListener(new NativeAdRequestListener() {
@Override
public void onAdLoaded(NativeAdResponse response) {
nativeAdResponse = response;
// Cover image
ImageView imageView = new ImageView(activityContext);
imageView.setImageBitmap(response.getImage());
// Icon image
ImageView iconView = new ImageView(activityContext);
iconView.setImageBitmap(response.getIcon());
// Title
TextView title = new TextView(activityContext);
title.setText(response.getTitle());
// Main text
TextView description = new TextView(activityContext);
description.setText(response.getDescription());
// Text that indicates a call to action -- for example, to install an app
TextView callToAction = new TextView(activityContext);
callToAction.setText(response.getCallToAction());
// Create a container (a parent view that holds all the
// views for native ads)
LinearLayout container = new LinearLayout(activityContext);
container.addView(iconView);
container.addView(title);
// Add the native ad container to the view hierarchy
LinearLayout ad_frame = findViewById(R.id.native_ad_frame);
ad_frame.addView(container);
}
@Override
public void onAdFailed(ResultCode errorcode) {
}
});
// Call loadAd() to request a response once
adRequest.loadAd();
// Register native views for click and impression tracking. The
// adEventListener is the listener created above; it can be null if
// you don't want to receive notifications about click events.
// Impressions and clicks won't be counted if the view is not registered.
NativeAdSDK.registerTracking(nativeAdResponse, container, adEventListener);
// It's your responsibility to keep a reference to the view
// and NativeAdResponse object if necessary.
// Once done with the native ad view, call the following method to
// unregister that view.
NativeAdSDK.unRegisterTracking(container);
}
}
ネイティブでサポートされるフィールド
Mobile SDK のバージョン 5.0 以降では、ネイティブアセットのサポートは、Xandr の UI でのネイティブ クリエイティブの設定方法に合わせて調整されています。
でレガシ ネイティブを引き続き使用している場合は、クリエイティブの "新規" ネイティブに移動する必要があります。
SDK でサポートされているネイティブ資産の包括的な一覧を次に示します。
Asset | 5.0 より前にサポートされていますか? | サポートされている Post 5.0? | v5.0 以降の API-Usage の例 |
---|---|---|---|
画像、幅、高さ | そうだそうだ | そうだそうだ | nativeAdResponse.getImage() ;nativeAdResponse.getImageSize() ;nativeAdResponse.getImageUrl(); |
アイコン + 幅 + 高さ | はい、いいえ、いいえ | そうだそうだ | nativeAdResponse.getIcon() ;nativeAdResponse.getIconSize() ;nativeAdResponse.getIconUrl(); |
タイトル | はい | はい | nativeAdResponse.getTitle(); |
主催 | はい | はい | nativeAdResponse.getSponsoredBy(); |
Body text | はい | はい | nativeAdResponse.getDescription(); |
Desc2 | はい | はい | nativeAdResponse.getAdditionalDescription(); |
行動喚起 | はい | はい | nativeAdResponse.getCallToAction(); |
評価、スケール | そうそう | はい、いいえ | nativeAdResponse.getAdStarRating(); |
いいね! | いいえ | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
ダウンロード | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
Price | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
セール価格 | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
Phone | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
Address | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
表示 URL | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
[URL] をクリックします | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
[フォールバック URL] をクリックします | 不要 | はい (json のみ) | if((nativeAdResponse.getNetworkIdentifier() == NativeAdResponse.Network.APPNEXUS) &&. (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) instanceof JSONObject){ JSONObject nativeResponseJSON = (JSONObject) (nativeAdResponse.getNativeElements().get(NativeAdResponse.NATIVE_ELEMENT_OBJECT)) ;String likes = JsonUtil.getJSONString(nativeResponseJSON,"likes"); String downloads = JsonUtil.getJSONString(nativeResponseJSON,"downloads"); String price = JsonUtil.getJSONString(nativeResponseJSON,"price"); String saleprice = JsonUtil.getJSONString(nativeResponseJSON,"saleprice"); String phone = JsonUtil.getJSONString(nativeResponseJSON,"phone"); String address = JsonUtil.getJSONString(nativeResponseJSON,"address"); String displayurl = JsonUtil.getJSONString(nativeResponseJSON,"displayurl"); // To Get clickUrl String clickUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("url"); //To Get clickFallbackUrl String clickFallbackUrl = JsonUtil.getJSONObject(nativeResponseJSON,"link").getString("fallback_url"); } |
プライバシー URL | 不要 | はい | nativeAdResponse.getPrivacyLink(); |
ビデオ | 不要 | はい | nativeAdResponse.getVastXml(); |
Custom | はい | 不要 | |
Context | はい | 不要 | |
本文 | はい | いいえ |