Search in sources :

Example 1 with CustomParameter

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

the class ProductPartitionNodeAdapterTest method testCommonAttributes.

/**
 * Asserts that the attributes of {@code adGroupCriterion} match expectations.
 *
 * @param node the node from which the criterion was built
 * @param adGroupCriterion the criterion created by {@link ProductPartitionNodeAdapter}
 * @param isForRemove if true, this method will only check the attributes required for a REMOVE
 *        operation
 */
private void testCommonAttributes(ProductPartitionNode node, AdGroupCriterion adGroupCriterion, boolean isForRemove) {
    assertEquals("Ad group ID is incorrect", adGroupId, adGroupCriterion.getAdGroupId());
    Criterion criterion = adGroupCriterion.getCriterion();
    assertTrue("Criterion should be a ProductPartition", criterion instanceof ProductPartition);
    assertEquals("Partition ID is incorrect", node.getProductPartitionId(), criterion.getId());
    if (isForRemove) {
        assertEquals("Type of AdGroupCriterion for REMOVE should be the base class", AdGroupCriterion.class, adGroupCriterion.getClass());
        // The above checks suffice for REMOVE operations.
        return;
    }
    ProductPartition partition = (ProductPartition) criterion;
    assertEquals("The caseValue of the partition does not match the dimension of the node", 0, new ProductDimensionComparator().compare(partition.getCaseValue(), node.getDimension()));
    if (node.getParent() == null) {
        assertNull("Parent ID should be null", partition.getParentCriterionId());
    } else {
        assertEquals("Parent ID does not match ID of parent node", node.getParent().getProductPartitionId(), partition.getParentCriterionId());
    }
    if (node.isBiddableUnit()) {
        assertTrue("Biddable node should be translated into a BiddableAdGroupCriterion", adGroupCriterion instanceof BiddableAdGroupCriterion);
        BiddableAdGroupCriterion biddableCriterion = (BiddableAdGroupCriterion) adGroupCriterion;
        BiddingStrategyConfiguration biddingStrategyConfig = biddableCriterion.getBiddingStrategyConfiguration();
        if (node.getBid() == null) {
            assertArrayEquals(new Bids[0], biddingStrategyConfig.getBids());
        } else {
            Bids bid = biddingStrategyConfig.getBids(0);
            assertTrue("Bid should be a CpcBid", bid instanceof CpcBid);
            CpcBid cpcBid = (CpcBid) bid;
            assertEquals("Bid amount is incorrect", node.getBid(), cpcBid.getBid().getMicroAmount());
            assertEquals("Partition is not a UNIT partition", ProductPartitionType.UNIT, partition.getPartitionType());
        }
        assertEquals("tracking URL template is incorrect", node.getTrackingUrlTemplate(), biddableCriterion.getTrackingUrlTemplate());
        // The adapter should always have a CustomParameters object, even if the node had no params.
        // This ensures that the parameters will be cleared (via doReplace=true) if all params were
        // removed from the node.
        CustomParameters customParameters = biddableCriterion.getUrlCustomParameters();
        assertNotNull("Biddable criterion does not have custom parameters", customParameters);
        assertEquals("doReplace for custom parameters should always be true", true, customParameters.getDoReplace());
        // Convert the BiddableAdGroupCriterion's custom parameters to a map to simplify comparison
        // against the node's custom parameter map.
        Map<String, String> actualCustomParameters = new HashMap<>();
        for (CustomParameter customParameter : customParameters.getParameters()) {
            actualCustomParameters.put(customParameter.getKey(), customParameter.getValue());
        }
        assertEquals("node and criterion do not have the same custom parameters", node.getCustomParameters(), actualCustomParameters);
    } else {
        assertTrue("Excluded node should be translated into a NegativeAdGroupCriterion", adGroupCriterion instanceof NegativeAdGroupCriterion);
    }
}
Also used : HashMap(java.util.HashMap) BiddingStrategyConfiguration(com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration) Bids(com.google.api.ads.adwords.axis.v201809.cm.Bids) CpcBid(com.google.api.ads.adwords.axis.v201809.cm.CpcBid) ProductPartition(com.google.api.ads.adwords.axis.v201809.cm.ProductPartition) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) NegativeAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion) AdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion) Criterion(com.google.api.ads.adwords.axis.v201809.cm.Criterion) BiddableAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion) CustomParameter(com.google.api.ads.adwords.axis.v201809.cm.CustomParameter) CustomParameters(com.google.api.ads.adwords.axis.v201809.cm.CustomParameters) NegativeAdGroupCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion)

Example 2 with CustomParameter

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

the class ProductPartitionNodeAdapter method createCustomParameters.

/**
 * Creates an AdWords API {@link CustomParameters} object using the map of custom parameters
 * on the node.
 *
 * @param node the node.
 */
