Apply incentive

Java

 // Copyright 2026 Google LLC 
 // 
 // Licensed under the Apache License, Version 2.0 (the "License"); 
 // you may not use this file except in compliance with the License. 
 // You may obtain a copy of the License at 
 // 
 //     https://www.apache.org/licenses/LICENSE-2.0 
 // 
 // Unless required by applicable law or agreed to in writing, software 
 // distributed under the License is distributed on an "AS IS" BASIS, 
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 // See the License for the specific language governing permissions and 
 // limitations under the License. 
 package 
  
 com.google.ads.googleads.examples.incentives 
 ; 
 import 
  
 com.beust.jcommander.Parameter 
 ; 
 import 
  
 com.google.ads.googleads.examples.utils.ArgumentNames 
 ; 
 import 
  
 com.google.ads.googleads.examples.utils.CodeSampleParams 
 ; 
 import 
  
 com.google.ads.googleads.lib.GoogleAdsClient 
 ; 
 import 
  
 com.google.ads.googleads.v24.errors.GoogleAdsError 
 ; 
 import 
  
 com.google.ads.googleads.v24.errors.GoogleAdsException 
 ; 
 import 
  
 com.google.ads.googleads.v24.services.ApplyIncentiveRequest 
 ; 
 import 
  
 com.google.ads.googleads.v24.services.ApplyIncentiveResponse 
 ; 
 import 
  
 com.google.ads.googleads.v24.services.IncentiveServiceClient 
 ; 
 import 
  
 java.io.FileNotFoundException 
 ; 
 import 
  
 java.io.IOException 
 ; 
 /** 
 * This example applies an incentive to a user's account. 
 * 
 * <p>This example is a no-op if the user already has an accepted incentive. If the user attempts to 
 * apply a new incentive, the response will simply return the existing incentive that has already 
 * been applied to the account. 
 */ 
 public 
  
 class 
 ApplyIncentive 
  
 { 
  
 private 
  
 static 
  
 class 
 ApplyIncentiveParams 
  
 extends 
  
 CodeSampleParams 
  
 { 
  
 @Parameter 
 ( 
  
 names 
  
 = 
  
 ArgumentNames 
 . 
 CUSTOMER_ID 
 , 
  
 required 
  
 = 
  
 true 
 , 
  
 description 
  
 = 
  
 "The Google Ads customer ID." 
 ) 
  
 private 
  
 Long 
  
 customerId 
 ; 
  
 @Parameter 
 ( 
  
 names 
  
 = 
  
 ArgumentNames 
 . 
 INCENTIVE_ID 
 , 
  
 required 
  
 = 
  
 true 
 , 
  
 description 
  
 = 
  
 "The ID of the incentive to apply." 
 ) 
  
 private 
  
 Long 
  
 incentiveId 
 ; 
  
 @Parameter 
 ( 
  
 names 
  
 = 
  
 ArgumentNames 
 . 
 COUNTRY_CODE 
 , 
  
 required 
  
 = 
  
 true 
 , 
  
 description 
  
 = 
  
 "The country for the incentive to apply." 
 ) 
  
 private 
  
 String 
  
 countryCode 
  
 = 
  
 "US" 
 ; 
  
 } 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 IOException 
  
 { 
  
 ApplyIncentiveParams 
  
 params 
  
 = 
  
 new 
  
 ApplyIncentiveParams 
 (); 
  
 if 
  
 ( 
 ! 
 params 
 . 
 parseArguments 
 ( 
 args 
 )) 
  
 { 
  
 // Either pass the required parameters for this example on the command line, or insert them 
  
 // into the code here. See the parameter class definition above for more information. 
  
 params 
 . 
 customerId 
  
 = 
  
 Long 
 . 
 parseLong 
 ( 
 "INSERT_CUSTOMER_ID_HERE" 
 ); 
  
 params 
 . 
 incentiveId 
  
 = 
  
 Long 
 . 
 parseLong 
 ( 
 "INSERT_INCENTIVE_ID_HERE" 
 ); 
  
 } 
  
 GoogleAdsClient 
  
 googleAdsClient 
  
 = 
  
 null 
 ; 
  
 try 
  
 { 
  
 googleAdsClient 
  
 = 
  
 GoogleAdsClient 
 . 
 newBuilder 
 (). 
 fromPropertiesFile 
 (). 
 build 
 (); 
  
 } 
  
 catch 
  
 ( 
 FileNotFoundException 
  
 fnfe 
 ) 
  
 { 
  
 System 
 . 
 err 
 . 
 printf 
 ( 
 "Failed to load GoogleAdsClient configuration from file. Exception: %s%n" 
 , 
  
 fnfe 
 ); 
  
 System 
 . 
 exit 
 ( 
 1 
 ); 
  
 } 
  
 catch 
  
 ( 
 IOException 
  
 ioe 
 ) 
  
 { 
  
 System 
 . 
 err 
 . 
 printf 
 ( 
 "Failed to create GoogleAdsClient. Exception: %s%n" 
 , 
  
 ioe 
 ); 
  
 System 
 . 
 exit 
 ( 
 1 
 ); 
  
 } 
  
 try 
  
 { 
  
 new 
  
 ApplyIncentive 
 (). 
 runExample 
 ( 
 googleAdsClient 
 , 
  
 params 
 . 
 customerId 
 , 
  
 params 
 . 
 incentiveId 
 , 
  
 params 
 . 
 countryCode 
 ); 
  
 } 
  
 catch 
  
 ( 
 GoogleAdsException 
  
 gae 
 ) 
  
 { 
  
 // GoogleAdsException is the base class for most exceptions thrown by an API request. 
  
 // Instances of this exception have a message and a GoogleAdsFailure that contains a 
  
 // collection of GoogleAdsError instances that detail the underlying causes of the 
  
 // exception. 
  
 System 
 . 
 err 
 . 
 printf 
 ( 
 "Request ID %s failed due to GoogleAdsException. Underlying errors:%n" 
 , 
  
 gae 
 . 
 getRequestId 
 ()); 
  
 int 
  
 i 
  
 = 
  
 0 
 ; 
  
 for 
  
 ( 
 GoogleAdsError 
  
 googleAdsError 
  
 : 
  
 gae 
 . 
 getGoogleAdsFailure 
 (). 
 getErrorsList 
 ()) 
  
 { 
  
 System 
 . 
 err 
 . 
 printf 
 ( 
 "  Error %d: %s%n" 
 , 
  
 i 
 ++ 
 , 
  
 googleAdsError 
 ); 
  
 } 
  
 System 
 . 
 exit 
 ( 
 1 
 ); 
  
 } 
  
 } 
  
 /** 
 * Runs the example. 
 * 
 * @param googleAdsClient the Google Ads API client. 
 * @param customerId the client customer ID. 
 * @param incentiveId the ID of the incentive to apply. 
 * @param countryCode the country of the incentive. 
 */ 
  
 private 
  
 void 
  
 runExample 
 ( 
 GoogleAdsClient 
  
 googleAdsClient 
 , 
  
 long 
  
 customerId 
 , 
  
 long 
  
 incentiveId 
 , 
  
 String 
  
 countryCode 
 ) 
  
 { 
  
 try 
  
 ( 
 IncentiveServiceClient 
  
 incentiveServiceClient 
  
 = 
  
 googleAdsClient 
 . 
 getLatestVersion 
 () 
  
 . 
 createIncentiveServiceClient 
 ()) 
  
 { 
  
 ApplyIncentiveRequest 
  
 request 
  
 = 
  
 ApplyIncentiveRequest 
 . 
 newBuilder 
 () 
  
 . 
 setCustomerId 
 ( 
 String 
 . 
 valueOf 
 ( 
 customerId 
 )) 
  
 . 
 setSelectedIncentiveId 
 ( 
 incentiveId 
 ) 
  
 . 
 setCountryCode 
 ( 
 countryCode 
 ). 
 build 
 (); 
  
 // Issues the request. 
  
 ApplyIncentiveResponse 
  
 response 
  
 = 
  
 incentiveServiceClient 
 . 
 applyIncentive 
 ( 
 request 
 ); 
  
 // Processes the response. 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "====================================================================" 
 ); 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "Applied incentive with coupon code '%s'.%n" 
 , 
  
 response 
 . 
 getCouponCode 
 ()); 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "Incentive was created at '%s'.%n" 
 , 
  
 response 
 . 
 getCreationTime 
 ()); 
  
 System 
 . 
 out 
 . 
 println 
 ( 
 "====================================================================" 
 ); 
  
 } 
  
 } 
 } 
  
  

