Search in sources :

Example 1 with FeedOperation

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

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

the class AddDynamicPageFeed method createFeed.

/**
 * Creates the feed for DSA page URLs.
 */
private static DSAFeedDetails createFeed(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws ApiException, RemoteException {
    // Get the FeedService.
    FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
    // Create attributes.
    FeedAttribute urlAttribute = new FeedAttribute();
    urlAttribute.setType(FeedAttributeType.URL_LIST);
    urlAttribute.setName("Page URL");
    FeedAttribute labelAttribute = new FeedAttribute();
    labelAttribute.setType(FeedAttributeType.STRING_LIST);
    labelAttribute.setName("Label");
    // Create the feed.
    Feed dsaPageFeed = new Feed();
    dsaPageFeed.setName("DSA Feed #" + System.currentTimeMillis());
    dsaPageFeed.setAttributes(new FeedAttribute[] { urlAttribute, labelAttribute });
    dsaPageFeed.setOrigin(FeedOrigin.USER);
    // Create operation.
    FeedOperation operation = new FeedOperation();
    operation.setOperand(dsaPageFeed);
    operation.setOperator(Operator.ADD);
    // Add the feed.
    Feed newFeed = feedService.mutate(new FeedOperation[] { operation }).getValue(0);
    DSAFeedDetails feedDetails = new DSAFeedDetails();
    feedDetails.feedId = newFeed.getId();
    FeedAttribute[] savedAttributes = newFeed.getAttributes();
    feedDetails.urlAttributeId = savedAttributes[0].getId();
    feedDetails.labelAttributeId = savedAttributes[1].getId();
    System.out.printf("Feed with name '%s' and ID %d with urlAttributeId %d" + " and labelAttributeId %d was created.%n", newFeed.getName(), feedDetails.feedId, feedDetails.urlAttributeId, feedDetails.labelAttributeId);
    return feedDetails;
}
Also used : FeedAttribute(com.google.api.ads.adwords.axis.v201809.cm.FeedAttribute) FeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface) FeedOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedOperation) Feed(com.google.api.ads.adwords.axis.v201809.cm.Feed) PageFeed(com.google.api.ads.adwords.axis.v201809.cm.PageFeed)

Example 3 with FeedOperation

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

the class AddAdCustomizer method createCustomizerFeed.

/**
 * Creates a new AdCustomizerFeed.
 *
 * @param feedName the name of the new AdCustomizerFeed
 * @return The new AdCustomizerFeed
 */
private static AdCustomizerFeed createCustomizerFeed(AdWordsServicesInterface adWordsServices, AdWordsSession session, String feedName) throws RemoteException {
    // Get the AdCustomizerFeedService.
    AdCustomizerFeedServiceInterface adCustomizerFeedService = adWordsServices.get(session, AdCustomizerFeedServiceInterface.class);
    AdCustomizerFeed customizerFeed = new AdCustomizerFeed();
    customizerFeed.setFeedName(feedName);
    AdCustomizerFeedAttribute nameAttribute = new AdCustomizerFeedAttribute();
    nameAttribute.setName("Name");
    nameAttribute.setType(AdCustomizerFeedAttributeType.STRING);
    AdCustomizerFeedAttribute priceAttribute = new AdCustomizerFeedAttribute();
    priceAttribute.setName("Price");
    priceAttribute.setType(AdCustomizerFeedAttributeType.STRING);
    AdCustomizerFeedAttribute dateAttribute = new AdCustomizerFeedAttribute();
    dateAttribute.setName("Date");
    dateAttribute.setType(AdCustomizerFeedAttributeType.DATE_TIME);
    customizerFeed.setFeedAttributes(new AdCustomizerFeedAttribute[] { nameAttribute, priceAttribute, dateAttribute });
    AdCustomizerFeedOperation feedOperation = new AdCustomizerFeedOperation();
    feedOperation.setOperand(customizerFeed);
    feedOperation.setOperator(Operator.ADD);
    AdCustomizerFeed addedFeed = adCustomizerFeedService.mutate(new AdCustomizerFeedOperation[] { feedOperation }).getValue()[0];
    System.out.printf("Created ad customizer feed with ID %d, name '%s' and attributes:%n", addedFeed.getFeedId(), addedFeed.getFeedName());
    for (AdCustomizerFeedAttribute feedAttribute : addedFeed.getFeedAttributes()) {
        System.out.printf("  ID: %d, name: '%s', type: %s%n", feedAttribute.getId(), feedAttribute.getName(), feedAttribute.getType());
    }
    return addedFeed;
}
Also used : AdCustomizerFeedAttribute(com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeedAttribute) AdCustomizerFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeedServiceInterface) AdCustomizerFeed(com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeed) AdCustomizerFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeedOperation)

