Search in sources :

Example 6 with AdGroup

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

the class JaxWsSerializationTest method testSerializeBatchJobMutateRequest.

@SuppressWarnings("cast")
@Test
public void testSerializeBatchJobMutateRequest() throws IOException, SAXException {
    BatchJobMutateRequest request = new BatchJobMutateRequest();
    CampaignOperation campaignOp = new CampaignOperation();
    campaignOp.setOperator(Operator.ADD);
    Campaign campaign = new Campaign();
    campaign.setId(-1L);
    campaign.setName("Test campaign");
    campaign.setAdvertisingChannelType(AdvertisingChannelType.SEARCH);
    campaignOp.setOperand(campaign);
    request.addOperation(campaignOp);
    AdGroupOperation adGroupOp = new AdGroupOperation();
    adGroupOp.setOperator(Operator.ADD);
    AdGroup adGroup = new AdGroup();
    adGroup.setName("Test ad group");
    adGroup.setCampaignId(campaign.getId());
    adGroupOp.setOperand(adGroup);
    request.addOperation(adGroupOp);
    JaxBSerializer<BatchJobMutateRequest> serializer = new JaxBSerializer<>(BatchJobMutateRequest.class, new QName("https://adwords.google.com/api/adwords/cm/v201809", "mutate"));
    String serializedRequest = serializer.serialize(request);
    assertNotNull("Null serialized request returned", serializedRequest);
    String expectedSerializedRequest = CharStreams.toString(new InputStreamReader(JaxWsSerializationTest.class.getResourceAsStream("resources/BatchJobMutate.request.xml"), UTF_8));
    // Perform XML diffs using the custom difference listener that properly handles namespaces
    // and attributes.
    Diff diff = DiffBuilder.compare(expectedSerializedRequest).withDifferenceEvaluator(DifferenceEvaluators.chain(DifferenceEvaluators.Default, new CustomDifferenceEvaluator())).withTest(serializedRequest).checkForSimilar().build();
    assertFalse("Serialized request does not match expected value", diff.hasDifferences());
    // Serialize again, this time without the XML declaration, and confirm the serialized XML
    // matches the expected string.
    serializedRequest = serializer.serialize(request, false);
    expectedSerializedRequest = expectedSerializedRequest.replaceFirst("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>", "");
    diff = DiffBuilder.compare(expectedSerializedRequest).normalizeWhitespace().withDifferenceEvaluator(new CustomDifferenceEvaluator()).withTest(serializedRequest).checkForSimilar().build();
    assertFalse("Serialized request with XML declaration excluded does not match expected value", diff.hasDifferences());
}
Also used : CustomDifferenceEvaluator(com.google.api.ads.common.lib.soap.testing.CustomDifferenceEvaluator) BatchJobMutateRequest(com.google.api.ads.adwords.jaxws.utils.v201809.batchjob.BatchJobMutateRequest) Campaign(com.google.api.ads.adwords.jaxws.v201809.cm.Campaign) InputStreamReader(java.io.InputStreamReader) Diff(org.xmlunit.diff.Diff) QName(javax.xml.namespace.QName) CampaignOperation(com.google.api.ads.adwords.jaxws.v201809.cm.CampaignOperation) JaxBSerializer(com.google.api.ads.common.lib.soap.jaxb.JaxBSerializer) AdGroup(com.google.api.ads.adwords.jaxws.v201809.cm.AdGroup) AdGroupOperation(com.google.api.ads.adwords.jaxws.v201809.cm.AdGroupOperation) Test(org.junit.Test)

Example 7 with AdGroup

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

the class AddSmartShoppingAd method createSmartShoppingAdGroup.

/**
 * Creates a Smart Shopping ad group by setting the ad group type to SHOPPING_GOAL_OPTIMIZED_ADS.
 */
private static AdGroup createSmartShoppingAdGroup(AdWordsServicesInterface adWordsServices, AdWordsSession session, long campaignId) throws RemoteException {
    // Get the AdGroupService.
    AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
    // Create ad group.
    AdGroup adGroup = new AdGroup();
    adGroup.setCampaignId(campaignId);
    adGroup.setName("Smart Shopping ad group #" + System.currentTimeMillis());
    // Set the ad group type to SHOPPING_GOAL_OPTIMIZED_ADS.
    adGroup.setAdGroupType(AdGroupType.SHOPPING_GOAL_OPTIMIZED_ADS);
    // Create operation.
    AdGroupOperation adGroupOperation = new AdGroupOperation();
    adGroupOperation.setOperand(adGroup);
    adGroupOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    AdGroupReturnValue adGroupAddResult = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation });
    // Display result.
    adGroup = adGroupAddResult.getValue(0);
    System.out.printf("Smart Shopping ad group with name '%s' and ID %d was added.%n", adGroup.getName(), adGroup.getId());
    return adGroup;
}
Also used : AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) AdGroupReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Example 8 with AdGroup

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

