use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface 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);
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface in project googleads-java-lib by googleads.
the class PauseAd method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group for the ad.
* @param adId the ID of the ad to pause.
* @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, Long adId) throws RemoteException {
// Get the AdGroupAdService.
AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create ad with updated status.
Ad ad = new Ad();
ad.setId(adId);
AdGroupAd adGroupAd = new AdGroupAd();
adGroupAd.setAdGroupId(adGroupId);
adGroupAd.setAd(ad);
adGroupAd.setStatus(AdGroupAdStatus.PAUSED);
// Create operations.
AdGroupAdOperation operation = new AdGroupAdOperation();
operation.setOperand(adGroupAd);
operation.setOperator(Operator.SET);
AdGroupAdOperation[] operations = new AdGroupAdOperation[] { operation };
// Update ad.
AdGroupAdReturnValue result = adGroupAdService.mutate(operations);
// Display ads.
for (AdGroupAd adGroupAdResult : result.getValue()) {
System.out.printf("Ad with ID %d, type '%s', and status '%s' was updated.%n", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getAdType(), adGroupAdResult.getStatus());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface in project googleads-java-lib by googleads.
the class ValidateTextAd method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group for the ad.
* @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 validation.
session.setValidateOnly(true);
// Get the validation AdGroupAdService.
AdGroupAdServiceInterface adGroupAdValidationService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
// Create text ad.
ExpandedTextAd textAd1 = new ExpandedTextAd();
textAd1.setHeadlinePart1("Luxury Cruise to Mars");
textAd1.setHeadlinePart2("Visit the Red Planet in style.");
textAd1.setDescription("Low-gravity fun for everyone!");
textAd1.setFinalUrls(new String[] { "http://www.example.com" });
// Create ad group ad.
AdGroupAd textAdGroupAd1 = new AdGroupAd();
textAdGroupAd1.setAdGroupId(adGroupId);
textAdGroupAd1.setAd(textAd1);
// Create operations.
AdGroupAdOperation textAdGroupAdOperation1 = new AdGroupAdOperation();
textAdGroupAdOperation1.setOperand(textAdGroupAd1);
textAdGroupAdOperation1.setOperator(Operator.ADD);
AdGroupAdOperation[] operations = new AdGroupAdOperation[] { textAdGroupAdOperation1 };
// Add ads.
adGroupAdValidationService.mutate(operations);
// No error means the request is valid.
// Now let's check an invalid ad using a very long line to trigger an error.
textAd1.setDescription("Low-gravity fun for all astronauts in orbit.");
try {
adGroupAdValidationService.mutate(operations);
} catch (ApiException e) {
System.err.printf("Validation failed for the following reason: %s%n", e.getMessage());
}
}
use of com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface in project googleads-java-lib by googleads.
the class GetAllDisapprovedAdsWithAwql method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param adGroupId the ID of the ad group to search for disapproved 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);
ServiceQuery serviceQuery = new ServiceQuery.Builder().fields(AdGroupAdField.Id, AdGroupAdField.PolicySummary).where(AdGroupAdField.AdGroupId).equalTo(adGroupId).where(AdGroupAdField.CombinedApprovalStatus).equalTo(PolicyApprovalStatus.DISAPPROVED.getValue()).orderBy(AdGroupAdField.Id, SortOrder.ASCENDING).limit(0, PAGE_SIZE).build();
// Get all disapproved ads.
AdGroupAdPage page = null;
int disapprovedAdsCount = 0;
do {
serviceQuery.nextPage(page);
page = adGroupAdService.query(serviceQuery.toString());
// Display ads.
for (AdGroupAd adGroupAd : page) {
disapprovedAdsCount++;
AdGroupAdPolicySummary policySummary = adGroupAd.getPolicySummary();
System.out.printf("Ad with ID %d and type '%s' was disapproved with the following " + "policy topic entries:%n", adGroupAd.getAd().getId(), adGroupAd.getAd().getAdType());
// Display the policy topic entries related to the ad disapproval.
for (PolicyTopicEntry policyTopicEntry : policySummary.getPolicyTopicEntries()) {
System.out.printf(" topic id: %s, topic name: '%s'%n", policyTopicEntry.getPolicyTopicId(), policyTopicEntry.getPolicyTopicName());
// Display the attributes and values that triggered the policy topic.
if (policyTopicEntry.getPolicyTopicEvidences() != null) {
for (PolicyTopicEvidence evidence : policyTopicEntry.getPolicyTopicEvidences()) {
System.out.printf(" evidence type: %s%n", evidence.getPolicyTopicEvidenceType());
if (evidence.getEvidenceTextList() != null) {
for (int i = 0; i < evidence.getEvidenceTextList().length; i++) {
System.out.printf(" evidence text[%d]: %s%n", i, evidence.getEvidenceTextList(i));
}
}
}
}
}
}
} while (serviceQuery.hasNext(page));
System.out.printf("%d disapproved ads were found.%n", disapprovedAdsCount);
}
Aggregations