use of com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetOperation in project googleads-java-lib by googleads.
the class CreateAndAttachSharedKeywordSet method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param campaignId the ID of the campaign that will use the shared set.
* @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 SharedSetService.
SharedSetServiceInterface sharedSetService = adWordsServices.get(session, SharedSetServiceInterface.class);
// Keywords to include in the shared set.
List<String> keywords = Arrays.asList("mars cruise", "mars hotels");
// Create the shared negative keyword set.
SharedSet sharedSet = new SharedSet();
sharedSet.setName("Negative keyword list #" + System.currentTimeMillis());
sharedSet.setType(SharedSetType.NEGATIVE_KEYWORDS);
SharedSetOperation sharedSetOperation = new SharedSetOperation();
sharedSetOperation.setOperator(Operator.ADD);
sharedSetOperation.setOperand(sharedSet);
// Add the shared set.
sharedSet = sharedSetService.mutate(new SharedSetOperation[] { sharedSetOperation }).getValue(0);
System.out.printf("Shared set with ID %d and name '%s' was successfully added.%n", sharedSet.getSharedSetId(), sharedSet.getName());
// Add negative keywords to the shared set.
List<SharedCriterionOperation> sharedCriterionOperations = new ArrayList<>();
for (String keyword : keywords) {
Keyword keywordCriterion = new Keyword();
keywordCriterion.setText(keyword);
keywordCriterion.setMatchType(KeywordMatchType.BROAD);
SharedCriterion sharedCriterion = new SharedCriterion();
sharedCriterion.setCriterion(keywordCriterion);
sharedCriterion.setNegative(true);
sharedCriterion.setSharedSetId(sharedSet.getSharedSetId());
SharedCriterionOperation sharedCriterionOperation = new SharedCriterionOperation();
sharedCriterionOperation.setOperator(Operator.ADD);
sharedCriterionOperation.setOperand(sharedCriterion);
sharedCriterionOperations.add(sharedCriterionOperation);
}
// Get the SharedCriterionService.
SharedCriterionServiceInterface sharedCriterionService = adWordsServices.get(session, SharedCriterionServiceInterface.class);
SharedCriterionReturnValue sharedCriterionReturnValue = sharedCriterionService.mutate(sharedCriterionOperations.toArray(new SharedCriterionOperation[sharedCriterionOperations.size()]));
for (SharedCriterion addedCriterion : sharedCriterionReturnValue.getValue()) {
System.out.printf("Added shared criterion ID %d with text '%s' to shared set with ID %d.%n", addedCriterion.getCriterion().getId(), ((Keyword) addedCriterion.getCriterion()).getText(), addedCriterion.getSharedSetId());
}
// Attach the negative keyword shared set to the campaign.
CampaignSharedSetServiceInterface campaignSharedSetService = adWordsServices.get(session, CampaignSharedSetServiceInterface.class);
CampaignSharedSet campaignSharedSet = new CampaignSharedSet();
campaignSharedSet.setCampaignId(campaignId);
campaignSharedSet.setSharedSetId(sharedSet.getSharedSetId());
CampaignSharedSetOperation campaignSharedSetOperation = new CampaignSharedSetOperation();
campaignSharedSetOperation.setOperator(Operator.ADD);
campaignSharedSetOperation.setOperand(campaignSharedSet);
campaignSharedSet = campaignSharedSetService.mutate(new CampaignSharedSetOperation[] { campaignSharedSetOperation }).getValue(0);
System.out.printf("Shared set ID %d was attached to campaign ID %d.%n", campaignSharedSet.getSharedSetId(), campaignSharedSet.getCampaignId());
}
Aggregations