use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class GetAdGroups method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param campaignId the ID of the campaign to use to find ad groups.
* @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 campaignId) throws RemoteException {
// Get the AdGroupService.
AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
int offset = 0;
boolean morePages = true;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AdGroupField.Id, AdGroupField.Name).orderAscBy(AdGroupField.Name).offset(offset).limit(PAGE_SIZE).equals(AdGroupField.CampaignId, campaignId.toString()).build();
while (morePages) {
// Get all ad groups.
AdGroupPage page = adGroupService.get(selector);
// Display ad groups.
if (page.getEntries() != null) {
for (AdGroup adGroup : page.getEntries()) {
System.out.printf("Ad group with name '%s' and ID %d was found.%n", adGroup.getName(), adGroup.getId());
}
} else {
System.out.println("No ad groups were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class HandlePartialFailures method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group.
* @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 {
// Enable partial failure.
session.setPartialFailure(true);
// Get the AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
List<AdGroupCriterionOperation> operations = new ArrayList<>();
// Create keywords.
String[] keywords = new String[] { "mars cruise", "inv@lid cruise", "venus cruise", "b(a)d keyword cruise" };
for (String keywordText : keywords) {
// Create keyword
Keyword keyword = new Keyword();
keyword.setText(keywordText);
keyword.setMatchType(KeywordMatchType.BROAD);
// Create biddable ad group criterion.
BiddableAdGroupCriterion keywordBiddableAdGroupCriterion = new BiddableAdGroupCriterion();
keywordBiddableAdGroupCriterion.setAdGroupId(adGroupId);
keywordBiddableAdGroupCriterion.setCriterion(keyword);
// Create operation.
AdGroupCriterionOperation keywordAdGroupCriterionOperation = new AdGroupCriterionOperation();
keywordAdGroupCriterionOperation.setOperand(keywordBiddableAdGroupCriterion);
keywordAdGroupCriterionOperation.setOperator(Operator.ADD);
operations.add(keywordAdGroupCriterionOperation);
}
// Add ad group criteria.
AdGroupCriterionReturnValue result = adGroupCriterionService.mutate(operations.toArray(new AdGroupCriterionOperation[] {}));
// Display results.
Arrays.stream(result.getValue()).filter(adGroupCriterionResult -> adGroupCriterionResult.getCriterion() != null).forEach(adGroupCriterionResult -> System.out.printf("Ad group criterion with ad group ID %d, and criterion ID %d, " + "and keyword '%s' was added.%n", adGroupCriterionResult.getAdGroupId(), adGroupCriterionResult.getCriterion().getId(), ((Keyword) adGroupCriterionResult.getCriterion()).getText()));
for (ApiError apiError : result.getPartialFailureErrors()) {
// Get the index of the failed operation from the error's field path elements.
FieldPathElement[] fieldPathElements = apiError.getFieldPathElements();
FieldPathElement firstFieldPathElement = null;
if (fieldPathElements != null && fieldPathElements.length > 0) {
firstFieldPathElement = fieldPathElements[0];
}
if (firstFieldPathElement != null && "operations".equals(firstFieldPathElement.getField()) && firstFieldPathElement.getIndex() != null) {
int operationIndex = firstFieldPathElement.getIndex();
AdGroupCriterion adGroupCriterion = operations.get(operationIndex).getOperand();
System.out.printf("Ad group criterion with ad group ID %d and keyword '%s' " + "triggered a failure for the following reason: %s.%n", adGroupCriterion.getAdGroupId(), ((Keyword) adGroupCriterion.getCriterion()).getText(), apiError.getErrorString());
} else {
System.out.printf("A failure has occurred for the following reason: %s%n", apiError.getErrorString());
}
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad 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.Ad in project googleads-java-lib by googleads.
the class GetExpandedTextAds 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.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AdGroupAdField.Id, AdGroupAdField.Status, AdGroupAdField.HeadlinePart1, AdGroupAdField.HeadlinePart2, AdGroupAdField.Description).orderAscBy(AdGroupAdField.Id).offset(offset).limit(PAGE_SIZE).equals(AdGroupAdField.AdGroupId, adGroupId.toString()).in(AdGroupAdField.Status, "ENABLED", "PAUSED").equals("AdType", "EXPANDED_TEXT_AD").build();
while (morePages) {
// Get all ads.
AdGroupAdPage page = adGroupAdService.get(selector);
// Display ads.
if (page.getEntries() != null && page.getEntries().length > 0) {
for (AdGroupAd adGroupAd : page.getEntries()) {
ExpandedTextAd expandedTextAd = (ExpandedTextAd) adGroupAd.getAd();
System.out.printf("Expanded text ad with ID %d, status '%s', and headline '%s - %s' was found.%n", adGroupAd.getAd().getId(), adGroupAd.getStatus(), expandedTextAd.getHeadlinePart1(), expandedTextAd.getHeadlinePart2());
}
} else {
System.out.println("No expanded text ads were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.Ad in project googleads-java-lib by googleads.
the class AddShoppingDynamicRemarketingCampaign method createAdGroup.
/**
* Creates an ad group in the specified campaign.
*
* @param campaign the campaign to which the ad group should be attached.
* @return the ad group that was created.
*/
private static AdGroup createAdGroup(AdWordsServicesInterface services, AdWordsSession session, Campaign campaign) throws RemoteException {
AdGroupServiceInterface adGroupService = services.get(session, AdGroupServiceInterface.class);
AdGroup group = new AdGroup();
group.setName("Dynamic remarketing ad group");
group.setCampaignId(campaign.getId());
group.setStatus(AdGroupStatus.ENABLED);
AdGroupOperation op = new AdGroupOperation();
op.setOperand(group);
op.setOperator(Operator.ADD);
AdGroupReturnValue result = adGroupService.mutate(new AdGroupOperation[] { op });
return result.getValue(0);
}
Aggregations