the class JaxWsBatchJobResponseDeserializerTest method testDeserializeBatchJobResponseWithoutErrors.

/**
 * Tests that a response without errors will be properly deserialized.
 */
@Test
public void testDeserializeBatchJobResponseWithoutErrors() throws Exception {
    BatchJobMutateResponse response = deserializer.deserializeBatchJobMutateResults(BatchJobMutateResponse.class, JaxWsBatchJobResponseDeserializerTest.class.getResource("resources/BatchJobMutate.responseWithoutErrors.xml"));
    assertNotNull(response);
    // Expect: Campaign, AdGroup, BiddableAdGroupCriterion, BiddableAdGroupCriterion
    assertEquals(4, response.getMutateResults().length);
    assertNotNull(response.getMutateResults()[0].getOperand().getCampaign());
    assertNotNull(response.getMutateResults()[1].getOperand().getAdGroup());
    assertNotNull(response.getMutateResults()[2].getOperand().getAdGroupCriterion());
    assertThat(response.getMutateResults()[3].getOperand().getAdGroupCriterion(), Matchers.instanceOf(BiddableAdGroupCriterion.class));
    assertNotNull(response.getMutateResults()[3].getOperand().getAdGroupCriterion());
    assertThat(response.getMutateResults()[3].getOperand().getAdGroupCriterion(), Matchers.instanceOf(BiddableAdGroupCriterion.class));
}
Also used : BatchJobMutateResponse(com.google.api.ads.adwords.jaxws.utils.v201809.batchjob.BatchJobMutateResponse) BiddableAdGroupCriterion(com.google.api.ads.adwords.jaxws.v201809.cm.BiddableAdGroupCriterion) Test(org.junit.Test)

Example 9 with AdGroup

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

the class AddSmartShoppingAd method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param merchantId the Merchant Center ID for the new campaign.
 * @param createDefaultPartition if true, a default product partition for all products will be
 *     created.
 * @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 merchantId, boolean createDefaultPartition) throws RemoteException {
    Budget budget = createBudget(adWordsServices, session);
    Campaign campaign = createSmartShoppingCampaign(adWordsServices, session, budget.getBudgetId(), merchantId);
    AdGroup adGroup = createSmartShoppingAdGroup(adWordsServices, session, campaign.getId());
    createSmartShoppingAd(adWordsServices, session, adGroup.getId());
    if (createDefaultPartition) {
        createDefaultPartition(adWordsServices, session, adGroup.getId());
    }
}
Also used : Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) Budget(com.google.api.ads.adwords.axis.v201809.cm.Budget) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup)

Example 10 with AdGroup

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

