Search in sources :

Example 1 with Placement

use of com.google.api.ads.adwords.axis.v201809.cm.Placement in project googleads-java-lib by googleads.

the class AddCustomerNegativeCriteria 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 CustomerNegativeCriterionService.
    CustomerNegativeCriterionServiceInterface customerNegativeCriterionService = adWordsServices.get(session, CustomerNegativeCriterionServiceInterface.class);
    List<Criterion> criteria = new ArrayList<>();
    // Exclude tragedy & conflict content.
    ContentLabel tragedyContentLabel = new ContentLabel();
    tragedyContentLabel.setContentLabelType(ContentLabelType.TRAGEDY);
    criteria.add(tragedyContentLabel);
    // Exclude a specific placement.
    Placement placement = new Placement();
    placement.setUrl("http://www.example.com");
    criteria.add(placement);
    // Additional criteria types are available for this service. See the types listed
    // under Criterion here:
    // https://developers.google.com/adwords/api/docs/reference/latest/CustomerNegativeCriterionService.Criterion
    // Create operations to add each of the criteria above.
    List<CustomerNegativeCriterionOperation> operations = new ArrayList<>();
    for (Criterion criterion : criteria) {
        CustomerNegativeCriterion negativeCriterion = new CustomerNegativeCriterion();
        negativeCriterion.setCriterion(criterion);
        CustomerNegativeCriterionOperation operation = new CustomerNegativeCriterionOperation();
        operation.setOperator(Operator.ADD);
        operation.setOperand(negativeCriterion);
        operations.add(operation);
    }
    // Send the request to add the criteria.
    CustomerNegativeCriterionReturnValue result = customerNegativeCriterionService.mutate(operations.toArray(new CustomerNegativeCriterionOperation[operations.size()]));
    // Display the results.
    for (CustomerNegativeCriterion negativeCriterion : result.getValue()) {
        System.out.printf("Customer negative criterion with criterion ID %d and type '%s' was added.%n", negativeCriterion.getCriterion().getId(), negativeCriterion.getCriterion().getCriterionType());
    }
}
Also used : ContentLabel(com.google.api.ads.adwords.axis.v201809.cm.ContentLabel) CustomerNegativeCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionOperation) CustomerNegativeCriterion(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterion) Criterion(com.google.api.ads.adwords.axis.v201809.cm.Criterion) Placement(com.google.api.ads.adwords.axis.v201809.cm.Placement) CustomerNegativeCriterion(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterion) CustomerNegativeCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionReturnValue) ArrayList(java.util.ArrayList) CustomerNegativeCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionServiceInterface)

Example 2 with Placement

use of com.google.api.ads.adwords.axis.v201809.cm.Placement 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

Criterion (com.google.api.ads.adwords.axis.v201809.cm.Criterion)2 Placement (com.google.api.ads.adwords.axis.v201809.cm.Placement)2 ArrayList (java.util.ArrayList)2 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)1 CampaignSharedSet (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSet)1 CampaignSharedSetPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetPage)1 CampaignSharedSetServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetServiceInterface)1 ContentLabel (com.google.api.ads.adwords.axis.v201809.cm.ContentLabel)1 CustomerNegativeCriterion (com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterion)1 CustomerNegativeCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionOperation)1 CustomerNegativeCriterionReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionReturnValue)1 CustomerNegativeCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CustomerNegativeCriterionServiceInterface)1 Keyword (com.google.api.ads.adwords.axis.v201809.cm.Keyword)1 SharedCriterion (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterion)1 SharedCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionOperation)1 SharedCriterionPage (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionPage)1 SharedCriterionReturnValue (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionReturnValue)1 SharedCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionServiceInterface)1