use of com.google.api.ads.adwords.jaxws.v201809.cm.Keyword in project googleads-java-lib by googleads.
the class GetKeywords 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 keywords.
* @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 AdGroupCriterionService.
AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
int offset = 0;
boolean morePages = true;
// Create selector.
SelectorBuilder builder = new SelectorBuilder();
Selector selector = builder.fields(AdGroupCriterionField.Id, AdGroupCriterionField.CriteriaType, AdGroupCriterionField.KeywordMatchType, AdGroupCriterionField.KeywordText).orderAscBy(AdGroupCriterionField.KeywordText).offset(offset).limit(PAGE_SIZE).in(AdGroupCriterionField.AdGroupId, adGroupId.toString()).in(AdGroupCriterionField.CriteriaType, "KEYWORD").build();
while (morePages) {
// Get all ad group criteria.
AdGroupCriterionPage page = adGroupCriterionService.get(selector);
// Display ad group criteria.
if (page.getEntries() != null && page.getEntries().length > 0) {
// Display results.
Arrays.stream(page.getEntries()).map(adGroupCriterionResult -> (Keyword) adGroupCriterionResult.getCriterion()).forEach(keyword -> System.out.printf("Keyword with text '%s', match type '%s', criteria type '%s'," + " and ID %d was found.%n", keyword.getText(), keyword.getMatchType(), keyword.getType(), keyword.getId()));
} else {
System.out.println("No ad group criteria were found.");
}
offset += PAGE_SIZE;
selector = builder.increaseOffsetBy(PAGE_SIZE).build();
morePages = offset < page.getTotalNumEntries();
}
}
use of com.google.api.ads.adwords.jaxws.v201809.cm.Keyword in project googleads-java-lib by googleads.
the class AddCampaignTargetingCriteria method runExample.
/**
* Runs the example.
*
* @param adWordsServices the services factory.
* @param session the session.
* @param campaignId the ID of the campaign where targeting criteria will be added.
* @param locationFeedId optional ID of a location targeting feed.
* @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, @Nullable Long locationFeedId) throws RemoteException {
// Get the CampaignService.
CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(session, CampaignCriterionServiceInterface.class);
// Create locations. The IDs can be found in the documentation or
// retrieved with the LocationCriterionService.
Location california = new Location();
california.setId(21137L);
Location mexico = new Location();
mexico.setId(2484L);
// Create languages. The IDs can be found in the documentation or
// retrieved with the ConstantDataService.
Language english = new Language();
english.setId(1000L);
Language spanish = new Language();
spanish.setId(1003L);
List<Criterion> criteria = new ArrayList<>(Arrays.asList(california, mexico, english, spanish));
// Distance targeting. Area of 10 miles around the locations in the location feed.
if (locationFeedId != null) {
LocationGroups radiusLocationGroup = new LocationGroups();
radiusLocationGroup.setFeedId(locationFeedId);
ConstantOperand radius = new ConstantOperand();
radius.setType(ConstantOperandConstantType.DOUBLE);
radius.setUnit(ConstantOperandUnit.MILES);
radius.setDoubleValue(10d);
LocationExtensionOperand distance = new LocationExtensionOperand();
distance.setRadius(radius);
Function radiusMatchingFunction = new Function();
radiusMatchingFunction.setOperator(FunctionOperator.IDENTITY);
radiusMatchingFunction.setLhsOperand(new FunctionArgumentOperand[] { distance });
radiusLocationGroup.setMatchingFunction(radiusMatchingFunction);
criteria.add(radiusLocationGroup);
}
// Create operations to add each of the criteria above.
List<CampaignCriterionOperation> operations = new ArrayList<>();
for (Criterion criterion : criteria) {
CampaignCriterionOperation operation = new CampaignCriterionOperation();
CampaignCriterion campaignCriterion = new CampaignCriterion();
campaignCriterion.setCampaignId(campaignId);
campaignCriterion.setCriterion(criterion);
operation.setOperand(campaignCriterion);
operation.setOperator(Operator.ADD);
operations.add(operation);
}
// Add a negative campaign criterion.
Keyword negativeKeyword = new Keyword();
negativeKeyword.setText("jupiter cruise");
negativeKeyword.setMatchType(KeywordMatchType.BROAD);
CampaignCriterion negativeCriterion = new NegativeCampaignCriterion();
negativeCriterion.setCampaignId(campaignId);
negativeCriterion.setCriterion(negativeKeyword);
CampaignCriterionOperation operation = new CampaignCriterionOperation();
operation.setOperand(negativeCriterion);
operation.setOperator(Operator.ADD);
operations.add(operation);
CampaignCriterionReturnValue result = campaignCriterionService.mutate(operations.toArray(new CampaignCriterionOperation[operations.size()]));
// Display campaigns.
for (CampaignCriterion campaignCriterion : result.getValue()) {
System.out.printf("Campaign criterion with campaign ID %d, criterion ID %d, " + "and type '%s' was added.%n", campaignCriterion.getCampaignId(), campaignCriterion.getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
}
}
use of com.google.api.ads.adwords.jaxws.v201809.cm.Keyword in project googleads-java-lib by googleads.
the class AxisBatchJobUploadBodyProviderTest method addCampaignNegativeKeywordOperation.
@Override
protected void addCampaignNegativeKeywordOperation(BatchJobMutateRequest request, long campaignId, String keywordText, String keywordMatchType) {
Keyword keyword = new Keyword();
keyword.setText(keywordText);
keyword.setMatchType(KeywordMatchType.fromString(keywordMatchType));
NegativeCampaignCriterion negativeCriterion = new NegativeCampaignCriterion();
negativeCriterion.setCampaignId(campaignId);
negativeCriterion.setCriterion(keyword);
CampaignCriterionOperation operation = new CampaignCriterionOperation();
operation.setOperand(negativeCriterion);
operation.setOperator(Operator.ADD);
request.addOperation(operation);
}
use of com.google.api.ads.adwords.jaxws.v201809.cm.Keyword in project googleads-java-lib by googleads.
the class JaxWsBatchJobUploadBodyProviderTest method addCampaignNegativeKeywordOperation.
@Override
protected void addCampaignNegativeKeywordOperation(BatchJobMutateRequest request, long campaignId, String keywordText, String keywordMatchType) {
Keyword keyword = new Keyword();
keyword.setText(keywordText);
keyword.setMatchType(KeywordMatchType.valueOf(keywordMatchType));
NegativeCampaignCriterion negativeCriterion = new NegativeCampaignCriterion();
negativeCriterion.setCampaignId(campaignId);
negativeCriterion.setCriterion(keyword);
CampaignCriterionOperation operation = new CampaignCriterionOperation();
operation.setOperand(negativeCriterion);
operation.setOperator(Operator.ADD);
request.addOperation(operation);
}
use of com.google.api.ads.adwords.jaxws.v201809.cm.Keyword in project googleads-java-lib by googleads.
the class FindAndRemoveCriteriaFromSharedSet 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 shared sets.
* @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 CampaignSharedSetService.
CampaignSharedSetServiceInterface campaignSharedSetService = adWordsServices.get(session, CampaignSharedSetServiceInterface.class);
// First, retrieve all shared sets associated with the campaign.
int offset = 0;
SelectorBuilder selectorBuilder = new SelectorBuilder().fields(CampaignSharedSetField.SharedSetId, CampaignSharedSetField.CampaignId, CampaignSharedSetField.SharedSetName, CampaignSharedSetField.SharedSetType).equals(CampaignSharedSetField.CampaignId, campaignId.toString()).in(CampaignSharedSetField.SharedSetType, SharedSetType.NEGATIVE_KEYWORDS.getValue(), SharedSetType.NEGATIVE_PLACEMENTS.getValue()).limit(PAGE_SIZE);
List<Long> sharedSetIds = new ArrayList<>();
CampaignSharedSetPage campaignSharedSetPage;
do {
selectorBuilder.offset(offset);
campaignSharedSetPage = campaignSharedSetService.get(selectorBuilder.build());
for (CampaignSharedSet campaignSharedSet : campaignSharedSetPage.getEntries()) {
sharedSetIds.add(campaignSharedSet.getSharedSetId());
System.out.printf("Campaign shared set ID %d and name '%s' found for campaign ID %d.%n", campaignSharedSet.getSharedSetId(), campaignSharedSet.getSharedSetName(), campaignSharedSet.getCampaignId());
}
offset += PAGE_SIZE;
} while (offset < campaignSharedSetPage.getTotalNumEntries());
if (sharedSetIds.isEmpty()) {
System.out.printf("No shared sets found for campaign ID %d.%n", campaignId);
return;
}
// Next, retrieve criterion IDs for all found shared sets.
SharedCriterionServiceInterface sharedCriterionService = adWordsServices.get(session, SharedCriterionServiceInterface.class);
// Transform shared set IDs to strings.
String[] sharedSetIdStrings = Collections2.transform(sharedSetIds, Functions.toStringFunction()).toArray(new String[sharedSetIds.size()]);
offset = 0;
selectorBuilder = new SelectorBuilder().fields("SharedSetId", "Id", "KeywordText", "KeywordMatchType", "PlacementUrl").in("SharedSetId", sharedSetIdStrings).limit(PAGE_SIZE);
List<SharedCriterionOperation> removeCriterionOperations = new ArrayList<>();
SharedCriterionPage sharedCriterionPage;
do {
selectorBuilder.offset(offset);
sharedCriterionPage = sharedCriterionService.get(selectorBuilder.build());
for (SharedCriterion sharedCriterion : sharedCriterionPage.getEntries()) {
if (CriterionType.KEYWORD.equals(sharedCriterion.getCriterion().getType())) {
Keyword keyword = (Keyword) sharedCriterion.getCriterion();
System.out.printf("Shared negative keyword with ID %d and text '%s' was found.%n", keyword.getId(), keyword.getText());
} else if (CriterionType.PLACEMENT.equals(sharedCriterion.getCriterion().getType())) {
Placement placement = (Placement) sharedCriterion.getCriterion();
System.out.printf("Shared negative placement with ID %d and URL '%s' was found.%n", placement.getId(), placement.getUrl());
} else {
System.out.printf("Shared criterion with ID %d was found.%n", sharedCriterion.getCriterion().getId());
}
// Create an operation to remove this criterion.
SharedCriterionOperation removeCriterionOperation = new SharedCriterionOperation();
removeCriterionOperation.setOperator(Operator.REMOVE);
SharedCriterion sharedCriterionToRemove = new SharedCriterion();
Criterion criterionToRemove = new Criterion();
criterionToRemove.setId(sharedCriterion.getCriterion().getId());
sharedCriterionToRemove.setCriterion(criterionToRemove);
sharedCriterionToRemove.setSharedSetId(sharedCriterion.getSharedSetId());
removeCriterionOperation.setOperand(sharedCriterionToRemove);
removeCriterionOperations.add(removeCriterionOperation);
}
offset += PAGE_SIZE;
} while (offset < sharedCriterionPage.getTotalNumEntries());
// Finally, remove the criteria.
if (removeCriterionOperations.isEmpty()) {
System.out.printf("No shared criteria to remove.%n");
} else {
SharedCriterionReturnValue sharedCriterionReturnValue = sharedCriterionService.mutate(removeCriterionOperations.toArray(new SharedCriterionOperation[removeCriterionOperations.size()]));
for (SharedCriterion removedCriterion : sharedCriterionReturnValue.getValue()) {
System.out.printf("Shared criterion ID %d was successfully removed from shared set ID %d.%n", removedCriterion.getCriterion().getId(), removedCriterion.getSharedSetId());
}
}
}
Aggregations