Search in sources :

Example 1 with SharedSet

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

Aggregations

CampaignSharedSet (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSet)1 CampaignSharedSetOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetOperation)1 CampaignSharedSetServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignSharedSetServiceInterface)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 SharedCriterionReturnValue (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionReturnValue)1 SharedCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.SharedCriterionServiceInterface)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 ArrayList (java.util.ArrayList)1