Example 4 with FeedOperation

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

the class AddSiteLinksUsingFeeds method createSiteLinksFeed.

private static void createSiteLinksFeed(AdWordsServicesInterface adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData, String feedName) throws RemoteException {
    // Get the FeedService.
    FeedServiceInterface feedService = adWordsServices.get(session, FeedServiceInterface.class);
    // Create attributes.
    FeedAttribute textAttribute = new FeedAttribute();
    textAttribute.setType(FeedAttributeType.STRING);
    textAttribute.setName("Link Text");
    FeedAttribute finalUrlAttribute = new FeedAttribute();
    finalUrlAttribute.setType(FeedAttributeType.URL_LIST);
    finalUrlAttribute.setName("Link Final URLs");
    FeedAttribute line2Attribute = new FeedAttribute();
    line2Attribute.setType(FeedAttributeType.STRING);
    line2Attribute.setName("Line 2");
    FeedAttribute line3Attribute = new FeedAttribute();
    line3Attribute.setType(FeedAttributeType.STRING);
    line3Attribute.setName("Line 3");
    // Create the feed.
    Feed siteLinksFeed = new Feed();
    siteLinksFeed.setName(feedName);
    siteLinksFeed.setAttributes(new FeedAttribute[] { textAttribute, finalUrlAttribute, line2Attribute, line3Attribute });
    siteLinksFeed.setOrigin(FeedOrigin.USER);
    // Create operation.
    FeedOperation operation = new FeedOperation();
    operation.setOperand(siteLinksFeed);
    operation.setOperator(Operator.ADD);
    // Add the feed.
    FeedReturnValue result = feedService.mutate(new FeedOperation[] { operation });
    Feed savedFeed = result.getValue()[0];
    siteLinksData.siteLinksFeedId = savedFeed.getId();
    FeedAttribute[] savedAttributes = savedFeed.getAttributes();
    siteLinksData.linkTextFeedAttributeId = savedAttributes[0].getId();
    siteLinksData.linkFinalUrlFeedAttributeId = savedAttributes[1].getId();
    siteLinksData.line2FeedAttributeId = savedAttributes[2].getId();
    siteLinksData.line3FeedAttributeId = savedAttributes[3].getId();
    System.out.printf("Feed with name '%s' and ID %d with linkTextAttributeId %d" + " and linkFinalUrlAttributeId %d and line2AttributeId %d" + " and line3AttributeId %d was created.%n", savedFeed.getName(), savedFeed.getId(), savedAttributes[0].getId(), savedAttributes[1].getId(), savedAttributes[2].getId(), savedAttributes[3].getId());
}
Also used : FeedAttribute(com.google.api.ads.adwords.axis.v201809.cm.FeedAttribute) FeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.FeedReturnValue) CampaignFeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedReturnValue) CampaignFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedServiceInterface) FeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface) FeedOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedOperation) CampaignFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedOperation) CampaignFeed(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed) Feed(com.google.api.ads.adwords.axis.v201809.cm.Feed)

Aggregations

Feed (com.google.api.ads.adwords.axis.v201809.cm.Feed)3 FeedOperation (com.google.api.ads.adwords.axis.v201809.cm.FeedOperation)3 FeedServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface)3 FeedAttribute (com.google.api.ads.adwords.axis.v201809.cm.FeedAttribute)2 FeedReturnValue (com.google.api.ads.adwords.axis.v201809.cm.FeedReturnValue)2 AdCustomizerFeed (com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeed)1 AdCustomizerFeedAttribute (com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeedAttribute)1 AdCustomizerFeedOperation (com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeedOperation)1 AdCustomizerFeedServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdCustomizerFeedServiceInterface)1 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 CampaignFeed (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed)1 CampaignFeedOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedOperation)1 CampaignFeedReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedReturnValue)1 CampaignFeedServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedServiceInterface)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 Function (com.google.api.ads.adwords.axis.v201809.cm.Function)1