Search in sources :

Example 1 with Function

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

the class MigrateToExtensionSettings method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @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) throws RemoteException {
    // Get all of the feeds for the session's account.
    List<Feed> feeds = getFeeds(adWordsServices, session);
    for (Feed feed : feeds) {
        // Retrieve all the sitelinks from the current feed.
        Map<Long, SiteLinkFromFeed> feedItems = getSiteLinksFromFeed(adWordsServices, session, feed);
        // Get all the instances where a sitelink from this feed has been added to a campaign.
        List<CampaignFeed> campaignFeeds = getCampaignFeeds(adWordsServices, session, feed, PLACEHOLDER_SITELINKS);
        Set<Long> allFeedItemsToDelete = Sets.newHashSet();
        for (CampaignFeed campaignFeed : campaignFeeds) {
            // Retrieve the sitelinks that have been associated with this campaign.
            Set<Long> feedItemIds = getFeedItemIdsForCampaign(campaignFeed);
            ExtensionSettingPlatform platformRestrictions = getPlatformRestictionsForCampaign(campaignFeed);
            if (feedItemIds.isEmpty()) {
                System.out.printf("Migration skipped for campaign feed with campaign ID %d " + "and feed ID %d because no mapped feed item IDs were found in the " + "campaign feed's matching function.%n", campaignFeed.getCampaignId(), campaignFeed.getFeedId());
            } else {
                // Delete the campaign feed that associates the sitelinks from the feed to the campaign.
                deleteCampaignFeed(adWordsServices, session, campaignFeed);
                // Create extension settings instead of sitelinks.
                createExtensionSetting(adWordsServices, session, feedItems, campaignFeed, feedItemIds, platformRestrictions);
                // Mark the sitelinks from the feed for deletion.
                allFeedItemsToDelete.addAll(feedItemIds);
            }
        }
        // Delete all the sitelinks from the feed.
        deleteOldFeedItems(adWordsServices, session, allFeedItemsToDelete, feed);
    }
}
Also used : CampaignFeed(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed) ExtensionSettingPlatform(com.google.api.ads.adwords.axis.v201809.cm.ExtensionSettingPlatform) CampaignFeed(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed) Feed(com.google.api.ads.adwords.axis.v201809.cm.Feed)

Example 2 with Function

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

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

the class AddSiteLinksUsingFeeds method createSiteLinksCampaignFeed.

private static void createSiteLinksCampaignFeed(AdWordsServicesInterface adWordsServices, AdWordsSession session, SiteLinksDataHolder siteLinksData, Long campaignId) throws RemoteException {
    // Get the CampaignFeedService.
    CampaignFeedServiceInterface campaignFeedService = adWordsServices.get(session, CampaignFeedServiceInterface.class);
    // Construct a matching function that associates the sitelink feed items to the campaign, and
    // sets the device preference to mobile. See the matching function guide at
    // https://developers.google.com/adwords/api/docs/guides/feed-matching-functions
    // for more details.
    String matchingFunctionString = String.format("AND( IN(FEED_ITEM_ID, {%s}), EQUALS(CONTEXT.DEVICE, 'Mobile') )", Joiner.on(',').join(siteLinksData.siteLinkFeedItemIds));
    CampaignFeed campaignFeed = new CampaignFeed();
    campaignFeed.setFeedId(siteLinksData.siteLinksFeedId);
    campaignFeed.setCampaignId(campaignId);
    Function matchingFunction = new Function();
    matchingFunction.setFunctionString(matchingFunctionString);
    campaignFeed.setMatchingFunction(matchingFunction);
    // Specifying placeholder types on the CampaignFeed allows the same feed
    // to be used for different placeholders in different Campaigns.
    campaignFeed.setPlaceholderTypes(new int[] { PLACEHOLDER_SITELINKS });
    CampaignFeedOperation operation = new CampaignFeedOperation();
    operation.setOperand(campaignFeed);
    operation.setOperator(Operator.ADD);
    CampaignFeedReturnValue result = campaignFeedService.mutate(new CampaignFeedOperation[] { operation });
    for (CampaignFeed savedCampaignFeed : result.getValue()) {
        System.out.printf("Campaign with ID %d was associated with feed with ID %d.%n", savedCampaignFeed.getCampaignId(), savedCampaignFeed.getFeedId());
    }
}
Also used : CampaignFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedServiceInterface) CampaignFeed(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed) Function(com.google.api.ads.adwords.axis.v201809.cm.Function) CampaignFeedReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedReturnValue) CampaignFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedOperation)

Example 4 with Function

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