C#

 // Copyright 2026 Google LLC 
 // 
 // Licensed under the Apache License, Version 2.0 (the "License"); 
 // you may not use this file except in compliance with the License. 
 // You may obtain a copy of the License at 
 // 
 //     http://www.apache.org/licenses/LICENSE-2.0 
 // 
 // Unless required by applicable law or agreed to in writing, software 
 // distributed under the License is distributed on an "AS IS" BASIS, 
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 // See the License for the specific language governing permissions and 
 // limitations under the License. 
 using 
  
 CommandLine 
 ; 
 using 
  
 Google.Ads.Gax.Examples 
 ; 
 using 
  
 Google.Ads.Gax.Lib 
 ; 
 using 
  
 Google.Ads.GoogleAds.Config 
 ; 
 using 
  
 Google.Ads.GoogleAds.Extensions.Config 
 ; 
 using 
  
 Google.Ads.GoogleAds.Lib 
 ; 
 using 
  
 Google.Ads.GoogleAds.V23.Errors 
 ; 
 using 
  
 Google.Ads.GoogleAds.V23.Services 
 ; 
 using 
  
 System 
 ; 
 namespace 
  
 Google.Ads.GoogleAds.Examples.V23 
 { 
  
 /// <summary> 
  
 /// This code example applies an incentive to a user's account. 
  
 /// </summary> 
  
 public 
  
 class 
  
 ApplyIncentive 
  
 : 
  
 ExampleBase 
  
 { 
  
 /// <summary> 
  
 /// Command line options for running the <see cref="FetchIncentives"/> example. 
  
 /// </summary> 
  
 public 
  
 class 
  
 Options 
  
 : 
  
 OptionsBase 
  
 { 
  
 /// <summary> 
  
 /// The customer ID for which the call is made. 
  
 /// </summary> 
  
 [Option("customerId", Required = true, HelpText = 
 "The customer ID for which the call is made.")] 
  
 public 
  
 long 
  
 CustomerId 
  
 { 
  
 get 
 ; 
  
 set 
 ; 
  
 } 
  
 /// <summary> 
  
 /// The ID of the incentive to apply. 
  
 /// </summary> 
  
 [Option("incentiveId", Required = true, HelpText = 
 "The ID of the incentive to apply.")] 
  
 public 
  
 long 
  
 IncentiveId 
  
 { 
  
 get 
 ; 
  
 set 
 ; 
  
 } 
  
 /// <summary> 
  
 /// The country for the incentive to apply. 
  
 /// </summary> 
  
 [Option("countryCode", Required = true, HelpText = 
 "The country for the incentive to apply.")] 
  
 public 
  
 string 
  
 CountryCode 
  
 { 
  
 get 
 ; 
  
 set 
 ; 
  
 } 
  
 } 
  
 /// <summary> 
  
 /// Main method, to run this code example as a standalone application. 
  
 /// </summary> 
  
 /// <param name="args">The command line arguments.</param> 
  
 public 
  
 static 
  
 void 
  
 Main 
 ( 
 string 
 [] 
  
 args 
 ) 
  
 { 
  
 Options 
  
 options 
  
 = 
  
 ExampleUtilities 
 . 
 ParseCommandLine<Options> 
 ( 
 args 
 ); 
  
 ApplyIncentive 
  
 codeExample 
  
 = 
  
 new 
  
 ApplyIncentive 
 (); 
  
 Console 
 . 
 WriteLine 
 ( 
 codeExample 
 . 
 Description 
 ); 
  
  
 codeExample 
 . 
 Run 
 ( 
 new 
  
 GoogleAdsClient 
 (), 
  
 options 
 . 
 CustomerId 
 , 
  
 options 
 . 
 IncentiveId 
 , 
  
 options 
 . 
 CountryCode 
 ); 
  
 } 
  
 /// <summary> 
  
 /// Returns a description about the code example. 
  
 /// </summary> 
  
 public 
  
 override 
  
 string 
  
 Description 
  
 = 
>  
 "This code example applies an incentive to a user's account." 
 ; 
  
 /// <summary> 
  
 /// Runs the code example. 
  
 /// </summary> 
  
 /// <param name="client">The Google Ads client.</param> 
  
 /// <param name="customerId">The client customer ID.</param> 
  
 /// <param name="incentiveId">The ID of the incentive to apply.</param> 
  
 /// <param name="countryCode">The country for the returned incentive.</param> 
  
 public 
  
 void 
  
 Run 
 ( 
 GoogleAdsClient 
  
 client 
 , 
  
 long 
  
 customerId 
 , 
  
 long 
  
 incentiveId 
 , 
  
 string 
  
 countryCode 
 ) 
  
 { 
  
 IncentiveServiceClient 
  
 incentiveService 
  
 = 
  
 client 
 . 
 GetService 
 ( 
  
 Services 
 . 
 V23 
 . 
 IncentiveService 
 ); 
  
 ApplyIncentiveRequest 
  
 request 
  
 = 
  
 new 
  
 ApplyIncentiveRequest 
 () 
  
 { 
  
 CustomerId 
  
 = 
  
 customerId 
 . 
 ToString 
 (), 
  
 SelectedIncentiveId 
  
 = 
  
 incentiveId 
 , 
  
 CountryCode 
  
 = 
  
 countryCode 
  
 }; 
  
 try 
  
 { 
  
 ApplyIncentiveResponse 
  
 response 
  
 = 
  
 incentiveService 
 . 
 ApplyIncentive 
 ( 
 request 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 "===========================================" 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Applied incentive with coupon code: {response.CouponCode}" 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Incentive was created at: {response.CreationTime}" 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 "===========================================" 
 ); 
  
 } 
  
 catch 
  
 ( 
 GoogleAdsException 
  
 e 
 ) 
  
 { 
  
 Console 
 . 
 WriteLine 
 ( 
 "Failure:" 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Message: {e.Message}" 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Failure: {e.Failure}" 
 ); 
  
 Console 
 . 
 WriteLine 
 ( 
 $"Request ID: {e.RequestId}" 
 ); 
  
 throw 
 ; 
  
 } 
  
 } 
  
 } 
 } 
  
  

