use of com.google.api.ads.adwords.axis.v201809.cm.Draft in project googleads-java-lib by googleads.
the class AddDraft method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param baseCampaignId the base campaign ID for the draft.
* @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 baseCampaignId) throws RemoteException {
// Get the DraftService.
DraftServiceInterface draftService = adWordsServices.get(session, DraftServiceInterface.class);
Draft draft = new Draft();
draft.setBaseCampaignId(baseCampaignId);
draft.setDraftName("Test Draft #" + System.currentTimeMillis());
DraftOperation draftOperation = new DraftOperation();
draftOperation.setOperator(Operator.ADD);
draftOperation.setOperand(draft);
draft = draftService.mutate(new DraftOperation[] { draftOperation }).getValue(0);
System.out.printf("Draft with ID %d and base campaign ID %d and draft campaign ID %d created.%n", draft.getDraftId(), draft.getBaseCampaignId(), draft.getDraftCampaignId());
// Once the draft is created, you can modify the draft campaign as if it
// were a real campaign. For example, you may add criteria, adjust bids,
// or even include additional ads. Adding a criterion is shown here.
CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(session, CampaignCriterionServiceInterface.class);
Language language = new Language();
// Spanish
language.setId(1003L);
// Make sure to use the draftCampaignId when modifying the virtual draft campaign.
CampaignCriterion campaignCriterion = new CampaignCriterion();
campaignCriterion.setCampaignId(draft.getDraftCampaignId());
campaignCriterion.setCriterion(language);
CampaignCriterionOperation criterionOperation = new CampaignCriterionOperation();
criterionOperation.setOperator(Operator.ADD);
criterionOperation.setOperand(campaignCriterion);
campaignCriterion = campaignCriterionService.mutate(new CampaignCriterionOperation[] { criterionOperation }).getValue(0);
System.out.printf("Draft updated to include criteria in draft campaign ID %d.%n", draft.getDraftCampaignId());
}
use of com.google.api.ads.adwords.axis.v201809.cm.Draft 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);
}
}
Aggregations