Search in sources :

Example 1 with FeedMapping

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

the class AddGoogleMyBusinessLocationExtensions method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param gmbEmailAddress the email address of the owner or manager of the GMB account.
 * @param gmbAccessToken the OAuth2 access token for GMB.
 * @param businessAccountIdentifier optional identifier of the Google My Business account. This is
 *     required when the {@code gmbEmailAddress} is a GMB manager.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws InterruptedException if the thread was interrupted while sleeping between retries.
 */
private static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, String gmbEmailAddress, String gmbAccessToken, @Nullable String businessAccountIdentifier) throws RemoteException, InterruptedException {
    FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
    // Create a feed that will sync to the Google My Business account specified
    // by gmbEmailAddress. Do not add FeedAttributes to this object,
    // as AdWords will add them automatically because this will be a
    // system generated feed.
    Feed gmbFeed = new Feed();
    gmbFeed.setName("Google My Business feed #" + System.currentTimeMillis());
    PlacesLocationFeedData feedData = new PlacesLocationFeedData();
    feedData.setEmailAddress(gmbEmailAddress);
    feedData.setBusinessAccountIdentifier(businessAccountIdentifier);
    // Optional: specify labels to filter Google My Business listings. If
    // specified, only listings that have any of the labels set are
    // synchronized into FeedItems.
    feedData.setLabelFilters(new String[] { "Stores in New York City" });
    OAuthInfo oAuthInfo = new OAuthInfo();
    oAuthInfo.setHttpMethod("GET");
    oAuthInfo.setHttpRequestUrl(GetRefreshToken.ADWORDS_API_SCOPE);
    oAuthInfo.setHttpAuthorizationHeader(String.format("Bearer %s", gmbAccessToken));
    feedData.setOAuthInfo(oAuthInfo);
    gmbFeed.setSystemFeedGenerationData(feedData);
    // Since this feed's feed items will be managed by AdWords,
    // you must set its origin to ADWORDS.
    gmbFeed.setOrigin(FeedOrigin.ADWORDS);
    // Create an operation to add the feed.
    FeedOperation feedOperation = new FeedOperation();
    feedOperation.setOperand(gmbFeed);
    feedOperation.setOperator(Operator.ADD);
    // Add the feed. Since it is a system generated feed, AdWords will automatically:
    // 1. Set up the FeedAttributes on the feed.
    // 2. Set up a FeedMapping that associates the FeedAttributes of the feed
    // with the placeholder fields of the LOCATION placeholder type.
    FeedReturnValue addFeedResult = feedService.mutate(new FeedOperation[] { feedOperation });
    Feed addedFeed = addFeedResult.getValue(0);
    System.out.printf("Added GMB feed with ID %d%n", addedFeed.getId());
    // Add a CustomerFeed that associates the feed with this customer for
    // the LOCATION placeholder type.
    CustomerFeed customerFeed = new CustomerFeed();
    customerFeed.setFeedId(addedFeed.getId());
    customerFeed.setPlaceholderTypes(new int[] { PLACEHOLDER_LOCATION });
    // Create a matching function that will always evaluate to true.
    Function customerMatchingFunction = new Function();
    ConstantOperand constOperand = new ConstantOperand();
    constOperand.setType(ConstantOperandConstantType.BOOLEAN);
    constOperand.setBooleanValue(true);
    customerMatchingFunction.setLhsOperand(new FunctionArgumentOperand[] { constOperand });
    customerMatchingFunction.setOperator(FunctionOperator.IDENTITY);
    customerFeed.setMatchingFunction(customerMatchingFunction);
    // Create an operation to add the customer feed.
    CustomerFeedOperation customerFeedOperation = new CustomerFeedOperation();
    customerFeedOperation.setOperand(customerFeed);
    customerFeedOperation.setOperator(Operator.ADD);
    CustomerFeedServiceInterface customerFeedService = adWordsServices.get(session, CustomerFeedServiceInterface.class);
    // After the completion of the Feed ADD operation above the added feed will not be available
    // for usage in a CustomerFeed until the sync between the AdWords and GMB accounts
    // completes. The loop below will retry adding the CustomerFeed up to ten times with an
    // exponential back-off policy.
    CustomerFeed addedCustomerFeed = null;
    int numberOfAttempts = 0;
    do {
        numberOfAttempts++;
        try {
            CustomerFeedReturnValue customerFeedResult = customerFeedService.mutate(new CustomerFeedOperation[] { customerFeedOperation });
            addedCustomerFeed = customerFeedResult.getValue(0);
            System.out.printf("Attempt #%d to add the CustomerFeed was successful%n", numberOfAttempts);
        } catch (Exception e) {
            // Wait using exponential backoff policy
            long sleepSeconds = (long) Math.scalb(5, numberOfAttempts);
            System.out.printf("Attempt #%d to add the CustomerFeed was not successful. " + "Waiting %d seconds before trying again.%n", numberOfAttempts, sleepSeconds);
            Thread.sleep(sleepSeconds * 1000);
        }
    } while (numberOfAttempts < MAX_CUSTOMER_FEED_ADD_ATTEMPTS && addedCustomerFeed == null);
    if (addedCustomerFeed == null) {
        throw new RuntimeException("Could not create the CustomerFeed after " + MAX_CUSTOMER_FEED_ADD_ATTEMPTS + " attempts. Please retry " + "the CustomerFeed ADD operation later.");
    }
    System.out.printf("Added CustomerFeed for feed ID %d and placeholder type %d%n", addedCustomerFeed.getFeedId(), addedCustomerFeed.getPlaceholderTypes()[0]);
// OPTIONAL: Create a CampaignFeed to specify which FeedItems to use at the Campaign
// level. This will be similar to the CampaignFeed in the AddSiteLinks example, except
// you can also filter based on the business name and category of each FeedItem
// by using a FeedAttributeOperand in your matching function.
// OPTIONAL: Create an AdGroupFeed for even more fine grained control over
// which feed items are used at the AdGroup level.
}
Also used : ConstantOperand(com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand) FeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.FeedReturnValue) CustomerFeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedReturnValue) CustomerFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedServiceInterface) PlacesLocationFeedData(com.google.api.ads.adwords.axis.v201809.cm.PlacesLocationFeedData) FeedOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedOperation) CustomerFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedOperation) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) RemoteException(java.rmi.RemoteException) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) Function(com.google.api.ads.adwords.axis.v201809.cm.Function) CustomerFeed(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeed) FeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface) CustomerFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedServiceInterface) OAuthInfo(com.google.api.ads.adwords.axis.v201809.cm.OAuthInfo) CustomerFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedOperation) CustomerFeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedReturnValue) CustomerFeed(com.google.api.ads.adwords.axis.v201809.cm.CustomerFeed) Feed(com.google.api.ads.adwords.axis.v201809.cm.Feed)