PHP

This example is not yet available in PHP; you can take a look at the other languages.

Python

 #!/usr/bin/env python 
 # Copyright 2025 Google LLC 
 # 
 # Licensed under the Apache License, Version 2.0 (the "License"); 
 # you may not use this file except in compliance with the License. 
 # You may obtain a copy of the License at 
 # 
 #     https://www.apache.org/licenses/LICENSE-2.0 
 # 
 # Unless required by applicable law or agreed to in writing, software 
 # distributed under the License is distributed on an "AS IS" BASIS, 
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 # See the License for the specific language governing permissions and 
 # limitations under the License. 
 """This example applies an incentive to a user's account. 
 This example is a no-op if the user already has an accepted incentive. If the 
 user attempts to apply a new incentive, the response will simply return the 
 existing incentive that has already been applied to the account. Use the 
 fetch_incentives.py example to get the available incentives. 
 """ 
 import 
  
 argparse 
 import 
  
 sys 
 from 
  
 google.ads.googleads.client 
  
 import 
 GoogleAdsClient 
 from 
  
 google.ads.googleads.errors 
  
 import 
 GoogleAdsException 
 from 
  
 google.ads.googleads.v24.services 
  
 import 
 ( 
 ApplyIncentiveRequest 
 , 
 ApplyIncentiveResponse 
 , 
 ) 
 from 
  
 google.ads.googleads.v24.services.services.incentive_service.client 
  
 import 
 ( 
 IncentiveServiceClient 
 , 
 ) 
 def 
  
 main 
 ( 
 client 
 : 
 GoogleAdsClient 
 , 
 customer_id 
 : 
 str 
 , 
 incentive_id 
 : 
 str 
 , 
 country_code 
 : 
 str 
 , 
 ) 
 - 
