Skip to content

Commit 7d11fe4

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added snippets for server to server requests.
PiperOrigin-RevId: 805508810
1 parent 0a4f6f7 commit 7d11fe4

File tree

2 files changed

+250
-0
lines changed

2 files changed

+250
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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.AdSize;
24+
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
25+
import com.google.android.gms.ads.query.QueryInfo;
26+
import com.google.android.gms.ads.query.QueryInfoGenerationCallback;
27+
28+
/** Java code snippets for the developer guide. */
29+
public class AdManagerSCARSnippets {
30+
31+
private static final String TAG = "AdManagerSCARSnippets";
32+
33+
public void loadNative(Context applicationContext, String adUnitID) {
34+
// [START signal_request_native]
35+
// Specify the "query_info_type" as "requester_type_8" to
36+
// denote that the usage of QueryInfo is for Ad Manager S2S.
37+
Bundle extras = new Bundle();
38+
extras.putString("query_info_type", "requester_type_8");
39+
40+
// Create a signal request for an ad.
41+
AdManagerAdRequest signalRequest =
42+
new AdManagerAdRequest.Builder()
43+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
44+
.setRequestAgent("REQUEST_AGENT")
45+
.build();
46+
47+
// Generate and send the signal request.
48+
QueryInfo.generate(
49+
applicationContext,
50+
AdFormat.NATIVE,
51+
signalRequest,
52+
adUnitID,
53+
new QueryInfoGenerationCallback() {
54+
@Override
55+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
56+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
57+
// TODO: Fetch the ad response using your generated query info.
58+
}
59+
60+
@Override
61+
public void onFailure(@NonNull String error) {
62+
Log.d(TAG, "QueryInfo failed with error: " + error);
63+
// TODO: Handle error.
64+
}
65+
});
66+
// [END signal_request_native]
67+
}
68+
69+
public void loadBanner(Context applicationContext, String adUnitID) {
70+
// [START signal_request_banner]
71+
// Specify the "query_info_type" as "requester_type_8" to
72+
// denote that the usage of QueryInfo is for Ad Manager S2S.
73+
Bundle extras = new Bundle();
74+
extras.putString("query_info_type", "requester_type_8");
75+
76+
// Set the adaptive banner size.
77+
// Refer to the AdSize class for available ad sizes.
78+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320);
79+
extras.putInt("adaptive_banner_w", size.getWidth());
80+
extras.putInt("adaptive_banner_h", size.getHeight());
81+
82+
// Create a signal request for an ad.
83+
AdManagerAdRequest signalRequest =
84+
new AdManagerAdRequest.Builder()
85+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
86+
.setRequestAgent("REQUEST_AGENT")
87+
.build();
88+
89+
// Generate and send the signal request.
90+
QueryInfo.generate(
91+
applicationContext,
92+
AdFormat.BANNER,
93+
signalRequest,
94+
adUnitID,
95+
new QueryInfoGenerationCallback() {
96+
@Override
97+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
98+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
99+
// TODO: Fetch the ad response using your generated query info.
100+
}
101+
102+
@Override
103+
public void onFailure(@NonNull String error) {
104+
Log.d(TAG, "QueryInfo failed with error: " + error);
105+
// TODO: Handle error.
106+
}
107+
});
108+
// [END signal_request_banner]
109+
}
110+
111+
public void loadNativePlusBanner(Context applicationContext, String adUnitID) {
112+
// [START signal_request_native_plus_banner]
113+
// Specify the "query_info_type" as "requester_type_8" to
114+
// denote that the usage of QueryInfo is for Ad Manager S2S.
115+
Bundle extras = new Bundle();
116+
extras.putString("query_info_type", "requester_type_8");
117+
118+
// Set the adaptive banner size.
119+
// Refer to the AdSize class for available ad sizes.
120+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320);
121+
extras.putInt("adaptive_banner_w", size.getWidth());
122+
extras.putInt("adaptive_banner_h", size.getHeight());
123+
124+
// Create a signal request for an ad.
125+
AdManagerAdRequest signalRequest =
126+
new AdManagerAdRequest.Builder()
127+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
128+
.setRequestAgent("REQUEST_AGENT")
129+
.build();
130+
131+
// Generate and send the signal request.
132+
QueryInfo.generate(
133+
applicationContext,
134+
AdFormat.NATIVE,
135+
signalRequest,
136+
adUnitID,
137+
new QueryInfoGenerationCallback() {
138+
@Override
139+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
140+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
141+
// TODO: Fetch the ad response using your generated query info.
142+
}
143+
144+
@Override
145+
public void onFailure(@NonNull String error) {
146+
Log.d(TAG, "QueryInfo failed with error: " + error);
147+
// TODO: Handle error.
148+
}
149+
});
150+
// [END signal_request_native_plus_banner]
151+
}
152+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.AdSize;
24+
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
25+
import com.google.android.gms.ads.query.QueryInfo;
26+
import com.google.android.gms.ads.query.QueryInfoGenerationCallback;
27+
28+
/** Java code snippets for the developer guide. */
29+
public class AdMobSCARSnippets {
30+
31+
private static final String TAG = "AdmobSCARSnippets";
32+
33+
public void loadNative(Context applicationContext, String adUnitID) {
34+
// [START signal_request_native]
35+
// Create a signal request for an ad.
36+
AdManagerAdRequest signalRequest =
37+
new AdManagerAdRequest.Builder().setRequestAgent("REQUEST_AGENT").build();
38+
39+
// Generate and send the signal request.
40+
QueryInfo.generate(
41+
applicationContext,
42+
AdFormat.NATIVE,
43+
signalRequest,
44+
adUnitID,
45+
new QueryInfoGenerationCallback() {
46+
@Override
47+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
48+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
49+
// TODO: Fetch the ad response using your generated query info.
50+
}
51+
52+
@Override
53+
public void onFailure(@NonNull String error) {
54+
Log.d(TAG, "QueryInfo failed with error: " + error);
55+
// TODO: Handle error.
56+
}
57+
});
58+
// [END signal_request_native]
59+
}
60+
61+
public void loadBanner(Context applicationContext, String adUnitID) {
62+
// [START signal_request_banner]
63+
// Set the adaptive banner size.
64+
// Refer to the AdSize class for available ad sizes.
65+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320);
66+
Bundle extras = new Bundle();
67+
extras.putInt("adaptive_banner_w", size.getWidth());
68+
extras.putInt("adaptive_banner_h", size.getHeight());
69+
70+
// Create a signal request for an ad.
71+
AdManagerAdRequest signalRequest =
72+
new AdManagerAdRequest.Builder()
73+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
74+
.setRequestAgent("REQUEST_AGENT")
75+
.build();
76+
77+
// Generate and send the signal request.
78+
QueryInfo.generate(
79+
applicationContext,
80+
AdFormat.BANNER,
81+
signalRequest,
82+
adUnitID,
83+
new QueryInfoGenerationCallback() {
84+
@Override
85+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
86+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
87+
// TODO: Fetch the ad response using your generated query info.
88+
}
89+
90+
@Override
91+
public void onFailure(@NonNull String error) {
92+
Log.d(TAG, "QueryInfo failed with error: " + error);
93+
// TODO: Handle error.
94+
}
95+
});
96+
// [END signal_request_banner]
97+
}
98+
}

0 commit comments

Comments
 (0)