Example 2 with FeedMapping

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

the class AddSiteLinksUsingFeeds method createSiteLinksFeedMapping.

private static void createSiteLinksFeedMapping(AdWordsServicesInterface adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData) throws RemoteException {
    // Get the FeedItemService.
    FeedMappingServiceInterface feedMappingService = adWordsServices.get(session, FeedMappingServiceInterface.class);
    // Map the FeedAttributeIds to the fieldId constants.
    AttributeFieldMapping linkTextFieldMapping = new AttributeFieldMapping();
    linkTextFieldMapping.setFeedAttributeId(siteLinksData.linkTextFeedAttributeId);
    linkTextFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_LINK_TEXT);
    AttributeFieldMapping linkFinalUrlFieldMapping = new AttributeFieldMapping();
    linkFinalUrlFieldMapping.setFeedAttributeId(siteLinksData.linkFinalUrlFeedAttributeId);
    linkFinalUrlFieldMapping.setFieldId(PLACEHOLDER_FIELD_SITELINK_FINAL_URL);
    AttributeFieldMapping line2FieldMapping = new AttributeFieldMapping();
    line2FieldMapping.setFeedAttributeId(siteLinksData.line2FeedAttributeId);
    line2FieldMapping.setFieldId(PLACEHOLDER_FIELD_LINE_2_TEXT);
    AttributeFieldMapping line3FieldMapping = new AttributeFieldMapping();
    line3FieldMapping.setFeedAttributeId(siteLinksData.line3FeedAttributeId);
    line3FieldMapping.setFieldId(PLACEHOLDER_FIELD_LINE_3_TEXT);
    // Create the FeedMapping and operation.
    FeedMapping feedMapping = new FeedMapping();
    feedMapping.setPlaceholderType(PLACEHOLDER_SITELINKS);
    feedMapping.setFeedId(siteLinksData.siteLinksFeedId);
    feedMapping.setAttributeFieldMappings(new AttributeFieldMapping[] { linkTextFieldMapping, linkFinalUrlFieldMapping, line2FieldMapping, line3FieldMapping });
    FeedMappingOperation operation = new FeedMappingOperation();
    operation.setOperand(feedMapping);
    operation.setOperator(Operator.ADD);
    // Save the field mapping.
    FeedMappingReturnValue result = feedMappingService.mutate(new FeedMappingOperation[] { operation });
    for (FeedMapping savedFeedMapping : result.getValue()) {
        System.out.printf("Feed mapping with ID %d and placeholderType %d was saved for feed with ID %d.%n", savedFeedMapping.getFeedMappingId(), savedFeedMapping.getPlaceholderType(), savedFeedMapping.getFeedId());
    }
}
Also used : AttributeFieldMapping(com.google.api.ads.adwords.axis.v201809.cm.AttributeFieldMapping) FeedMappingReturnValue(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingReturnValue) FeedMappingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingServiceInterface) FeedMapping(com.google.api.ads.adwords.axis.v201809.cm.FeedMapping) FeedMappingOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingOperation)

