Search in sources :

Example 6 with Feed

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

the class MigrateToExtensionSettings method getSiteLinksFromFeed.

/**
 * Gets the site links from a feed.
 *
 * @return a map of feed item ID to SiteLinkFromFeed
 */
private static Map<Long, SiteLinkFromFeed> getSiteLinksFromFeed(AdWordsServicesInterface adWordsServices, AdWordsSession session, Feed feed) throws RemoteException {
    // Retrieve the feed's attribute mapping.
    Multimap<Long, Integer> feedMappings = getFeedMapping(adWordsServices, session, feed, PLACEHOLDER_SITELINKS);
    Map<Long, SiteLinkFromFeed> feedItems = Maps.newHashMap();
    for (FeedItem feedItem : getFeedItems(adWordsServices, session, feed)) {
        SiteLinkFromFeed siteLinkFromFeed = new SiteLinkFromFeed();
        for (FeedItemAttributeValue attributeValue : feedItem.getAttributeValues()) {
            // Skip this attribute if it hasn't been mapped to a field.
            if (!feedMappings.containsKey(attributeValue.getFeedAttributeId())) {
                continue;
            }
            for (Integer fieldId : feedMappings.get(attributeValue.getFeedAttributeId())) {
                switch(fieldId) {
                    case PLACEHOLDER_FIELD_SITELINK_LINK_TEXT:
                        siteLinkFromFeed.text = attributeValue.getStringValue();
                        break;
                    case PLACEHOLDER_FIELD_SITELINK_URL:
                        siteLinkFromFeed.url = attributeValue.getStringValue();
                        break;
                    case PLACEHOLDER_FIELD_FINAL_URLS:
                        siteLinkFromFeed.finalUrls = attributeValue.getStringValues();
                        break;
                    case PLACEHOLDER_FIELD_FINAL_MOBILE_URLS:
                        siteLinkFromFeed.finalMobileUrls = attributeValue.getStringValues();
                        break;
                    case PLACEHOLDER_FIELD_TRACKING_URL_TEMPLATE:
                        siteLinkFromFeed.trackingUrlTemplate = attributeValue.getStringValue();
                        break;
                    case PLACEHOLDER_FIELD_LINE_2_TEXT:
                        siteLinkFromFeed.line2 = attributeValue.getStringValue();
                        break;
                    case PLACEHOLDER_FIELD_LINE_3_TEXT:
                        siteLinkFromFeed.line3 = attributeValue.getStringValue();
                        break;
                    default:
                        // Ignore attributes that do not map to a predefined placeholder field.
                        break;
                }
            }
        }
        feedItems.put(feedItem.getFeedItemId(), siteLinkFromFeed);
    }
    return feedItems;
}
Also used : FeedItem(com.google.api.ads.adwords.axis.v201809.cm.FeedItem) ExtensionFeedItem(com.google.api.ads.adwords.axis.v201809.cm.ExtensionFeedItem) SitelinkFeedItem(com.google.api.ads.adwords.axis.v201809.cm.SitelinkFeedItem) FeedItemAttributeValue(com.google.api.ads.adwords.axis.v201809.cm.FeedItemAttributeValue)

Example 7 with Feed

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

the class MigrateToExtensionSettings method getCampaignFeeds.

/**
 * Returns the campaign feeds that use a particular feed for a particular placeholder type.
 */
private static List<CampaignFeed> getCampaignFeeds(AdWordsServicesInterface adWordsServices, AdWordsSession session, Feed feed, int placeholderType) throws RemoteException {
    // Get the CampaignFeedService.
    CampaignFeedServiceInterface campaignFeedService = adWordsServices.get(session, CampaignFeedServiceInterface.class);
    String query = String.format("SELECT CampaignId, MatchingFunction, PlaceholderTypes WHERE Status = 'ENABLED' " + "AND FeedId = %d AND PlaceholderTypes CONTAINS_ANY [%d]", feed.getId(), placeholderType);
    List<CampaignFeed> campaignFeeds = new ArrayList<>();
    int offset = 0;
    CampaignFeedPage campaignFeedPage;
    do {
        String pageQuery = String.format(query + " LIMIT %d, %d", offset, PAGE_SIZE);
        campaignFeedPage = campaignFeedService.query(pageQuery);
        if (campaignFeedPage.getEntries() != null) {
            campaignFeeds.addAll(Arrays.asList(campaignFeedPage.getEntries()));
        }
        offset += PAGE_SIZE;
    } while (offset < campaignFeedPage.getTotalNumEntries());
    return campaignFeeds;
}
Also used : CampaignFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedServiceInterface) CampaignFeed(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed) CampaignFeedPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedPage) ArrayList(java.util.ArrayList)

