// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService =
adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create text ads.
TextAd textAd = new TextAd();
textAd.setHeadline("Luxury Cruise to Mars");
textAd.setDescription1("Visit the Red Planet in style.");
textAd.setDescription2("Low-gravity fun for everyone!");
textAd.setDisplayUrl("www.example.com");
// Specify a tracking url for 3rd party tracking provider. You may
// specify one at customer, campaign, ad group, ad, criterion or
// feed item levels.
textAd.setTrackingUrlTemplate(
"http://tracker.example.com/?cid={_season}&promocode={_promocode}&u={lpurl}");
// Since your tracking url has two custom parameters, provide their
// values too. This can be provided at campaign, ad group, ad, criterion
// or feed item levels.
CustomParameter seasonParameter = new CustomParameter();
seasonParameter.setKey("season");
seasonParameter.setValue("christmas");
CustomParameter promoCodeParameter = new CustomParameter();
promoCodeParameter.setKey("promocode");
promoCodeParameter.setValue("NYC123");
CustomParameters trackingUrlParameters = new CustomParameters();
trackingUrlParameters.setParameters(
new CustomParameter[] {seasonParameter, promoCodeParameter});
textAd.setUrlCustomParameters(trackingUrlParameters);
// Specify a list of final urls. This field cannot be set if url field is
// set. This may be specified at ad, criterion, and feed item levels.
textAd.setFinalUrls(new String[] {"http://www.example.com/cruise/space/",
"http://www.example.com/locations/mars/"});
// Specify a list of final mobile urls. This field cannot be set if url field is
// set or finalUrls is not set. This may be specified at ad, criterion, and feed
// item levels.
textAd.setFinalMobileUrls(new String[] {"http://mobile.example.com/cruise/space/",
"http://mobile.example.com/locations/mars/"});
// Create ad group ad.
AdGroupAd textAdGroupAd = new AdGroupAd();
textAdGroupAd.setAdGroupId(adGroupId);