Example 3 with FeedMapping

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

the class AddDynamicPageFeed method createFeedMapping.

/**
 * Creates the feed mapping for the DSA page feeds.
 */
private static void createFeedMapping(AdWordsServicesInterface adWordsServices, AdWordsSession session, DSAFeedDetails feedDetails) throws RemoteException {
    // Get the FeedMappingService.
    FeedMappingServiceInterface feedMappingService = adWordsServices.get(session, FeedMappingServiceInterface.class);
    // Map the FeedAttributeIds to the fieldId constants.
    AttributeFieldMapping urlFieldMapping = new AttributeFieldMapping();
    urlFieldMapping.setFeedAttributeId(feedDetails.urlAttributeId);
    urlFieldMapping.setFieldId(DSA_PAGE_URLS_FIELD_ID);
    AttributeFieldMapping labelFieldMapping = new AttributeFieldMapping();
    labelFieldMapping.setFeedAttributeId(feedDetails.labelAttributeId);
    labelFieldMapping.setFieldId(DSA_LABEL_FIELD_ID);
    // Create the FeedMapping and operation.
    FeedMapping feedMapping = new FeedMapping();
    feedMapping.setCriterionType(DSA_PAGE_FEED_CRITERION_TYPE);
    feedMapping.setFeedId(feedDetails.feedId);
    feedMapping.setAttributeFieldMappings(new AttributeFieldMapping[] { urlFieldMapping, labelFieldMapping });
    FeedMappingOperation operation = new FeedMappingOperation();
    operation.setOperand(feedMapping);
    operation.setOperator(Operator.ADD);
    // Add the field mapping.
    FeedMapping newFeedMapping = feedMappingService.mutate(new FeedMappingOperation[] { operation }).getValue(0);
    System.out.printf("Feed mapping with ID %d and criterionType %d was saved for feed with ID %d.%n", newFeedMapping.getFeedMappingId(), newFeedMapping.getCriterionType(), newFeedMapping.getFeedId());
}
Also used : AttributeFieldMapping(com.google.api.ads.adwords.axis.v201809.cm.AttributeFieldMapping) FeedMappingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingServiceInterface) FeedMapping(com.google.api.ads.adwords.axis.v201809.cm.FeedMapping) FeedMappingOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingOperation)