Example 8 with Feed

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

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

the class AddPrices method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignId the ID of the campaign where price feed items will be added.
 * @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 ApiException, RemoteException {
    // Get the CustomerExtensionSettingService.
    CustomerExtensionSettingServiceInterface customerExtensionSettingService = adWordsServices.get(session, CustomerExtensionSettingServiceInterface.class);
    // Create the price extension feed item.
    PriceFeedItem priceFeedItem = new PriceFeedItem();
    priceFeedItem.setPriceExtensionType(PriceExtensionType.SERVICES);
    // Price qualifier is optional.
    priceFeedItem.setPriceQualifier(PriceExtensionPriceQualifier.FROM);
    priceFeedItem.setTrackingUrlTemplate("http://tracker.example.com/?u={lpurl}");
    priceFeedItem.setLanguage("en");
    FeedItemCampaignTargeting campaignTargeting = new FeedItemCampaignTargeting();
    campaignTargeting.setTargetingCampaignId(campaignId);
    priceFeedItem.setCampaignTargeting(campaignTargeting);
    priceFeedItem.setScheduling(new FeedItemScheduling(new FeedItemSchedule[] { new FeedItemSchedule(DayOfWeek.SUNDAY, 10, MinuteOfHour.ZERO, 18, MinuteOfHour.ZERO), new FeedItemSchedule(DayOfWeek.SATURDAY, 10, MinuteOfHour.ZERO, 22, MinuteOfHour.ZERO) }));
    // To create a price extension, at least three table rows are needed.
    List<PriceTableRow> priceTableRows = new ArrayList<>();
    String currencyCode = "USD";
    priceTableRows.add(createPriceTableRow("Scrubs", "Body Scrub, Salt Scrub", "http://www.example.com/scrubs", "http://m.example.com/scrubs", 60000000, currencyCode, PriceExtensionPriceUnit.PER_HOUR));
    priceTableRows.add(createPriceTableRow("Hair Cuts", "Once a month", "http://www.example.com/haircuts", "http://m.example.com/haircuts", 75000000, currencyCode, PriceExtensionPriceUnit.PER_MONTH));
    priceTableRows.add(createPriceTableRow("Skin Care Package", "Four times a month", "http://www.example.com/skincarepackage", null, 250000000, currencyCode, PriceExtensionPriceUnit.PER_MONTH));
    priceFeedItem.setTableRows(priceTableRows.toArray(new PriceTableRow[priceTableRows.size()]));
    // Create your campaign extension settings. This associates the sitelinks
    // to your campaign.
    CustomerExtensionSetting customerExtensionSetting = new CustomerExtensionSetting();
    customerExtensionSetting.setExtensionType(FeedType.PRICE);
    ExtensionSetting extensionSetting = new ExtensionSetting();
    extensionSetting.setExtensions(new ExtensionFeedItem[] { priceFeedItem });
    customerExtensionSetting.setExtensionSetting(extensionSetting);
    CustomerExtensionSettingOperation operation = new CustomerExtensionSettingOperation();
    operation.setOperand(customerExtensionSetting);
    operation.setOperator(Operator.ADD);
    // Add the extensions.
    CustomerExtensionSettingReturnValue returnValue = customerExtensionSettingService.mutate(new CustomerExtensionSettingOperation[] { operation });
    if (returnValue.getValue() != null && returnValue.getValue().length > 0) {
        CustomerExtensionSetting newExtensionSetting = returnValue.getValue(0);
        System.out.printf("Extension setting with type '%s' was added.%n", newExtensionSetting.getExtensionType().getValue());
    } else {
        System.out.println("No extension settings were created.");
    }
}
Also used : CustomerExtensionSettingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CustomerExtensionSettingServiceInterface) ExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.ExtensionSetting) CustomerExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.CustomerExtensionSetting) CustomerExtensionSettingOperation(com.google.api.ads.adwords.axis.v201809.cm.CustomerExtensionSettingOperation) FeedItemSchedule(com.google.api.ads.adwords.axis.v201809.cm.FeedItemSchedule) ArrayList(java.util.ArrayList) PriceTableRow(com.google.api.ads.adwords.axis.v201809.cm.PriceTableRow) FeedItemCampaignTargeting(com.google.api.ads.adwords.axis.v201809.cm.FeedItemCampaignTargeting) FeedItemScheduling(com.google.api.ads.adwords.axis.v201809.cm.FeedItemScheduling) PriceFeedItem(com.google.api.ads.adwords.axis.v201809.cm.PriceFeedItem) CustomerExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.CustomerExtensionSetting) CustomerExtensionSettingReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CustomerExtensionSettingReturnValue)

