Search in sources :

Example 1 with SharedCriterion

use of com.google.api.ads.adwords.axis.v201809.cm.SharedCriterion 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());
}
Also used : SharedCriterion(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterion) CampaignSharedSetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetServiceInterface) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) ArrayList(java.util.ArrayList) SharedSetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.SharedSetServiceInterface) CampaignSharedSetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetServiceInterface) SharedCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionOperation) CampaignSharedSet(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSet) SharedSet(com.google.api.ads.adwords.axis.v201809.cm.SharedSet) SharedSetOperation(com.google.api.ads.adwords.axis.v201809.cm.SharedSetOperation) CampaignSharedSetOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetOperation) SharedCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionReturnValue) CampaignSharedSet(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSet) SharedCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionServiceInterface) CampaignSharedSetOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetOperation)

Example 2 with SharedCriterion

use of com.google.api.ads.adwords.axis.v201809.cm.SharedCriterion 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());
        }
    }
}
Also used : CampaignSharedSetPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetPage) SharedCriterion(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterion) CampaignSharedSetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetServiceInterface) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) ArrayList(java.util.ArrayList) SharedCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionOperation) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) Placement(com.google.api.ads.adwords.axis.v201809.cm.Placement) SharedCriterion(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterion) Criterion(com.google.api.ads.adwords.axis.v201809.cm.Criterion) CampaignSharedSet(com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSet) SharedCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionReturnValue) SharedCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionServiceInterface) SharedCriterionPage(com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionPage)

Aggregations

CampaignSharedSet (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSet)2 CampaignSharedSetServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetServiceInterface)2 Keyword (com.google.api.ads.adwords.axis.v201809.cm.Keyword)2 SharedCriterion (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterion)2 SharedCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionOperation)2 SharedCriterionReturnValue (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionReturnValue)2 SharedCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionServiceInterface)2 ArrayList (java.util.ArrayList)2 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 CampaignSharedSetOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetOperation)1 CampaignSharedSetPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetPage)1 Criterion (com.google.api.ads.adwords.axis.v201809.cm.Criterion)1 Placement (com.google.api.ads.adwords.axis.v201809.cm.Placement)1 SharedCriterionPage (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionPage)1 SharedSet (com.google.api.ads.adwords.axis.v201809.cm.SharedSet)1 SharedSetOperation (com.google.api.ads.adwords.axis.v201809.cm.SharedSetOperation)1 SharedSetServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.SharedSetServiceInterface)1