Skip to content

Commit df9bb55

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added snippets for server to server requests.
PiperOrigin-RevId: 805096286
1 parent 3fc22a8 commit df9bb55

File tree

4 files changed

+568
-0
lines changed

4 files changed

+568
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320);
78+
extras.putInt("adaptive_banner_w", size.getWidth());
79+
extras.putInt("adaptive_banner_h", size.getHeight());
80+
81+
// Create a signal request for an ad.
82+
AdManagerAdRequest signalRequest =
83+
new AdManagerAdRequest.Builder()
84+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
85+
.setRequestAgent("request_agent")
86+
.build();
87+
88+
// Generate and send the signal request.
89+
QueryInfo.generate(
90+
applicationContext,
91+
AdFormat.BANNER,
92+
signalRequest,
93+
adUnitID,
94+
new QueryInfoGenerationCallback() {
95+
@Override
96+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
97+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
98+
// TODO: Fetch the ad response using your generated query info.
99+
}
100+
101+
@Override
102+
public void onFailure(@NonNull String error) {
103+
Log.d(TAG, "QueryInfo failed with error: " + error);
104+
// TODO: Handle error.
105+
}
106+
});
107+
// [END signal_request_banner]
108+
}
109+
110+
public void loadNativePlusBanner(Context applicationContext, String adUnitID) {
111+
// [START signal_request_native_plus_banner]
112+
// Specify the "query_info_type" as "requester_type_8" to
113+
// denote that the usage of QueryInfo is for Ad Manager S2S.
114+
Bundle extras = new Bundle();
115+
extras.putString("query_info_type", "requester_type_8");
116+
117+
// Set the adaptive banner size.
118+
AdSize size = 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+
AdManagerAdRequest signalRequest =
124+
new AdManagerAdRequest.Builder()
125+
.addNetworkExtrasBundle(AdMobAdapter.class, 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+
new QueryInfoGenerationCallback() {
136+
@Override
137+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
138+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
139+
// TODO: Fetch the ad response using your generated query info.
140+
}
141+
142+
@Override
143+
public void onFailure(@NonNull String error) {
144+
Log.d(TAG, "QueryInfo failed with error: " + error);
145+
// TODO: Handle error.
146+
}
147+
});
148+
// [END signal_request_native_plus_banner]
149+
}
150+
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320);
65+
Bundle extras = new Bundle();
66+
extras.putInt("adaptive_banner_w", size.getWidth());
67+
extras.putInt("adaptive_banner_h", size.getHeight());
68+
69+
// Create a signal request for an ad.
70+
AdManagerAdRequest signalRequest =
71+
new AdManagerAdRequest.Builder()
72+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
73+
.setRequestAgent("request_agent")
74+
.build();
75+
76+
// Generate and send the signal request.
77+
QueryInfo.generate(
78+
applicationContext,
79+
AdFormat.BANNER,
80+
signalRequest,
81+
adUnitID,
82+
new QueryInfoGenerationCallback() {
83+
@Override
84+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
85+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
86+
// TODO: Fetch the ad response using your generated query info.
87+
}
88+
89+
@Override
90+
public void onFailure(@NonNull String error) {
91+
Log.d(TAG, "QueryInfo failed with error: " + error);
92+
// TODO: Handle error.
93+
}
94+
});
95+
// [END signal_request_banner]
96+
}
97+
98+
public void loadNativePlusBanner(Context applicationContext, String adUnitID) {
99+
// [START signal_request_native_plus_banner]
100+
// Set the adaptive banner size.
101+
AdSize size = AdSize.getCurrentOrientationInlineAdaptiveBannerAdSize(applicationContext, 320);
102+
Bundle extras = new Bundle();
103+
extras.putInt("adaptive_banner_w", size.getWidth());
104+
extras.putInt("adaptive_banner_h", size.getHeight());
105+
106+
// Create a signal request for an ad.
107+
AdManagerAdRequest signalRequest =
108+
new AdManagerAdRequest.Builder()
109+
.addNetworkExtrasBundle(AdMobAdapter.class, extras)
110+
.setRequestAgent("request_agent")
111+
.build();
112+
113+
// Generate and send the signal request.
114+
QueryInfo.generate(
115+
applicationContext,
116+
AdFormat.NATIVE,
117+
signalRequest,
118+
adUnitID,
119+
new QueryInfoGenerationCallback() {
120+
@Override
121+
public void onSuccess(@NonNull final QueryInfo queryInfo) {
122+
Log.d(TAG, "QueryInfo string: " + queryInfo.getQuery());
123+
// TODO: Fetch the ad response using your generated query info.
124+
}
125+
126+
@Override
127+
public void onFailure(@NonNull String error) {
128+
Log.d(TAG, "QueryInfo failed with error: " + error);
129+
// TODO: Handle error.
130+
}
131+
});
132+
// [END signal_request_native_plus_banner]
133+
}
134+
}

0 commit comments

Comments
 (0)