|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package com.google.android.gms.snippets |
| 15 | + |
| 16 | +import android.content.Context |
| 17 | +import android.os.Bundle |
| 18 | +import android.util.Log |
| 19 | +import com.google.ads.mediation.admob.AdMobAdapter |
| 20 | +import com.google.android.gms.ads.AdFormat |
| 21 | +import com.google.android.gms.ads.AdSize |
| 22 | +import com.google.android.gms.ads.admanager.AdManagerAdRequest |
| 23 | +import com.google.android.gms.ads.query.QueryInfo |
| 24 | +import com.google.android.gms.ads.query.QueryInfoGenerationCallback |
| 25 | + |
| 26 | +/** Kotlin code snippets for the developer guide. */ |
| 27 | +class AdManagerSCARSnippets { |
| 28 | + |
| 29 | + fun loadNative(applicationContext: Context, adUnitID: String) { |
| 30 | + // [START signal_request_native] |
| 31 | + // Specify the "query_info_type" as "requester_type_8" to |
| 32 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 33 | + val extras = Bundle() |
| 34 | + extras.putString("query_info_type", "requester_type_8") |
| 35 | + |
| 36 | + // Create a signal request for an ad. |
| 37 | + val signalRequest: AdManagerAdRequest = |
| 38 | + AdManagerAdRequest.Builder() |
| 39 | + .addNetworkExtrasBundle(AdMobAdapter::class.java, extras) |
| 40 | + .setRequestAgent("REQUEST_AGENT") |
| 41 | + .build() |
| 42 | + |
| 43 | + // Generate and send the signal request. |
| 44 | + QueryInfo.generate( |
| 45 | + applicationContext, |
| 46 | + AdFormat.NATIVE, |
| 47 | + signalRequest, |
| 48 | + adUnitID, |
| 49 | + object : QueryInfoGenerationCallback() { |
| 50 | + override fun onSuccess(queryInfo: QueryInfo) { |
| 51 | + Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery()) |
| 52 | + // TODO: Fetch the ad response using your generated query info. |
| 53 | + } |
| 54 | + |
| 55 | + override fun onFailure(error: String) { |
| 56 | + Log.d(TAG, "QueryInfo failed with error: $error") |
| 57 | + // TODO: Handle error. |
| 58 | + } |
| 59 | + }, |
| 60 | + ) |
| 61 | + // [END signal_request_native] |
| 62 | + } |
| 63 | + |
| 64 | + fun loadBanner(applicationContext: Context, adUnitID: String) { |
| 65 | + // [START signal_request_banner] |
| 66 | + |
| 67 | + // Specify the "query_info_type" as "requester_type_8" to |
| 68 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 69 | + val extras = Bundle() |
| 70 | + extras.putString("query_info_type", "requester_type_8") |
| 71 | + |
| 72 | + // Set the adaptive banner size. |
| 73 | + // Refer to the AdSize class for available ad sizes. |
| 74 | + val size: AdSize = |
| 75 | + AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320) |
| 76 | + extras.putInt("adaptive_banner_w", size.width) |
| 77 | + extras.putInt("adaptive_banner_h", size.height) |
| 78 | + |
| 79 | + // Create a signal request for an ad. |
| 80 | + val signalRequest: AdManagerAdRequest = |
| 81 | + AdManagerAdRequest.Builder() |
| 82 | + .addNetworkExtrasBundle(AdMobAdapter::class.java, extras) |
| 83 | + .setRequestAgent("REQUEST_AGENT") |
| 84 | + .build() |
| 85 | + |
| 86 | + // Generate and send the signal request. |
| 87 | + QueryInfo.generate( |
| 88 | + applicationContext, |
| 89 | + AdFormat.BANNER, |
| 90 | + signalRequest, |
| 91 | + adUnitID, |
| 92 | + object : QueryInfoGenerationCallback() { |
| 93 | + override fun onSuccess(queryInfo: QueryInfo) { |
| 94 | + Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery()) |
| 95 | + // TODO: Fetch the ad response using your generated query info. |
| 96 | + } |
| 97 | + |
| 98 | + override fun onFailure(error: String) { |
| 99 | + Log.d(TAG, "QueryInfo failed with error: $error") |
| 100 | + // TODO: Handle error. |
| 101 | + } |
| 102 | + }, |
| 103 | + ) |
| 104 | + // [END signal_request_banner] |
| 105 | + } |
| 106 | + |
| 107 | + fun loadNativePlusBanner(applicationContext: Context, adUnitID: String) { |
| 108 | + // [START signal_request_native_plus_banner] |
| 109 | + |
| 110 | + // Specify the "query_info_type" as "requester_type_8" to |
| 111 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 112 | + val extras = Bundle() |
| 113 | + extras.putString("query_info_type", "requester_type_8") |
| 114 | + |
| 115 | + // Set the adaptive banner size. |
| 116 | + // Refer to the AdSize class for available ad sizes. |
| 117 | + val size: AdSize = |
| 118 | + AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320) |
| 119 | + extras.putInt("adaptive_banner_w", size.getWidth()) |
| 120 | + extras.putInt("adaptive_banner_h", size.getHeight()) |
| 121 | + |
| 122 | + // Create a signal request for an ad. |
| 123 | + val signalRequest: AdManagerAdRequest = |
| 124 | + AdManagerAdRequest.Builder() |
| 125 | + .addNetworkExtrasBundle(AdMobAdapter::class.java, extras) |
| 126 | + .setRequestAgent("REQUEST_AGENT") |
| 127 | + .build() |
| 128 | + |
| 129 | + // Generate and send the signal request. |
| 130 | + QueryInfo.generate( |
| 131 | + applicationContext, |
| 132 | + AdFormat.NATIVE, |
| 133 | + signalRequest, |
| 134 | + adUnitID, |
| 135 | + object : QueryInfoGenerationCallback() { |
| 136 | + override fun onSuccess(queryInfo: QueryInfo) { |
| 137 | + Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery()) |
| 138 | + // TODO: Fetch the ad response using your generated query info. |
| 139 | + } |
| 140 | + |
| 141 | + override fun onFailure(error: String) { |
| 142 | + Log.d(TAG, "QueryInfo failed with error: $error") |
| 143 | + // TODO: Handle error. |
| 144 | + } |
| 145 | + }, |
| 146 | + ) |
| 147 | + // [END signal_request_native_plus_banner] |
| 148 | + } |
| 149 | + |
| 150 | + companion object { |
| 151 | + private const val TAG = "AdManagerSCARSnippets" |
| 152 | + } |
| 153 | +} |
0 commit comments