1818import com .google .ads .googleads .examples .utils .ArgumentNames ;
1919import com .google .ads .googleads .examples .utils .CodeSampleParams ;
2020import com .google .ads .googleads .lib .GoogleAdsClient ;
21- import com .google .ads .googleads .v11 .common .ExpandedTextAdInfo ;
21+ import com .google .ads .googleads .v11 .common .AdTextAsset ;
2222import com .google .ads .googleads .v11 .common .PolicyTopicEntry ;
23+ import com .google .ads .googleads .v11 .common .ResponsiveSearchAdInfo ;
2324import com .google .ads .googleads .v11 .enums .AdGroupAdStatusEnum .AdGroupAdStatus ;
25+ import com .google .ads .googleads .v11 .enums .ServedAssetFieldTypeEnum .ServedAssetFieldType ;
2426import com .google .ads .googleads .v11 .errors .GoogleAdsError ;
2527import com .google .ads .googleads .v11 .errors .GoogleAdsException ;
2628import com .google .ads .googleads .v11 .errors .PolicyFindingErrorEnum .PolicyFindingError ;
3133import com .google .ads .googleads .v11 .services .MutateAdGroupAdsRequest ;
3234import com .google .ads .googleads .v11 .services .MutateAdGroupAdsResponse ;
3335import com .google .ads .googleads .v11 .utils .ResourceNames ;
36+ import com .google .common .collect .ImmutableList ;
3437import java .io .FileNotFoundException ;
3538import java .io .IOException ;
3639import java .util .List ;
3740import java .util .stream .Collectors ;
3841
3942/**
40- * Shows how to use the validateOnly header to validate an expanded text ad. No ads will be created,
41- * but exceptions will still be thrown.
43+ * Shows how to use the validateOnly header to validate a responsive search ad. No ads will be
44+ * created, but exceptions will still be thrown.
4245 */
43- public class ValidateTextAd {
46+ public class ValidateAd {
4447
45- private static class ValidateTextAdParams extends CodeSampleParams {
48+ private static class ValidateAdParams extends CodeSampleParams {
4649
4750 @ Parameter (names = ArgumentNames .CUSTOMER_ID , required = true )
4851 private Long customerId ;
@@ -52,7 +55,7 @@ private static class ValidateTextAdParams extends CodeSampleParams {
5255 }
5356
5457 public static void main (String [] args ) {
55- ValidateTextAdParams params = new ValidateTextAdParams ();
58+ ValidateAdParams params = new ValidateAdParams ();
5659 if (!params .parseArguments (args )) {
5760
5861 // Either pass the required parameters for this example on the command line, or insert them
@@ -74,7 +77,7 @@ public static void main(String[] args) {
7477 }
7578
7679 try {
77- new ValidateTextAd ().runExample (googleAdsClient , params .customerId , params .adGroupId );
80+ new ValidateAd ().runExample (googleAdsClient , params .customerId , params .adGroupId );
7881 } catch (GoogleAdsException gae ) {
7982 // GoogleAdsException is the base class for most exceptions thrown by an API request.
8083 // Instances of this exception have a message and a GoogleAdsFailure that contains a
@@ -102,20 +105,28 @@ public static void main(String[] args) {
102105 private void runExample (GoogleAdsClient googleAdsClient , long customerId , long adGroupId ) {
103106 String adGroupResourceName = ResourceNames .adGroup (customerId , adGroupId );
104107
105- // Creates the expanded text ad info.
106- ExpandedTextAdInfo expandedTextAdInfo =
107- ExpandedTextAdInfo .newBuilder ()
108- .setDescription ("Luxury Cruise to Mars" )
109- .setHeadlinePart1 ("Visit the Red Planet in style" )
110- // Adds a headline that will trigger a policy violation to demonstrate error handling.
111- .setHeadlinePart2 ("Low-gravity fun for everyone!!" )
108+ // Creates the responsive search ad info.
109+ ResponsiveSearchAdInfo responsiveSearchAdInfo =
110+ ResponsiveSearchAdInfo .newBuilder ()
111+ .addAllHeadlines (
112+ ImmutableList .of (
113+ AdTextAsset .newBuilder ()
114+ .setText ("Visit the Red Planet in style." )
115+ .setPinnedField (ServedAssetFieldType .HEADLINE_1 )
116+ .build (),
117+ AdTextAsset .newBuilder ().setText ("Low-gravity fun for everyone!" ).build (),
118+ AdTextAsset .newBuilder ().setText ("Book your Cruise to Mars now!" ).build ()))
119+ .addAllDescriptions (
120+ ImmutableList .of (
121+ AdTextAsset .newBuilder ().setText ("Luxury Cruise to Mars" ).build (),
122+ AdTextAsset .newBuilder ().setText ("Book your ticket now" ).build ()))
112123 .build ();
113124
114125 // Wraps the info in an Ad object.
115126 Ad ad =
116127 Ad .newBuilder ()
117- .setExpandedTextAd ( expandedTextAdInfo )
118- .addFinalUrls ("http ://www.example.com" )
128+ .setResponsiveSearchAd ( responsiveSearchAdInfo )
129+ .addFinalUrls ("https ://www.example.com" )
119130 .build ();
120131
121132 // Builds the final ad group ad representation.
@@ -139,25 +150,26 @@ private void runExample(GoogleAdsClient googleAdsClient, long customerId, long a
139150 .addOperations (operation )
140151 .setValidateOnly (true )
141152 .build ());
142- // Since validation is ON, result will be null .
143- System .out .println ("Expanded text ad validated successfully." );
153+ // This line will not be executed since the ad will fail validation .
154+ System .out .println ("Responsive search ad validated successfully." );
144155 } catch (GoogleAdsException e ) {
145156 // This block will be hit if there is a validation error from the server.
146- System .out .println ("There were validation error(s) while adding expanded text ad." );
147-
148- if ( e . getGoogleAdsFailure () != null ) {
149- // Note: Policy violation errors are returned as PolicyFindingErrors. See
150- // https://developers.google.com/google-ads/api/docs/policy-exemption/overview
151- // for additional details.
152- List < GoogleAdsError > policyFindingErrors =
153- e . getGoogleAdsFailure (). getErrorsList (). stream ()
154- . filter (
155- err ->
156- err . getErrorCode (). getPolicyFindingError ( )
157- == PolicyFindingError . POLICY_FINDING )
158- . collect ( Collectors . toList ());
159- int count = 1 ;
157+ System .out .println ("There were validation error(s) while adding responsive search ad." );
158+
159+ // Note: Policy violation errors are returned as PolicyFindingErrors. See
160+ // https://developers.google.com/google-ads/api/docs/policy-exemption/overview
161+ // for additional details.
162+ List < GoogleAdsError > policyFindingErrors =
163+ e . getGoogleAdsFailure (). getErrorsList (). stream ()
164+ . filter (
165+ err ->
166+ err . getErrorCode (). getPolicyFindingError ()
167+ == PolicyFindingError . POLICY_FINDING )
168+ . collect ( Collectors . toList ());
169+
170+ if (! policyFindingErrors . isEmpty ()) {
160171 for (GoogleAdsError policyFindingError : policyFindingErrors ) {
172+ int count = 1 ;
161173 if (policyFindingError .getDetails ().hasPolicyFindingDetails ()) {
162174 List <PolicyTopicEntry > policyTopicEntries =
163175 policyFindingError
@@ -166,7 +178,7 @@ private void runExample(GoogleAdsClient googleAdsClient, long customerId, long a
166178 .getPolicyTopicEntriesList ();
167179 for (PolicyTopicEntry policyTopicEntry : policyTopicEntries ) {
168180 System .out .printf (
169- "%d Policy topic entry with topic '%s' and type '%s' was found.%n" ,
181+ "%d Policy topic entries with topic '%s' and type '%s' found.%n" ,
170182 count , policyTopicEntry .getTopic (), policyTopicEntry .getType ());
171183 }
172184 count ++;
0 commit comments