the class AddShoppingCampaign method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param budgetId the budget ID to use for the new campaign.
 * @param merchantId the Merchant Center ID for the new campaign.
 * @param createDefaultPartition if true, a default product partition for all products will be
 *     created.
 * @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 budgetId, Long merchantId, boolean createDefaultPartition) throws RemoteException {
    // Get the CampaignService
    CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
    // Create campaign.
    Campaign campaign = new Campaign();
    campaign.setName("Shopping campaign #" + System.currentTimeMillis());
    // The advertisingChannelType is what makes this a Shopping campaign
    campaign.setAdvertisingChannelType(AdvertisingChannelType.SHOPPING);
    // Recommendation: Set the campaign to PAUSED when creating it to prevent
    // the ads from immediately serving. Set to ENABLED once you've added
    // targeting and the ads are ready to serve.
    campaign.setStatus(CampaignStatus.PAUSED);
    // Set shared budget (required).
    Budget budget = new Budget();
    budget.setBudgetId(budgetId);
    campaign.setBudget(budget);
    // Set bidding strategy (required).
    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
    biddingStrategyConfiguration.setBiddingStrategyType(BiddingStrategyType.MANUAL_CPC);
    campaign.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
    // All Shopping campaigns need a ShoppingSetting.
    ShoppingSetting shoppingSetting = new ShoppingSetting();
    shoppingSetting.setSalesCountry("US");
    shoppingSetting.setCampaignPriority(0);
    shoppingSetting.setMerchantId(merchantId);
    // Set to 'true' to enable Local Inventory Ads in your campaign.
    shoppingSetting.setEnableLocal(true);
    campaign.setSettings(new Setting[] { shoppingSetting });
    // Create operation.
    CampaignOperation campaignOperation = new CampaignOperation();
    campaignOperation.setOperand(campaign);
    campaignOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    CampaignReturnValue campaignAddResult = campaignService.mutate(new CampaignOperation[] { campaignOperation });
    // Display result.
    campaign = campaignAddResult.getValue(0);
    System.out.printf("Campaign with name '%s' and ID %d was added.%n", campaign.getName(), campaign.getId());
    // Get the AdGroupService.
    AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
    // Create ad group.
    AdGroup adGroup = new AdGroup();
    adGroup.setCampaignId(campaign.getId());
    adGroup.setName("Ad Group #" + System.currentTimeMillis());
    // Create operation.
    AdGroupOperation adGroupOperation = new AdGroupOperation();
    adGroupOperation.setOperand(adGroup);
    adGroupOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    AdGroupReturnValue adGroupAddResult = adGroupService.mutate(new AdGroupOperation[] { adGroupOperation });
    // Display result.
    adGroup = adGroupAddResult.getValue(0);
    System.out.printf("Ad group with name '%s' and ID %d was added.%n", adGroup.getName(), adGroup.getId());
    // Create product ad.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    ProductAd productAd = new ProductAd();
    // Create ad group ad.
    AdGroupAd adGroupAd = new AdGroupAd();
    adGroupAd.setAdGroupId(adGroup.getId());
    adGroupAd.setAd(productAd);
    // Create operation.
    AdGroupAdOperation adGroupAdOperation = new AdGroupAdOperation();
    adGroupAdOperation.setOperand(adGroupAd);
    adGroupAdOperation.setOperator(Operator.ADD);
    // Make the mutate request.
    AdGroupAdReturnValue adGroupAdAddResult = adGroupAdService.mutate(new AdGroupAdOperation[] { adGroupAdOperation });
    // Display result.
    adGroupAd = adGroupAdAddResult.getValue(0);
    System.out.printf("Product ad with ID %d was added.%n", adGroupAd.getAd().getId());
    if (createDefaultPartition) {
        // Create an ad group criterion for 'All products' using the ProductPartitionTree utility.
        ProductPartitionTree productPartitionTree = ProductPartitionTree.createAdGroupTree(adWordsServices, session, adGroup.getId());
        productPartitionTree.getRoot().asBiddableUnit().setBid(500000L);
        List<AdGroupCriterionOperation> mutateOperations = productPartitionTree.getMutateOperations();
        // Make the mutate request.
        AdGroupCriterionServiceInterface adGroupCriterionService = adWordsServices.get(session, AdGroupCriterionServiceInterface.class);
        AdGroupCriterionReturnValue adGroupCriterionResult = adGroupCriterionService.mutate(mutateOperations.toArray(new AdGroupCriterionOperation[0]));
        // Display result.
        for (AdGroupCriterion adGroupCriterion : adGroupCriterionResult.getValue()) {
            System.out.printf("Ad group criterion with ID %d in ad group with ID %d was added.%n", adGroupCriterion.getCriterion().getId(), adGroupCriterion.getAdGroupId());
        }
    }
}
Also used : ShoppingSetting(com.google.api.ads.adwords.axis.v201809.cm.ShoppingSetting) AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) ProductPartitionTree(com.google.api.ads.adwords.axis.utils.v201809.shopping.ProductPartitionTree) AdGroupCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionServiceInterface) BiddingStrategyConfiguration(com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration) AdGroupAdReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdReturnValue) CampaignOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation) CampaignReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignReturnValue) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) ProductAd(com.google.api.ads.adwords.axis.v201809.cm.ProductAd) AdGroupCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionOperation) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) AdGroupCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterionReturnValue) AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) Budget(com.google.api.ads.adwords.axis.v201809.cm.Budget) AdGroupReturnValue(com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue) AdGroupOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)

Aggregations

AdGroup (com.google.api.ads.adwords.axis.v201809.cm.AdGroup)14 AdGroupOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)9 AdGroupServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface)9 AdGroupReturnValue (com.google.api.ads.adwords.axis.v201809.cm.AdGroupReturnValue)6 Test (org.junit.Test)6 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)5 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)5 CpcBid (com.google.api.ads.adwords.axis.v201809.cm.CpcBid)4 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)4 BatchJobMutateResponse (com.google.api.ads.adwords.jaxws.utils.v201809.batchjob.BatchJobMutateResponse)4 BiddableAdGroupCriterion (com.google.api.ads.adwords.jaxws.v201809.cm.BiddableAdGroupCriterion)4 Budget (com.google.api.ads.adwords.axis.v201809.cm.Budget)3 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)3 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)2 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)2 AdGroupPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupPage)2 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)2 CriterionError (com.google.api.ads.adwords.jaxws.v201809.cm.CriterionError)2 InputStreamReader (java.io.InputStreamReader)2 Source (javax.xml.transform.Source)2