Example 10 with Feed

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

the class AddSiteLinksUsingFeeds method restrictFeedItemToAdGroup.

/**
 * Restricts the first feed item in {@code siteLinksData} to only serve with ads for the specified
 * ad group ID.
 */
private static void restrictFeedItemToAdGroup(AdWordsServicesInterface adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData, Long adGroupId) throws RemoteException {
    FeedItemTargetServiceInterface feedItemTargetService = adWordsServices.get(session, FeedItemTargetServiceInterface.class);
    FeedItemAdGroupTarget feedItemAdGroupTarget = new FeedItemAdGroupTarget();
    feedItemAdGroupTarget.setAdGroupId(adGroupId);
    feedItemAdGroupTarget.setFeedId(siteLinksData.siteLinksFeedId);
    feedItemAdGroupTarget.setFeedItemId(siteLinksData.siteLinkFeedItemIds.get(0));
    FeedItemTargetOperation operation = new FeedItemTargetOperation();
    operation.setOperand(feedItemAdGroupTarget);
    operation.setOperator(Operator.ADD);
    feedItemAdGroupTarget = (FeedItemAdGroupTarget) feedItemTargetService.mutate(new FeedItemTargetOperation[] { operation }).getValue(0);
    System.out.printf("Feed item target for feed ID %d and feed item ID %d was created to restrict serving to " + "ad group ID %d'.%n", feedItemAdGroupTarget.getFeedId(), feedItemAdGroupTarget.getFeedItemId(), feedItemAdGroupTarget.getAdGroupId());
}
Also used : FeedItemTargetServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedItemTargetServiceInterface) FeedItemAdGroupTarget(com.google.api.ads.adwords.axis.v201809.cm.FeedItemAdGroupTarget) FeedItemTargetOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedItemTargetOperation)

Aggregations

ArrayList (java.util.ArrayList)11 FeedItem (com.google.api.ads.adwords.axis.v201809.cm.FeedItem)9 FeedItemOperation (com.google.api.ads.adwords.axis.v201809.cm.FeedItemOperation)7 CampaignFeed (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed)6 CampaignFeedServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedServiceInterface)6 Feed (com.google.api.ads.adwords.axis.v201809.cm.Feed)6 ExtensionFeedItem (com.google.api.ads.adwords.axis.v201809.cm.ExtensionFeedItem)5 FeedItemAttributeValue (com.google.api.ads.adwords.axis.v201809.cm.FeedItemAttributeValue)5 FeedItemServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.FeedItemServiceInterface)5 SitelinkFeedItem (com.google.api.ads.adwords.axis.v201809.cm.SitelinkFeedItem)5 AttributeFieldMapping (com.google.api.ads.adwords.axis.v201809.cm.AttributeFieldMapping)4 CampaignFeedOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedOperation)4 FeedMapping (com.google.api.ads.adwords.axis.v201809.cm.FeedMapping)4 FeedMappingServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.FeedMappingServiceInterface)4 FeedServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface)4 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)3 CampaignFeedPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedPage)3 ConstantOperand (com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand)3 ExtensionSetting (com.google.api.ads.adwords.axis.v201809.cm.ExtensionSetting)3 Function (com.google.api.ads.adwords.axis.v201809.cm.Function)3