the class AddCampaignTargetingCriteria method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignId the ID of the campaign where targeting criteria will be added.
 * @param locationFeedId optional ID of a location targeting feed.
 * @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, @Nullable Long locationFeedId) throws RemoteException {
    // Get the CampaignService.
    CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(session, CampaignCriterionServiceInterface.class);
    // Create locations. The IDs can be found in the documentation or
    // retrieved with the LocationCriterionService.
    Location california = new Location();
    california.setId(21137L);
    Location mexico = new Location();
    mexico.setId(2484L);
    // Create languages. The IDs can be found in the documentation or
    // retrieved with the ConstantDataService.
    Language english = new Language();
    english.setId(1000L);
    Language spanish = new Language();
    spanish.setId(1003L);
    List<Criterion> criteria = new ArrayList<>(Arrays.asList(california, mexico, english, spanish));
    // Distance targeting. Area of 10 miles around the locations in the location feed.
    if (locationFeedId != null) {
        LocationGroups radiusLocationGroup = new LocationGroups();
        radiusLocationGroup.setFeedId(locationFeedId);
        ConstantOperand radius = new ConstantOperand();
        radius.setType(ConstantOperandConstantType.DOUBLE);
        radius.setUnit(ConstantOperandUnit.MILES);
        radius.setDoubleValue(10d);
        LocationExtensionOperand distance = new LocationExtensionOperand();
        distance.setRadius(radius);
        Function radiusMatchingFunction = new Function();
        radiusMatchingFunction.setOperator(FunctionOperator.IDENTITY);
        radiusMatchingFunction.setLhsOperand(new FunctionArgumentOperand[] { distance });
        radiusLocationGroup.setMatchingFunction(radiusMatchingFunction);
        criteria.add(radiusLocationGroup);
    }
    // Create operations to add each of the criteria above.
    List<CampaignCriterionOperation> operations = new ArrayList<>();
    for (Criterion criterion : criteria) {
        CampaignCriterionOperation operation = new CampaignCriterionOperation();
        CampaignCriterion campaignCriterion = new CampaignCriterion();
        campaignCriterion.setCampaignId(campaignId);
        campaignCriterion.setCriterion(criterion);
        operation.setOperand(campaignCriterion);
        operation.setOperator(Operator.ADD);
        operations.add(operation);
    }
    // Add a negative campaign criterion.
    Keyword negativeKeyword = new Keyword();
    negativeKeyword.setText("jupiter cruise");
    negativeKeyword.setMatchType(KeywordMatchType.BROAD);
    CampaignCriterion negativeCriterion = new NegativeCampaignCriterion();
    negativeCriterion.setCampaignId(campaignId);
    negativeCriterion.setCriterion(negativeKeyword);
    CampaignCriterionOperation operation = new CampaignCriterionOperation();
    operation.setOperand(negativeCriterion);
    operation.setOperator(Operator.ADD);
    operations.add(operation);
    CampaignCriterionReturnValue result = campaignCriterionService.mutate(operations.toArray(new CampaignCriterionOperation[operations.size()]));
    // Display campaigns.
    for (CampaignCriterion campaignCriterion : result.getValue()) {
        System.out.printf("Campaign criterion with campaign ID %d, criterion ID %d, " + "and type '%s' was added.%n", campaignCriterion.getCampaignId(), campaignCriterion.getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
    }
}
Also used : ConstantOperand(com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand) CampaignCriterionOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation) LocationGroups(com.google.api.ads.adwords.axis.v201809.cm.LocationGroups) Keyword(com.google.api.ads.adwords.axis.v201809.cm.Keyword) NegativeCampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeCampaignCriterion) CampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion) ArrayList(java.util.ArrayList) NegativeCampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeCampaignCriterion) CampaignCriterionReturnValue(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionReturnValue) Function(com.google.api.ads.adwords.axis.v201809.cm.Function) Language(com.google.api.ads.adwords.axis.v201809.cm.Language) NegativeCampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.NegativeCampaignCriterion) CampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion) Criterion(com.google.api.ads.adwords.axis.v201809.cm.Criterion) CampaignCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface) LocationExtensionOperand(com.google.api.ads.adwords.axis.v201809.cm.LocationExtensionOperand) Location(com.google.api.ads.adwords.axis.v201809.cm.Location)

Aggregations

Function (com.google.api.ads.adwords.axis.v201809.cm.Function)3 CampaignFeed (com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed)2 ConstantOperand (com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand)2 Feed (com.google.api.ads.adwords.axis.v201809.cm.Feed)2 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 CampaignCriterion (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion)1 CampaignCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation)1 CampaignCriterionReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionReturnValue)1 CampaignCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface)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 Criterion (com.google.api.ads.adwords.axis.v201809.cm.Criterion)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 ExtensionSettingPlatform (com.google.api.ads.adwords.axis.v201809.cm.ExtensionSettingPlatform)1 FeedOperation (com.google.api.ads.adwords.axis.v201809.cm.FeedOperation)1 FeedReturnValue (com.google.api.ads.adwords.axis.v201809.cm.FeedReturnValue)1