private static CustomParameters createCustomParameters(ProductPartitionNode node) {
    Preconditions.checkArgument(node.isBiddableUnit(), "Node is not a biddable unit. Custom parameters not supported.");
    CustomParameters customParameters = new CustomParameters();
    List<CustomParameter> parameters = new ArrayList<>();
    for (Entry<String, String> customParamEntry : node.getCustomParameters().entrySet()) {
        CustomParameter customParameter = new CustomParameter();
        customParameter.setKey(customParamEntry.getKey());
        customParameter.setValue(customParamEntry.getValue());
        parameters.add(customParameter);
    }
    customParameters.setParameters(parameters.toArray(new CustomParameter[0]));
    // Always replace all custom parameters. This attribute is ignored on ADD.
    customParameters.setDoReplace(true);
    return customParameters;
}
Also used : ArrayList(java.util.ArrayList) CustomParameter(com.google.api.ads.adwords.axis.v201809.cm.CustomParameter) CustomParameters(com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)

Example 3 with CustomParameter

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

the class AddExpandedTextAdWithUpgradedUrls method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the ID of the ad group where the ad 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 adGroupId) throws RemoteException {
    // Get the AdGroupAdService.
    AdGroupAdServiceInterface adGroupAdService = adWordsServices.get(session, AdGroupAdServiceInterface.class);
    // Create expanded text ad with a tracking template and custom parameters.
    ExpandedTextAd expandedTextAd = new ExpandedTextAd();
    expandedTextAd.setHeadlinePart1("Luxury Cruise to Mars");
    expandedTextAd.setHeadlinePart2("Visit the Red Planet in style.");
    expandedTextAd.setDescription("Low-gravity fun for everyone!");
    // Specify a tracking url for 3rd party tracking provider. You may
    // specify one at customer, campaign, ad group, ad, criterion or
    // feed item levels.
    expandedTextAd.setTrackingUrlTemplate("http://tracker.example.com/?season={_season}&promocode={_promocode}&u={lpurl}");
    // Since your tracking url has two custom parameters, provide their
    // values too. This can be provided at campaign, ad group, ad, criterion
    // or feed item levels.
    CustomParameter seasonParameter = new CustomParameter();
    seasonParameter.setKey("season");
    seasonParameter.setValue("christmas");
    CustomParameter promoCodeParameter = new CustomParameter();
    promoCodeParameter.setKey("promocode");
    promoCodeParameter.setValue("NYC123");
    CustomParameters trackingUrlParameters = new CustomParameters();
    trackingUrlParameters.setParameters(new CustomParameter[] { seasonParameter, promoCodeParameter });
    expandedTextAd.setUrlCustomParameters(trackingUrlParameters);
    // Specify a list of final urls. This field cannot be set if url field is
    // set. This may be specified at ad, criterion, and feed item levels.
    expandedTextAd.setFinalUrls(new String[] { "http://www.example.com/cruise/space/", "http://www.example.com/locations/mars/" });
    // Specify a list of final mobile urls. This field cannot be set if url field is
    // set or finalUrls is not set. This may be specified at ad, criterion, and feed
    // item levels.
    expandedTextAd.setFinalMobileUrls(new String[] { "http://mobile.example.com/cruise/space/", "http://mobile.example.com/locations/mars/" });
    // Create ad group ad.
    AdGroupAd textAdGroupAd = new AdGroupAd();
    textAdGroupAd.setAdGroupId(adGroupId);
    textAdGroupAd.setAd(expandedTextAd);
    // Optional: Set status.
    textAdGroupAd.setStatus(AdGroupAdStatus.PAUSED);
    // Create operation.
    AdGroupAdOperation textAdGroupAdOperation = new AdGroupAdOperation();
    textAdGroupAdOperation.setOperand(textAdGroupAd);
    textAdGroupAdOperation.setOperator(Operator.ADD);
    AdGroupAdOperation[] operations = new AdGroupAdOperation[] { textAdGroupAdOperation };
    // Add ad.
    AdGroupAd adGroupAdResult = adGroupAdService.mutate(operations).getValue(0);
    // Display ad.
    System.out.printf("Ad with ID %d and tracking URL template '%s' was added.", adGroupAdResult.getAd().getId(), adGroupAdResult.getAd().getTrackingUrlTemplate());
}
Also used : AdGroupAd(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd) AdGroupAdServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface) ExpandedTextAd(com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd) AdGroupAdOperation(com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation) CustomParameter(com.google.api.ads.adwords.axis.v201809.cm.CustomParameter) CustomParameters(com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)

Aggregations

CustomParameter (com.google.api.ads.adwords.axis.v201809.cm.CustomParameter)3 CustomParameters (com.google.api.ads.adwords.axis.v201809.cm.CustomParameters)3 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)1 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)1 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)1 AdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.AdGroupCriterion)1 BiddableAdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.BiddableAdGroupCriterion)1 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)1 Bids (com.google.api.ads.adwords.axis.v201809.cm.Bids)1 CpcBid (com.google.api.ads.adwords.axis.v201809.cm.CpcBid)1 Criterion (com.google.api.ads.adwords.axis.v201809.cm.Criterion)1 ExpandedTextAd (com.google.api.ads.adwords.axis.v201809.cm.ExpandedTextAd)1 NegativeAdGroupCriterion (com.google.api.ads.adwords.axis.v201809.cm.NegativeAdGroupCriterion)1 ProductPartition (com.google.api.ads.adwords.axis.v201809.cm.ProductPartition)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1