כשמודעה מותאמת נטענת, GMA Next Gen SDK מפעיל את מאזין האירועים של פורמט המודעה המתאים. האחריות להצגת המודעה מוטלת על האפליקציה, אבל היא לא חייבת להציג אותה באופן מיידי. כדי להקל על הצגת פורמטים של מודעות שמוגדרים על ידי המערכת, ה-SDK מציע כמה משאבים שימושיים, כמו שמתואר בהמשך.
הגדרת הכיתה NativeAdView
הגדרת כיתה NativeAdView. הכיתה הזו היא כיתה מסוג
ViewGroup
והיא מאגר ברמה העליונה לכיתה מסוג NativeAdView. כל תצוגה של מודעה מותאמת מכילה נכסים של מודעות מותאמות, כמו רכיב התצוגה MediaView או רכיב התצוגה Title, שחייב להיות צאצא של האובייקט NativeAdView.
פריסת XML
מוסיפים קובץ XML NativeAdView לפרויקט:
<com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal">
<ImageView
android:id="@+id/ad_app_icon" />
<TextView
android:id="@+id/ad_headline" />
</LinearLayout>
<!--Add remaining assets such as the image and media view.-->
</LinearLayout>
</com.google.android.libraries.ads.mobile.sdk.nativead.NativeAdView>
Jetpack פיתוח נייטיב
כוללים את המודול JetpackComposeDemo/compose-util
שכולל כלי עזר להרכבת NativeAdView והנכסים שלו.
באמצעות המודול compose-util, יוצרים NativeAdView:
import com.google.android.gms.compose_util.NativeAdAttribution
import com.google.android.gms.compose_util.NativeAdView
@Composable
/** Display a native ad with a user defined template. */
fun DisplayNativeAdView(nativeAd: NativeAd) {
NativeAdView {
// Display the ad attribution.
NativeAdAttribution(text = context.getString("Ad"))
// Add remaining assets such as the image and media view.
}
}
טיפול במודעה מותאמת שנטענה
כשמודעה מותאמת נטענת, צריך לטפל באירוע הקריאה החוזרת, להרחיב את התצוגה של המודעה המותאמת ולהוסיף אותה להיררכיית התצוגה:
Kotlin
// Build an ad request with native ad options to customize the ad.
val adTypes = listOf(NativeAd.NativeAdType.NATIVE)
val adRequest = NativeAdRequest
.Builder("/21775744923/example/native", adTypes)
.build()
val adCallback =
object : NativeAdLoaderCallback {
override fun onNativeAdLoaded(nativeAd: NativeAd) {
activity?.runOnUiThread {
val nativeAdBinding = NativeAdBinding.inflate(layoutInflater)
val adView = nativeAdBinding.root
val frameLayout = myActivityLayout.nativeAdPlaceholder
// Populate and register the native ad asset views.
displayNativeAd(nativeAd, nativeAdBinding)
// Remove all old ad views and add the new native ad
// view to the view hierarchy.
frameLayout.removeAllViews()
frameLayout.addView(adView)
}
}
}
// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback)
Java
// Build an ad request with native ad options to customize the ad.
List<NativeAd.NativeAdType> adTypes = Arrays.asList(NativeAd.NativeAdType.NATIVE);
NativeAdRequest adRequest = new NativeAdRequest
.Builder("/21775744923/example/native", adTypes)
.build();
NativeAdLoaderCallback adCallback = new NativeAdLoaderCallback() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
if (getActivity() != null) {
getActivity()
.runOnUiThread(() -> {
// Inflate the native ad view and add it to the view hierarchy.
NativeAdBinding nativeAdBinding = NativeAdBinding.inflate(getLayoutInflater());
NativeAdView adView = (NativeAdView) nativeAdBinding.getRoot();
FrameLayout frameLayout = myActivityLayout.nativeAdPlaceholder;
// Populate and register the native ad asset views.
displayNativeAd(nativeAd, nativeAdBinding);
// Remove all old ad views and add the new native ad
// view to the view hierarchy.
frameLayout.removeAllViews();
frameLayout.addView(adView);
});
}
}
};
// Load the native ad with our request and callback.
NativeAdLoader.load(adRequest, adCallback);
הערה: כל הנכסים של מודעה מותאמת מסוימת צריכים להיות מוצגים בתוך התג NativeAdView layout. GMA Next Gen SDK מנסה לרשום אזהרה ביומן כשנכסים מותאמים מוצגים מחוץ לפריסת תצוגת המודעה המותאמת.
בנוסף, מחלקות התצוגה של המודעות יש שיטות שמשמשות לרישום התצוגה שמשמשת לכל נכס בנפרד, ושיטה אחת לרישום האובייקט NativeAd עצמו.
רישום התצוגות בצורה הזו מאפשר ל-SDK לטפל אוטומטית במשימות כמו:
- קליקים להקלטה
- תיעוד חשיפות כשהפיקסל הראשון גלוי במסך
- הצגת שכבת-על של AdChoices בנכסי קריאייטיב של מודעות מילוי שטחי פרסום (backfill) מסוג מודעות מותאמות – כרגע מוגבל לקבוצה נבחרת של בעלי אפליקציות
הצגת המודעה המותאמת
בדוגמה הבאה אפשר לראות איך מציגים מודעה מותאמת:
Kotlin
private fun displayNativeAd(nativeAd: NativeAd, nativeAdBinding : NativeAdBinding) {
// Set the native ad view elements.
val nativeAdView = nativeAdBinding.root
nativeAdView.advertiserView = nativeAdBinding.adAdvertiser
nativeAdView.bodyView = nativeAdBinding.adBody
nativeAdView.callToActionView = nativeAdBinding.adCallToAction
nativeAdView.headlineView = nativeAdBinding.adHeadline
nativeAdView.iconView = nativeAdBinding.adAppIcon
nativeAdView.priceView = nativeAdBinding.adPrice
nativeAdView.starRatingView = nativeAdBinding.adStars
nativeAdView.storeView = nativeAdBinding.adStore
// Set the view element with the native ad assets.
nativeAdBinding.adAdvertiser.text = nativeAd.advertiser
nativeAdBinding.adBody.text = nativeAd.body
nativeAdBinding.adCallToAction.text = nativeAd.callToAction
nativeAdBinding.adHeadline.text = nativeAd.headline
nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.icon?.drawable)
nativeAdBinding.adPrice.text = nativeAd.price
nativeAd.starRating?.toFloat().let { value ->
nativeAdBinding.adStars.rating = value
}
nativeAdBinding.adStore.text = nativeAd.store
// Hide views for assets that don't have data.
nativeAdBinding.adAdvertiser.visibility = getAssetViewVisibility(nativeAd.advertiser)
nativeAdBinding.adBody.visibility = getAssetViewVisibility(nativeAd.body)
nativeAdBinding.adCallToAction.visibility = getAssetViewVisibility(nativeAd.callToAction)
nativeAdBinding.adHeadline.visibility = getAssetViewVisibility(nativeAd.headline)
nativeAdBinding.adAppIcon.visibility = getAssetViewVisibility(nativeAd.icon)
nativeAdBinding.adPrice.visibility = getAssetViewVisibility(nativeAd.price)
nativeAdBinding.adStars.visibility = getAssetViewVisibility(nativeAd.starRating)
nativeAdBinding.adStore.visibility = getAssetViewVisibility(nativeAd.store)
// Inform GMA Next Gen SDK that you have finished populating
// the native ad views with this native ad.
nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia)
}
/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return [View.VISIBLE] if the asset is not null, [View.INVISIBLE] otherwise.
*/
private fun getAssetViewVisibility(asset: Any?): Int {
return if (asset == null) View.INVISIBLE else View.VISIBLE
}
Java
private void displayNativeAd(ad: NativeAd, nativeAdBinding : NativeAdBinding) {
// Set the native ad view elements.
NativeAdView nativeAdView = nativeAdBinding.getRoot();
nativeAdView.setAdvertiserView(nativeAdBinding.adAdvertiser);
nativeAdView.setBodyView(nativeAdBinding.adBody);
nativeAdView.setCallToActionView(nativeAdBinding.adCallToAction);
nativeAdView.setHeadlineView(nativeAdBinding.adHeadline);
nativeAdView.setIconView(nativeAdBinding.adAppIcon);
nativeAdView.setPriceView(nativeAdBinding.adPrice);
nativeAdView.setStarRatingView(nativeAdBinding.adStars);
nativeAdView.setStoreView(nativeAdBinding.adStore);
// Set the view element with the native ad assets.
nativeAdBinding.adAdvertiser.setText(nativeAd.getAdvertiser());
nativeAdBinding.adBody.setText(nativeAd.getBody());
nativeAdBinding.adCallToAction.setText(nativeAd.getCallToAction());
nativeAdBinding.adHeadline.setText(nativeAd.getHeadline());
if (nativeAd.getIcon() != null) {
nativeAdBinding.adAppIcon.setImageDrawable(nativeAd.getIcon().getDrawable());
}
nativeAdBinding.adPrice.setText(nativeAd.getPrice());
if (nativeAd.getStarRating() != null) {
nativeAdBinding.adStars.setRating(nativeAd.getStarRating().floatValue());
}
nativeAdBinding.adStore.setText(nativeAd.getStore());
// Hide views for assets that don't have data.
nativeAdBinding.adAdvertiser.setVisibility(getAssetViewVisibility(nativeAd.getAdvertiser()));
nativeAdBinding.adBody.setVisibility(getAssetViewVisibility(nativeAd.getBody()));
nativeAdBinding.adCallToAction.setVisibility(getAssetViewVisibility(nativeAd.getCallToAction()));
nativeAdBinding.adHeadline.setVisibility(getAssetViewVisibility(nativeAd.getHeadline()));
nativeAdBinding.adAppIcon.setVisibility(getAssetViewVisibility(nativeAd.getIcon()));
nativeAdBinding.adPrice.setVisibility(getAssetViewVisibility(nativeAd.getPrice()));
nativeAdBinding.adStars.setVisibility(getAssetViewVisibility(nativeAd.getStarRating()));
nativeAdBinding.adStore.setVisibility(getAssetViewVisibility(nativeAd.getStore()));
// Inform GMA Next Gen SDK that you have finished populating
// the native ad views with this native ad.
nativeAdView.registerNativeAd(nativeAd, nativeAdBinding.adMedia);
}
/**
* Determines the visibility of an asset view based on the presence of its asset.
*
* @param asset The native ad asset to check for nullability.
* @return {@link View#VISIBLE} if the asset is not null, {@link View#INVISIBLE} otherwise.
*/
private int getAssetViewVisibility(Object asset) {
return (asset == null) ? View.INVISIBLE : View.VISIBLE;
}
שכבת-על של AdChoices
ערכת ה-SDK מוסיפה שכבת-על של AdChoices כתצוגת מודעה כשמוחזרת מודעה למילוי חוסר. אם באפליקציה שלכם נעשה שימוש במילוי אוטומטי של מודעות מותאמות, צריך להשאיר מקום בפינה המועדפת בתצוגת המודעות המותאמות ללוגו AdChoices שמוכנס באופן אוטומטי. בנוסף, חשוב שיהיה קל לראות את שכבת-העל של AdChoices, לכן צריך לבחור בקפידה את צבעי הרקע והתמונות. מידע נוסף על המראה והתפקוד של שכבת העל זמין בהנחיות להטמעה של מודעות מותאמות שנקנות באופן פרוגרמטי.
סימן למודעות מותאמות פרוגרמטיות
כשמציגים מודעות מותאמות שנקנות באופן פרוגרמטי, צריך להציג סימן למודעה כדי לציין שהתצוגה היא מודעה. מידע נוסף זמין בהנחיות המדיניות שלנו.
טיפול בקליקים
אסור להטמיע מטפלים מותאמים אישית של קליקים בתצוגות שמעל או בתוך תצוגת המודעה המקורית. ערכת ה-SDK מטפלת בקליקים על נכסי התצוגה של המודעה, בתנאי שמילאתם ורשמתם את נתוני התצוגות של הנכס בצורה נכונה.
כדי להאזין לקליקים, מטמיעים את קריאת החזרה (callback) של קליקים ב-GMA Next Gen SDK:
Kotlin
private fun setEventCallback(nativeAd: NativeAd) {
nativeAd.adEventCallback =
object : NativeAdEventCallback {
override fun onAdClicked() {
Log.d(Constant.TAG, "Native ad recorded a click.")
}
}
}
Java
private void setEventCallback(NativeAd nativeAd) {
nativeAd.setAdEventCallback(new NativeAdEventCallback() {
@Override
public void onAdClicked() {
Log.d(Constant.TAG, "Native ad recorded a click.");
}
});
}
ImageScaleType
למחלקת MediaView יש מאפיין ImageScaleType כשמוצגים תמונות. כדי לשנות את אופן שינוי הגודל של תמונה ב-MediaView, צריך להגדיר את ImageView.ScaleType המתאים באמצעות השיטה setImageScaleType() של MediaView:
Kotlin
nativeAdViewBinding.mediaView.imageScaleType = ImageView.ScaleType.CENTER_CROP
Java
nativeAdViewBinding.mediaView.setImageScaleType(ImageView.ScaleType.CENTER_CROP);
MediaContent
הנתונים שקשורים לתוכן המדיה של המודעה המקורית מאוחסנים במחלקה MediaContent, והמודעה מוצגת באמצעות המחלקה MediaView. כשהמאפיין MediaView mediaContent מוגדר עם מופע MediaContent:
אם נכס וידאו זמין, הוא נטען למאגר ומתחיל לפעול בתוך התג
MediaView. כדי לדעת אם נכס וידאו זמין, אפשר לבדוק אתhasVideoContent().אם המודעה לא מכילה נכס וידאו, הנכס
mainImageיורד ומוצב בתוךMediaView.
הסרת מודעה
אחרי שמציגים מודעה מותאמת, צריך להשמיד את המודעה. בדוגמה הבאה מוצגת השמדה של מודעה מקורית:
Kotlin
nativeAd.destroy()
Java
nativeAd.destroy();
בדיקת קוד של מודעות מותאמות
מודעות במכירה ישירה
אם אתם רוצים לבדוק איך נראות מודעות מותאמות שנמכרות ישירות, אתם יכולים להשתמש במזהה יחידת המודעות הזה ב-Ad Manager:
/21775744923/example/native
היא מוגדרת להצגת מודעות לדוגמה להתקנת אפליקציות ומודעות באתרי תוכן, וגם פורמט מותאם של מודעה מותאמת עם הנכסים הבאים:
- כותרת (טקסט)
- MainImage (תמונה)
- כיתוב (טקסט)
מזהה התבנית של פורמט המודעה המותאמת הוא 10063170.
מודעות גיבוי מותאמות
מילוי חוזר ב-Ad Exchange מוגבל לקבוצה נבחרת של בעלי אפליקציות. כדי לבדוק את ההתנהגות של מודעות מותאמות למילוי שטחי פרסום, משתמשים ביחידת המודעות הזו ב-Ad Manager:
/21775744923/example/native-backfill
הוא מציג דוגמאות למודעות להתקנת אפליקציה ולמודעות ברשת המדיה שכוללות את שכבת העל AdChoices.
חשוב לזכור לעדכן את הקוד כך שיפנה למזהים של יחידת המודעות והתבנית בפועל לפני שהמודעה תתחיל לפעול.
השלבים הבאים
כדאי לעיין בנושאים הבאים:
דוגמה
מורידים ומריצים את האפליקציה לדוגמה שמדגימה את השימוש ב-GMA Next Gen SDK.