privatevoidloadCustomNativeAd(Contextcontext,ViewGroupnativeCustomFormatAdContainer){AdLoaderadLoader=newAdLoader.Builder(context,"AD_UNIT_ID").forCustomFormatAd("CUSTOM_TEMPLATE_ID",newNativeCustomFormatAd.OnCustomFormatAdLoadedListener(){@OverridepublicvoidonCustomFormatAdLoaded(@NonNullNativeCustomFormatAdad){// Show the ad first and then register your view and begin open measurement.// Make sure to do this on the main thread.// ...// Show ad// ...startOpenMeasurement(ad,nativeCustomFormatAdContainer);}},newNativeCustomFormatAd.OnCustomClickListener(){@OverridepublicvoidonCustomClick(NativeCustomFormatAdad,StringassetName){// Handle the click action}}).build();adLoader.loadAd(newAdRequest.Builder().build());}
privatefunloadCustomNativeAd(context:Context,nativeCustomFormatAdContainer:ViewGroup){valadLoader=AdLoader.Builder(context,"AD_UNIT_ID").forCustomFormatAd("CUSTOM_TEMPLATE_ID",NativeCustomFormatAd.OnCustomFormatAdLoadedListener{ad->
// Show the ad first and then register your view and begin open measurement. Make sure// to do this on the main thread.// ...// Show ad// ...startOpenMeasurement(ad,nativeCustomFormatAdContainer)},NativeCustomFormatAd.OnCustomClickListener{ad,assetName->
// Handle the click action.},).build()adLoader.loadAd(AdRequest.Builder().build())}
[null,null,["最后更新时间 (UTC):2025-08-31。"],[[["\u003cp\u003eThis guide explains how to integrate custom native ad formats with open measurement for display-type ads using the Google Mobile Ads SDK.\u003c/p\u003e\n"],["\u003cp\u003eFor custom native ads with video assets, the Mobile Ads SDK automatically tracks viewability.\u003c/p\u003e\n"],["\u003cp\u003ePublishers using display-type custom native ads need to register their ad view with the \u003ccode\u003eDisplayOpenMeasurement\u003c/code\u003e object and explicitly start measurement.\u003c/p\u003e\n"],["\u003cp\u003eTo ensure IAB-certified measurement data, publishers must complete the IAB certification process after releasing their app.\u003c/p\u003e\n"],["\u003cp\u003eBefore implementing open measurement, ensure you have fulfilled prerequisites such as integrating custom native ad formats and configuring a viewability provider.\u003c/p\u003e\n"]]],[],null,["This guide explains how to integrate custom native ad formats with open\nmeasurement.\n\nPrerequisites\n\nBefore you continue, do the following:\n\n- Read [Open Measurement with the Mobile Ads\n SDK](/ad-manager/mobile-ads-sdk/android/open-measurement).\n- Integrate [custom native ad\n formats](/ad-manager/mobile-ads-sdk/android/native/custom-formats).\n- [Configure a viewability\n provider](//support.google.com/admanager/answer/9025968#configure-a-viewability-provider) and assign it to your line item.\n- Enter your partner name when [creating a custom\n format](//support.google.com/admanager/answer/7661908) in the Ad Manager UI.\n\nIntegration\n\nIf you plan to use open measurement with custom native ad formats that\ndon't contain a video asset, you'll be responsible for calling the Open\nMeasurement APIs yourself.\n\nIf you're using custom native ad formats with a video asset, you don't need to\nfollow this guide; the Mobile Ads SDK tracks viewability of the video asset on\nyour behalf.\n| **Key Point:** In order for your display-type native custom ad measurement results to be certified by the IAB, you must be certified as a Measurement Provider with the [IAB Tech Lab](//iabtechlab.com/compliance-programs/faq-om-sdk-compliance/).\n\nLoad an ad\n\nLoading an ad is the same whether you're using open measurement or not. Here is\na method that demonstrates how to request a custom native ad: \n\nJava \n\n private void loadCustomNativeAd(Context context, ViewGroup nativeCustomFormatAdContainer) {\n\n AdLoader adLoader =\n new AdLoader.Builder(context, \"\u003cvar translate=\"no\"\u003eAD_UNIT_ID\u003c/var\u003e\")\n .forCustomFormatAd(\n \"\u003cvar translate=\"no\"\u003eCUSTOM_TEMPLATE_ID\u003c/var\u003e\",\n new NativeCustomFormatAd.OnCustomFormatAdLoadedListener() {\n @Override\n public void onCustomFormatAdLoaded(@NonNull NativeCustomFormatAd ad) {\n // Show the ad first and then register your view and begin open measurement.\n // Make sure to do this on the main thread.\n\n // ...\n // Show ad\n // ...\n\n startOpenMeasurement(ad, nativeCustomFormatAdContainer);\n }\n },\n new NativeCustomFormatAd.OnCustomClickListener() {\n @Override\n public void onCustomClick(NativeCustomFormatAd ad, String assetName) {\n // Handle the click action\n }\n })\n .build();\n adLoader.loadAd(new AdRequest.Builder().build());\n } \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/CustomNativeOMIDSnippets.java#L29-L57\n\nKotlin \n\n private fun loadCustomNativeAd(context: Context, nativeCustomFormatAdContainer: ViewGroup) {\n val adLoader =\n AdLoader.Builder(context, \"\u003cvar translate=\"no\"\u003eAD_UNIT_ID\u003c/var\u003e\")\n .forCustomFormatAd(\n \"\u003cvar translate=\"no\"\u003eCUSTOM_TEMPLATE_ID\u003c/var\u003e\",\n NativeCustomFormatAd.OnCustomFormatAdLoadedListener { ad -\u003e\n // Show the ad first and then register your view and begin open measurement. Make sure\n // to do this on the main thread.\n\n // ...\n // Show ad\n // ...\n\n startOpenMeasurement(ad, nativeCustomFormatAdContainer)\n },\n NativeCustomFormatAd.OnCustomClickListener { ad, assetName -\u003e\n // Handle the click action.\n },\n )\n .build()\n\n adLoader.loadAd(AdRequest.Builder().build())\n } \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/CustomNativeOMIDSnippets.kt#L24-L47\n\nReplace \u003cvar translate=\"no\"\u003eAD_UNIT_ID\u003c/var\u003e and \u003cvar translate=\"no\"\u003eCUSTOM_TEMPLATE_ID\u003c/var\u003e with your ad unit ID and custom template ID.\n\nRegister your view and begin measuring\n\nTo enable open measurement on a custom native ad, display the ad first, and then\nregister your custom ad view with the `DisplayOpenMeasurement` object associated\nwith the custom native ad. The `DisplayOpenMeasurement` object provides the\n`setView()` method to register your container view with open measurement.\n\nYou also need to explicitly tell the SDK to begin measuring your ad. To do this\ncall the `start()` method on the `DisplayOpenMeasurement` object of your\ncustom native ad. The `start()` method must be called from the main\nthread, and subsequent calls have no effect.\n\nHere's what it looks like: \n\nJava \n\n private void startOpenMeasurement(\n NativeCustomFormatAd ad, ViewGroup nativeCustomFormatAdContainer) {\n DisplayOpenMeasurement displayOpenMeasurement = ad.getDisplayOpenMeasurement();\n if (displayOpenMeasurement != null) {\n displayOpenMeasurement.setView(nativeCustomFormatAdContainer);\n displayOpenMeasurement.start();\n }\n } \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/java/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/CustomNativeOMIDSnippets.java#L61-L68\n\nKotlin \n\n private fun startOpenMeasurement(\n ad: NativeCustomFormatAd,\n nativeCustomFormatAdContainer: ViewGroup,\n ) {\n ad.getDisplayOpenMeasurement()?.let { displayOpenMeasurement -\u003e\n displayOpenMeasurement.setView(nativeCustomFormatAdContainer)\n displayOpenMeasurement.start()\n }\n } \n https://github.com/googleads/googleads-mobile-android-examples/blob/ccc290a583d7f552bdcf81ea76adc05beaa43f0b/kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/snippets/CustomNativeOMIDSnippets.kt#L51-L60\n\n\u003cbr /\u003e\n\nNote that you should display your ad before registering your view. Displaying\nyour ad is covered in [Custom Native Ad\nFormats](/ad-manager/mobile-ads-sdk/android/native/custom-formats#display_custom_native_ad_formats).\n\nThe `customTemplateAdFrame`\nlayout is the ad container for the custom native ad and must contain all\nassets within the bounding box of the view.\n| **Important:** Make sure you call `destroy()` on the custom native ad when you're done with the ad, for example, after dereferencing the object or destroying the activity. This makes sure that the IAB `sessionFinish` event is fired.\n\nCertify with IAB\n\nOnce you release your app, you will begin receiving measurement data; however,\nyour data won't be certified until you complete the\n[IAB certification process](//iabtechlab.com/compliance-programs/faq-om-sdk-compliance/)."]]