Skip to content

Commit 4f6db13

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added snippets for server to server requests.
PiperOrigin-RevId: 805096286
1 parent 981f415 commit 4f6db13

File tree

4 files changed

+867
-0
lines changed

4 files changed

+867
-0
lines changed
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
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+
15+
package com.google.android.gms.snippets;
16+
17+
import android.content.Context;
18+
import android.os.Bundle;
19+
import android.util.Log;
20+
import androidx.annotation.NonNull;
21+
import com.google.ads.mediation.admob.AdMobAdapter;
22+
import com.google.android.gms.ads.AdFormat;
23+
import com.google.android.gms.ads.AdLoader;
24+
import com.google.android.gms.ads.AdSize;
25+
import com.google.android.gms.ads.VideoOptions;
26+
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
27+
import com.google.android.gms.ads.admanager.AdManagerAdView;
28+
import com.google.android.gms.ads.nativead.NativeAd;
29+
import com.google.android.gms.ads.nativead.NativeAdOptions;
30+
import com.google.android.gms.ads.query.QueryInfo;
31+
import com.google.android.gms.ads.query.QueryInfoGenerationCallback;
32+
33+
/** Java code snippets for the developer guide. */
34+
public class AdManagerSCARSnippets {
35+
36+
private static final String TAG = "AdManagerSCARSnippets";
37+
38+
public void loadNative(Context context, String adUnitId) {
39+
// [START signal_request_native]
40+
// Specify the "query_info_type" as "requester_type_8" to
41+
// denote that the usage of QueryInfo is for Ad Manager S2S.
42+
Bundle extras = new Bundle();
43+
extras.putString("query_info_type", "requester_type_8");
44+
45+
// Create a signal request for an ad.
46+
AdManagerAdRequest signalRequest =
47+
new AdManagerAdRequest.Builder()
48+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
49+
.setRequestAgent("REQUEST_AGENT")
50+
.build();
51+
52+
// Generate and send the signal request.
53+
QueryInfo.generate(
54+
context,
55+
AdFormat.NATIVE,
56+
signalRequest,
57+
adUnitId,
58+
new QueryInfoGenerationCallback() {
59+
@Override
60+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
61+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
62+
// Fetch the ad response using your generated query info.
63+
String adResponseString = fetchSignalResponse(queryInfo);
64+
}
65+
66+
@Override
67+
public void onFailure(@NonNull String error) {
68+
Log.d(TAG, "QueryInfo failed with error: " + error);
69+
// TODO: Handle error.
70+
}
71+
});
72+
// [END signal_request_native]
73+
}
74+
75+
public void loadBanner(Context context, String adUnitId) {
76+
// [START signal_request_banner]
77+
// Specify the "query_info_type" as "requester_type_8" to
78+
// denote that the usage of QueryInfo is for Ad Manager S2S.
79+
Bundle extras = new Bundle();
80+
extras.putString("query_info_type", "requester_type_8");
81+
82+
// Set the adaptive banner size.
83+
// Refer to the AdSize class for available ad sizes.
84+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(context, 320);
85+
extras.putInt("adaptive_banner_w", size.getWidth());
86+
extras.putInt("adaptive_banner_h", size.getHeight());
87+
88+
// Create a signal request for an ad.
89+
AdManagerAdRequest signalRequest =
90+
new AdManagerAdRequest.Builder()
91+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
92+
.setRequestAgent("REQUEST_AGENT")
93+
.build();
94+
95+
// Generate and send the signal request.
96+
QueryInfo.generate(
97+
context,
98+
AdFormat.BANNER,
99+
signalRequest,
100+
adUnitId,
101+
new QueryInfoGenerationCallback() {
102+
@Override
103+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
104+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
105+
// Fetch the ad response using your generated query info.
106+
String adResponseString = fetchSignalResponse(queryInfo);
107+
}
108+
109+
@Override
110+
public void onFailure(@NonNull String error) {
111+
Log.d(TAG, "QueryInfo failed with error: " + error);
112+
// TODO: Handle error.
113+
}
114+
});
115+
// [END signal_request_banner]
116+
}
117+
118+
public void loadNativePlusBanner(Context context, String adUnitId) {
119+
// [START signal_request_native_plus_banner]
120+
// Specify the "query_info_type" as "requester_type_8" to
121+
// denote that the usage of QueryInfo is for Ad Manager S2S.
122+
Bundle extras = new Bundle();
123+
extras.putString("query_info_type", "requester_type_8");
124+
125+
// Set the adaptive banner size.
126+
// Refer to the AdSize class for available ad sizes.
127+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(context, 320);
128+
extras.putInt("adaptive_banner_w", size.getWidth());
129+
extras.putInt("adaptive_banner_h", size.getHeight());
130+
131+
// Create a signal request for an ad.
132+
AdManagerAdRequest signalRequest =
133+
new AdManagerAdRequest.Builder()
134+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
135+
.setRequestAgent("REQUEST_AGENT")
136+
.build();
137+
138+
// Generate and send the signal request.
139+
QueryInfo.generate(
140+
context,
141+
AdFormat.NATIVE,
142+
signalRequest,
143+
adUnitId,
144+
new QueryInfoGenerationCallback() {
145+
@Override
146+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
147+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
148+
// Fetch the ad response using your generated query info.
149+
String adResponseString = fetchSignalResponse(queryInfo);
150+
}
151+
152+
@Override
153+
public void onFailure(@NonNull String error) {
154+
Log.d(TAG, "QueryInfo failed with error: " + error);
155+
// TODO: Handle error.
156+
}
157+
});
158+
// [END signal_request_native_plus_banner]
159+
}
160+
161+
public void loadNativeWithOptions(Context context, String adUnitId) {
162+
// [START native_ad_options]
163+
VideoOptions videoOptions = new VideoOptions.Builder().setStartMuted(true).build();
164+
NativeAdOptions adOptions =
165+
new NativeAdOptions.Builder()
166+
.setReturnUrlsForImageAssets(false)
167+
.setRequestMultipleImages(true)
168+
.setVideoOptions(videoOptions)
169+
.build();
170+
171+
// Specify the "query_info_type" as "requester_type_8" to
172+
// denote that the usage of QueryInfo is for Ad Manager S2S.
173+
Bundle extras = new Bundle();
174+
extras.putString("query_info_type", "requester_type_8");
175+
176+
// Create a signal request for an ad.
177+
AdManagerAdRequest signalRequest =
178+
new AdManagerAdRequest.Builder()
179+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
180+
.setRequestAgent("REQUEST_AGENT")
181+
.build();
182+
183+
// Generate and send the signal request.
184+
QueryInfo.generate(
185+
context,
186+
AdFormat.NATIVE,
187+
signalRequest,
188+
adUnitId,
189+
new QueryInfoGenerationCallback() {
190+
@Override
191+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
192+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
193+
// Fetch the ad response using your generated query info.
194+
String adResponseString = fetchSignalResponse(queryInfo);
195+
}
196+
197+
@Override
198+
public void onFailure(@NonNull String error) {
199+
Log.d(TAG, "QueryInfo failed with error: " + error);
200+
// TODO: Handle error.
201+
}
202+
});
203+
// [END native_ad_options]
204+
}
205+
206+
// [START fetch_response]
207+
// This function emulates a request to your ad server.
208+
private String fetchSignalResponse(QueryInfo queryInfo) {
209+
return "adResponseString";
210+
}
211+
212+
// [END fetch_response]
213+
214+
public void renderNative(Context context, String adUnitId, String adResponseString) {
215+
// [START render_native]
216+
AdManagerAdRequest adRequest =
217+
new AdManagerAdRequest.Builder().setAdString(adResponseString).build();
218+
219+
AdLoader adLoader =
220+
new AdLoader.Builder(context, adUnitId)
221+
.forNativeAd(
222+
new NativeAd.OnNativeAdLoadedListener() {
223+
@Override
224+
public void onNativeAdLoaded(NativeAd nativeAd) {
225+
Log.d(TAG, "Native ad rendered.");
226+
// TODO: Show the ad.
227+
}
228+
})
229+
.build();
230+
adLoader.loadAd(adRequest);
231+
// [END render_native]
232+
}
233+
234+
public void renderBanner(Context context, String adUnitId, String adResponseString) {
235+
// [START render_banner]
236+
AdManagerAdRequest adRequest =
237+
new AdManagerAdRequest.Builder().setAdString(adResponseString).build();
238+
239+
AdManagerAdView adView = new AdManagerAdView(context);
240+
adView.setAdUnitId(adUnitId);
241+
adView.setAdSizes(AdSize.BANNER);
242+
adView.loadAd(adRequest);
243+
// [END render_banner]
244+
}
245+
246+
public void renderNativePlusBanner(Context context, String adUnitId, String adResponseString) {
247+
// [START render_native_plus_banner]
248+
AdManagerAdRequest adRequest =
249+
new AdManagerAdRequest.Builder().setAdString(adResponseString).build();
250+
251+
AdLoader adLoader =
252+
new AdLoader.Builder(context, adUnitId)
253+
.forNativeAd(
254+
new NativeAd.OnNativeAdLoadedListener() {
255+
@Override
256+
public void onNativeAdLoaded(NativeAd nativeAd) {
257+
Log.d(TAG, "Native ad rendered.");
258+
// TODO: Show the ad.
259+
}
260+
})
261+
.build();
262+
adLoader.loadAd(adRequest);
263+
// [END render_native_plus_banner]
264+
}
265+
}

0 commit comments

Comments
 (0)