> None 
 : 
  
 """Applies an incentive for the ads customer. 
 Args: 
 client: An initialized GoogleAdsClient instance. 
 customer_id: The client customer ID. 
 country_code: The country code of the user. 
 incentive_id: The incentive ID to select. 
 """ 
 incentive_service 
 : 
 IncentiveServiceClient 
 = 
 client 
 . 
 get_service 
 ( 
 "IncentiveService" 
 ) 
 apply_incentive_request 
 : 
 ApplyIncentiveRequest 
 = 
 client 
 . 
 get_type 
 ( 
 "ApplyIncentiveRequest" 
 ) 
 apply_incentive_request 
 . 
 customer_id 
 = 
 customer_id 
 apply_incentive_request 
 . 
 selected_incentive_id 
 = 
 incentive_id 
 if 
 country_code 
 : 
 apply_incentive_request 
 . 
 country_code 
 = 
 country_code 
 response 
 : 
 ApplyIncentiveResponse 
 = 
 incentive_service 
 . 
 apply_incentive 
 ( 
 request 
 = 
 apply_incentive_request 
 ) 
 print 
 ( 
 "Applied incentive." 
 ) 
 print 
 ( 
 f 
 "Coupon Code: 
 { 
 response 
 . 
 coupon_code 
 } 
 " 
 ) 
 print 
 ( 
 f 
 "Creation Time: 
 { 
 response 
 . 
 creation_time 
 } 
 " 
 ) 
 if 
 __name__ 
 == 
 "__main__" 
 : 
 parser 
 = 
 argparse 
 . 
 ArgumentParser 
 ( 
 description 
 = 
 "Applies an incentive for the ads customer." 
 ) 
 # The following argument(s) should be provided to run the example. 
 parser 
 . 
 add_argument 
 ( 
 "-c" 
 , 
 "--customer_id" 
 , 
 type 
 = 
 str 
 , 
 required 
 = 
 True 
 , 
 help 
 = 
 "The Google Ads customer ID." 
 , 
 ) 
 parser 
 . 
 add_argument 
 ( 
 "-i" 
 , 
 "--incentive_id" 
 , 
 type 
 = 
 int 
 , 
 required 
 = 
 True 
 , 
 help 
 = 
 "The incentive ID to select." 
 , 
 ) 
 parser 
 . 
 add_argument 
 ( 
 "-k" 
 , 
 "--country_code" 
 , 
 type 
 = 
 str 
 , 
 required 
 = 
 True 
 , 
 help 
 = 
 "The country code of the user (e.g. 'US')." 
 , 
 ) 
 args 
 = 
 parser 
 . 
 parse_args 
 () 
 # GoogleAdsClient will read the google-ads.yaml configuration file in the 
 # home directory if none is specified. 
 googleads_client 
 = 
 GoogleAdsClient 
 . 
 load_from_storage 
 ( 
 version 
 = 
 "v24" 
 ) 
 try 
 : 
 main 
 ( 
 googleads_client 
 , 
 args 
 . 
 customer_id 
 , 
 args 
 . 
 incentive_id 
 , 
 args 
 . 
 country_code 
 , 
 ) 
 except 
 GoogleAdsException 
 as 
 ex 
 : 
 print 
 ( 
 f 
 'Request with ID " 
 { 
 ex 
 . 
 request_id 
 } 
 " failed with status ' 
 f 
 '" 
 { 
 ex 
 . 
 error 
 . 
 code 
 () 
 . 
 name 
 } 
 " and includes the following errors:' 
 ) 
 for 
 error 
 in 
 ex 
 . 
 failure 
 . 
 errors 
 : 
 print 
 ( 
 f 
 ' 
 \t 
 Error with message " 
 { 
 error 
 . 
 message 
 } 
 ".' 
 ) 
 if 
 error 
 . 
 location 
 : 
 for 
 field_path_element 
 in 
 error 
 . 
 location 
 . 
 field_path_elements 
 : 
 print 
 ( 
 f 
 " 
 \t\t 
 On field: 
 { 
 field_path_element 
 . 
 field_name 
 } 
 " 
 ) 
 sys 
 . 
 exit 
 ( 
 1 
 ) 
  

Ruby

This example is not yet available in Ruby; you can take a look at the other languages.

Perl

This example is not yet available in Perl; you can take a look at the other languages.

curl

Create a Mobile Website
View Site in Mobile | Classic
Share by: