use of com.google.api.ads.adwords.axis.v201809.cm.ServedAssetFieldType in project googleads-java-lib by googleads.
the class AddResponsiveSearchAd method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group where the ad will be created.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long adGroupId) throws RemoteException {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
List<AdGroupAdOperation> operations = new ArrayList<>();
ResponsiveSearchAd responsiveSearchAd = new ResponsiveSearchAd();
List<AssetLink> headlines = new ArrayList<>();
AssetLink pinnedHeadline = createAssetLinkForText("Cruise to Mars #" + System.currentTimeMillis());
// Set a pinning to always choose this asset for HEADLINE_1. Pinning is optional; if no pinning
// is set, then headlines and descriptions will be rotated and the ones that perform best will
// be used more often.
pinnedHeadline.setPinnedField(ServedAssetFieldType.HEADLINE_1);
headlines.add(pinnedHeadline);
headlines.add(createAssetLinkForText("Best Space Cruise Line"));
headlines.add(createAssetLinkForText("Experience the Stars"));
responsiveSearchAd.setHeadlines(headlines.toArray(new AssetLink[0]));
List<AssetLink> descriptions = new ArrayList<>();
descriptions.add(createAssetLinkForText("Buy your tickets now"));
descriptions.add(createAssetLinkForText("Visit the Red Planet"));
responsiveSearchAd.setDescriptions(descriptions.toArray(new AssetLink[0]));
responsiveSearchAd.setFinalUrls(new String[] { "http://www.example.com/cruise" });
responsiveSearchAd.setPath1("all-inclusive");
responsiveSearchAd.setPath2("deals");
// Create ad group ad.
AdGroupAd responsiveSearchAdGroupAd = new AdGroupAd();
responsiveSearchAdGroupAd.setAdGroupId(adGroupId);
responsiveSearchAdGroupAd.setAd(responsiveSearchAd);
// Optional: set the status.
responsiveSearchAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create the operation.
AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
adGroupAdOperation.setOperand(responsiveSearchAdGroupAd);
adGroupAdOperation.setOperator(Operator.ADD);
operations.add(adGroupAdOperation);
// Add ad.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations.toArray(new AdGroupAdOperation[0]));
for (AdGroupAd adGroupAdResult : result.getValue()) {
ResponsiveSearchAd newAd = (ResponsiveSearchAd) adGroupAdResult.getAd();
System.out.printf("New responsive search ad with ID %d was added.%n", adGroupAdResult.getAd().getId());
System.out.println("Headlines:");
for (AssetLink headline : newAd.getHeadlines()) {
ServedAssetFieldType pinning = headline.getPinnedField();
System.out.printf(" %s%n", ((TextAsset) headline.getAsset()).getAssetText());
if (pinning != null) {
System.out.printf(" (pinned to %s)%n", pinning);
}
}
System.out.println("Descriptions:");
for (AssetLink description : newAd.getDescriptions()) {
ServedAssetFieldType pinning = description.getPinnedField();
System.out.printf(" %s%n", ((TextAsset) description.getAsset()).getAssetText());
if (pinning != null) {
System.out.printf(" (pinned to %s)%n", pinning);
}
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.ServedAssetFieldType in project googleads-java-lib by googleads.
the class GetResponsiveSearchAds method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group to use to find expanded text ads.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long adGroupId) throws RemoteException {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
int offset = 0;
boolean morePages = true;
// Create selector to get all of the ads for the ad group.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AdGroupAdField.Id, AdGroupAdField.Status, AdGroupAdField.ResponsiveSearchAdHeadlines, AdGroupAdField.ResponsiveSearchAdDescriptions).orderAscBy(AdGroupAdField.Id).offset(offset).limit(PAGE_SIZE).equals(AdGroupAdField.AdGroupId, adGroupId.toString()).in(AdGroupAdField.Status, "ENABLED", "PAUSED").equals("AdType", AdType.RESPONSIVE_SEARCH_AD.getValue()).build();
int totalEntries = 0;
while (morePages) {
// Get all ads.
AdGroupAdPage page = adGroupAdService.get(selector);
// Display ads.
if (page.getEntries() != null && page.getEntries().length > 0) {
totalEntries = page.getTotalNumEntries();
for (AdGroupAd adGroupAd : page.getEntries()) {
ResponsiveSearchAd responsiveSearchAd = (ResponsiveSearchAd) adGroupAd.getAd();
System.out.printf("Responsive search ad with ID %d, status '%s' was found.%n", adGroupAd.getAd().getId(), adGroupAd.getStatus());
System.out.println("Headlines:");
for (AssetLink headline : responsiveSearchAd.getHeadlines()) {
ServedAssetFieldType pinning = headline.getPinnedField();
System.out.printf(" %s%n", ((TextAsset) headline.getAsset()).getAssetText());
if (pinning != null) {
System.out.printf(" (pinned to %s)%n", pinning);
}
}
System.out.println("Descriptions:");
for (AssetLink description : responsiveSearchAd.getDescriptions()) {
ServedAssetFieldType pinning = description.getPinnedField();
System.out.printf(" %s%n", ((TextAsset) description.getAsset()).getAssetText());
if (pinning != null) {
System.out.printf(" (pinned to %s)%n", pinning);
}
}
}
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
System.out.printf("Ad group ID %d has %d responsive search ads.%n", adGroupId, totalEntries);
}
Aggregations