Skip to content

Commit 407ee23

Browse files
nventimiglicopybara-github
authored andcommitted
Added snippets for server to server requests.
PiperOrigin-RevId: 805509103
1 parent 0a4f6f7 commit 407ee23

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 AdmobSCARSnippets {
28+
29+
fun loadNative(applicationContext: Context, adUnitID: String) {
30+
// [START signal_request_native]
31+
// Create a signal request for an ad.
32+
val signalRequest: AdManagerAdRequest =
33+
AdManagerAdRequest.Builder().setRequestAgent("REQUEST_AGENT").build()
34+
35+
// Generate and send the signal request.
36+
QueryInfo.generate(
37+
applicationContext,
38+
AdFormat.NATIVE,
39+
signalRequest,
40+
adUnitID,
41+
object : QueryInfoGenerationCallback() {
42+
override fun onSuccess(queryInfo: QueryInfo) {
43+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery())
44+
// TODO: Fetch the ad response using your generated query info.
45+
}
46+
47+
override fun onFailure(error: String) {
48+
Log.d(TAG, "QueryInfo failed with error: $error")
49+
// TODO: Handle error.
50+
}
51+
},
52+
)
53+
// [END signal_request_native]
54+
}
55+
56+
fun loadBanner(applicationContext: Context, adUnitID: String) {
57+
// [START signal_request_banner]
58+
// Set the adaptive banner size.
59+
// Refer to the AdSize class for available ad sizes.
60+
val size: AdSize =
61+
AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320)
62+
val extras = Bundle()
63+
extras.putInt("adaptive_banner_w", size.width)
64+
extras.putInt("adaptive_banner_h", size.height)
65+
66+
// Create a signal request for an ad.
67+
val signalRequest: AdManagerAdRequest =
68+
AdManagerAdRequest.Builder()
69+
.setRequestAgent("REQUEST_AGENT")
70+
.addNetworkExtrasBundle(AdMobAdapter::class.java, extras)
71+
.build()
72+
73+
// Generate and send the signal request.
74+
QueryInfo.generate(
75+
applicationContext,
76+
AdFormat.BANNER,
77+
signalRequest,
78+
adUnitID,
79+
object : QueryInfoGenerationCallback() {
80+
override fun onSuccess(queryInfo: QueryInfo) {
81+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery())
82+
// TODO: Fetch the ad response using your generated query info.
83+
}
84+
85+
override fun onFailure(error: String) {
86+
Log.d(TAG, "QueryInfo failed with error: $error")
87+
// TODO: Handle error.
88+
}
89+
},
90+
)
91+
// [END signal_request_banner]
92+
}
93+
94+
companion object {
95+
private const val TAG = "AdmobSCARSnippets"
96+
}
97+
}

0 commit comments

Comments
 (0)