Example 4 with FeedMapping

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

the class MigrateToExtensionSettings method getFeedMapping.

/**
 * Gets the feed mapping for a feed.
 *
 * @return a multimap from feed attribute ID to the set of field IDs mapped to the attribute
 */
private static Multimap<Long, Integer> getFeedMapping(AdWordsServicesInterface adWordsServices, AdWordsSession session, Feed feed, long placeholderType) throws RemoteException {
    // Get the FeedMappingService.
    FeedMappingServiceInterface feedMappingService = adWordsServices.get(session, FeedMappingServiceInterface.class);
    String query = String.format("SELECT FeedMappingId, AttributeFieldMappings WHERE FeedId = %d and PlaceholderType = %d " + "AND Status = 'ENABLED'", feed.getId(), placeholderType);
    Multimap<Long, Integer> attributeMappings = HashMultimap.create();
    int offset = 0;
    FeedMappingPage feedMappingPage;
    do {
        String pageQuery = String.format(query + " LIMIT %d, %d", offset, PAGE_SIZE);
        feedMappingPage = feedMappingService.query(pageQuery);
        if (feedMappingPage.getEntries() != null) {
            // than one field if needed.
            for (FeedMapping feedMapping : feedMappingPage.getEntries()) {
                for (AttributeFieldMapping attributeMapping : feedMapping.getAttributeFieldMappings()) {
                    attributeMappings.put(attributeMapping.getFeedAttributeId(), attributeMapping.getFieldId());
                }
            }
        }
        offset += PAGE_SIZE;
    } while (offset < feedMappingPage.getTotalNumEntries());
    return attributeMappings;
}
Also used : FeedMappingPage(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingPage) AttributeFieldMapping(com.google.api.ads.adwords.axis.v201809.cm.AttributeFieldMapping) FeedMappingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingServiceInterface) FeedMapping(com.google.api.ads.adwords.axis.v201809.cm.FeedMapping)

Aggregations

AttributeFieldMapping (com.google.api.ads.adwords.axis.v201809.cm.AttributeFieldMapping)3 FeedMapping (com.google.api.ads.adwords.axis.v201809.cm.FeedMapping)3 FeedMappingServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.FeedMappingServiceInterface)3 FeedMappingOperation (com.google.api.ads.adwords.axis.v201809.cm.FeedMappingOperation)2 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 ConstantOperand (com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand)1 CustomerFeed (com.google.api.ads.adwords.axis.v201809.cm.CustomerFeed)1 CustomerFeedOperation (com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedOperation)1 CustomerFeedReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedReturnValue)1 CustomerFeedServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CustomerFeedServiceInterface)1 Feed (com.google.api.ads.adwords.axis.v201809.cm.Feed)1 FeedMappingPage (com.google.api.ads.adwords.axis.v201809.cm.FeedMappingPage)1 FeedMappingReturnValue (com.google.api.ads.adwords.axis.v201809.cm.FeedMappingReturnValue)1 FeedOperation (com.google.api.ads.adwords.axis.v201809.cm.FeedOperation)1 FeedReturnValue (com.google.api.ads.adwords.axis.v201809.cm.FeedReturnValue)1 FeedServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface)1 Function (com.google.api.ads.adwords.axis.v201809.cm.Function)1 OAuthInfo (com.google.api.ads.adwords.axis.v201809.cm.OAuthInfo)1 PlacesLocationFeedData (com.google.api.ads.adwords.axis.v201809.cm.PlacesLocationFeedData)1 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)1