use of com.google.api.ads.adwords.jaxws.v201809.cm.Selector 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.jaxws.v201809.cm.Selector in project googleads-java-lib by googleads.
the class AddTrial method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param draftId the ID of the draft.
* @param baseCampaignId the ID of the base campaign.
* @throws ApiException if the API request failed with one or more service errors.
* @throws RemoteException if the API request failed due to other errors.
* @throws InterruptedException
*/
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, long draftId, long baseCampaignId) throws RemoteException, InterruptedException {
// Get the TrialService.
TrialServiceInterface trialService = adWordsServices.get(session, TrialServiceInterface.class);
Trial trial = new Trial();
trial.setDraftId(draftId);
trial.setBaseCampaignId(baseCampaignId);
trial.setName("Test Trial #" + System.currentTimeMillis());
trial.setTrafficSplitPercent(50);
trial.setTrafficSplitType(CampaignTrialTrafficSplitType.RANDOM_QUERY);
TrialOperation trialOperation = new TrialOperation();
trialOperation.setOperator(Operator.ADD);
trialOperation.setOperand(trial);
long trialId = trialService.mutate(new TrialOperation[] { trialOperation }).getValue(0).getId();
// Since creating a trial is asynchronous, we have to poll it to wait for it to finish.
Selector trialSelector = new SelectorBuilder().fields(TrialField.Id, TrialField.Status, TrialField.BaseCampaignId, TrialField.TrialCampaignId).equalsId(trialId).build();
trial = null;
boolean isPending = true;
int pollAttempts = 0;
do {
long sleepSeconds = (long) Math.scalb(30d, pollAttempts);
System.out.printf("Sleeping for %d seconds.%n", sleepSeconds);
Thread.sleep(sleepSeconds * 1000);
trial = trialService.get(trialSelector).getEntries(0);
System.out.printf("Trial ID %d has status '%s'.%n", trial.getId(), trial.getStatus());
pollAttempts++;
isPending = TrialStatus.CREATING.equals(trial.getStatus());
} while (isPending && pollAttempts < MAX_POLL_ATTEMPTS);
if (TrialStatus.ACTIVE.equals(trial.getStatus())) {
// The trial creation was successful.
System.out.printf("Trial created with ID %d and trial campaign ID %d.%n", trial.getId(), trial.getTrialCampaignId());
} else if (TrialStatus.CREATION_FAILED.equals(trial.getStatus())) {
// The trial creation failed, and errors can be fetched from the TrialAsyncErrorService.
Selector errorsSelector = new SelectorBuilder().fields(TrialAsyncErrorField.TrialId, TrialAsyncErrorField.AsyncError).equals(TrialAsyncErrorField.TrialId, trial.getId().toString()).build();
TrialAsyncErrorServiceInterface trialAsyncErrorService = adWordsServices.get(session, TrialAsyncErrorServiceInterface.class);
TrialAsyncErrorPage trialAsyncErrorPage = trialAsyncErrorService.get(errorsSelector);
if (trialAsyncErrorPage.getEntries() == null || trialAsyncErrorPage.getEntries().length == 0) {
System.out.printf("Could not retrieve errors for trial ID %d for draft ID %d.%n", trial.getId(), draftId);
} else {
System.out.printf("Could not create trial ID %d for draft ID %d due to the following errors:%n", trial.getId(), draftId);
int i = 0;
for (TrialAsyncError error : trialAsyncErrorPage.getEntries()) {
ApiError asyncError = error.getAsyncError();
System.out.printf("Error #%d: errorType='%s', errorString='%s', trigger='%s', fieldPath='%s'%n", i++, asyncError.getApiErrorType(), asyncError.getErrorString(), asyncError.getTrigger(), asyncError.getFieldPath());
}
}
} else {
// Most likely, the trial is still being created. You can continue polling,
// but we have limited the number of attempts in the example.
System.out.printf("Timed out waiting to create trial from draft ID %d with base campaign ID %d.%n", draftId, baseCampaignId);
}
}
use of com.google.api.ads.adwords.jaxws.v201809.cm.Selector in project googleads-java-lib by googleads.
the class GetCampaigns method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @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) throws RemoteException {
// Get the CampaignService.
CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
int offset = 0;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(CampaignField.Id, CampaignField.Name).orderAscBy(CampaignField.Name).offset(offset).limit(PAGE_SIZE).build();
CampaignPage page;
do {
// Get all campaigns.
page = campaignService.get(selector);
// Display campaigns.
if (page.getEntries() != null) {
for (Campaign campaign : page.getEntries()) {
System.out.printf("Campaign with name '%s' and ID %d was found.%n", campaign.getName(), campaign.getId());
}
} else {
System.out.println("No campaigns were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
} while (offset < page.getTotalNumEntries());
}
use of com.google.api.ads.adwords.jaxws.v201809.cm.Selector in project googleads-java-lib by googleads.
the class GetAllImageAssets method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @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) throws RemoteException {
// Get the AdGroupAdService.
AssetServiceInterface assetService = adWordsServices.get(session, AssetServiceInterface.class);
int offset = 0;
boolean morePages = true;
// Create the selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AssetField.AssetName, AssetField.AssetStatus, AssetField.ImageFileSize, AssetField.ImageWidth, AssetField.ImageHeight, AssetField.ImageFullSizeUrl).offset(offset).limit(PAGE_SIZE).equals(AssetField.AssetSubtype, AssetType.IMAGE.getValue()).build();
int totalEntries = 0;
while (morePages) {
// Get the image assets.
AssetPage page = assetService.get(selector);
// Display the results.
if (page.getEntries() != null && page.getEntries().length > 0) {
totalEntries = page.getTotalNumEntries();
int i = selector.getPaging().getStartIndex();
for (Asset asset : page.getEntries()) {
System.out.printf("%d) Image asset with ID %d, name '%s', and status '%s' was found.%n", i, asset.getAssetId(), asset.getAssetName(), asset.getAssetStatus());
}
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
System.out.printf("Found %d image assets.%n", totalEntries);
}
use of com.google.api.ads.adwords.jaxws.v201809.cm.Selector in project googleads-java-lib by googleads.
the class GetCampaignsByLabel method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param labelId the ID of the label.
* @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 labelId) throws RemoteException {
// Get the CampaignService.
CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
int offset = 0;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(CampaignField.Id, CampaignField.Name, CampaignField.Labels).containsAny(CampaignField.Labels, labelId.toString()).orderAscBy(CampaignField.Name).offset(offset).limit(PAGE_SIZE).build();
CampaignPage page = null;
do {
// Get all campaigns.
page = campaignService.get(selector);
// Display campaigns.
if (page.getEntries() != null) {
for (Campaign campaign : page.getEntries()) {
String labels = Joiner.on(", ").join(Lists.transform(Lists.newArrayList(campaign.getLabels()), label -> String.format("%d/%s", label.getId(), label.getName())));
System.out.printf("Campaign found with name '%s' and ID %d and labels: %s.%n", campaign.getName(), campaign.getId(), labels);
}
} else {
System.out.println("No campaigns were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
} while (offset < page.getTotalNumEntries());
}
Aggregations