Search in sources :

Example 11 with Campaign

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

the class GetAdGroups method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param campaignId the ID of the campaign to use to find ad groups.
 * @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 RemoteException {
    // Get the AdGroupService.
    AdGroupServiceInterface adGroupService = adWordsServices.get(session, AdGroupServiceInterface.class);
    int offset = 0;
    boolean morePages = true;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(AdGroupField.Id, AdGroupField.Name).orderAscBy(AdGroupField.Name).offset(offset).limit(PAGE_SIZE).equals(AdGroupField.CampaignId, campaignId.toString()).build();
    while (morePages) {
        // Get all ad groups.
        AdGroupPage page = adGroupService.get(selector);
        // Display ad groups.
        if (page.getEntries() != null) {
            for (AdGroup adGroup : page.getEntries()) {
                System.out.printf("Ad group with name '%s' and ID %d was found.%n", adGroup.getName(), adGroup.getId());
            }
        } else {
            System.out.println("No ad groups were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
        morePages = offset < page.getTotalNumEntries();
    }
}
Also used : AdGroupServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) AdGroupPage(com.google.api.ads.adwords.axis.v201809.cm.AdGroupPage) AdGroup(com.google.api.ads.adwords.axis.v201809.cm.AdGroup) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 12 with Campaign

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

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

the class MigrateToExtensionSettings method createExtensionSetting.

/**
 * Creates the extension setting for a list of feed items.
 *
 * @param adWordsServices the AdWordsServices
 * @param session the AdWordsSession
 * @param feedItems the list of all feed items
 * @param campaignFeed the original campaign feed
 * @param feedItemIds IDs of the feed items for which extension settings should be created
 * @param platformRestrictions the platform restrictions for the new campaign extension setting
 */
private static void createExtensionSetting(AdWordsServicesInterface adWordsServices, AdWordsSession session, Map<Long, SiteLinkFromFeed> feedItems, CampaignFeed campaignFeed, Set<Long> feedItemIds, ExtensionSettingPlatform platformRestrictions) throws RemoteException {
    // Get the CampaignExtensionSettingService.
    CampaignExtensionSettingServiceInterface campaignExtensionSettingService = adWordsServices.get(session, CampaignExtensionSettingServiceInterface.class);
    CampaignExtensionSetting campaignExtensionSetting = new CampaignExtensionSetting();
    campaignExtensionSetting.setCampaignId(campaignFeed.getCampaignId());
    campaignExtensionSetting.setExtensionType(FeedType.SITELINK);
    ExtensionSetting extensionSetting = new ExtensionSetting();
    List<ExtensionFeedItem> extensionFeedItems = new ArrayList<>();
    for (Long feedItemId : feedItemIds) {
        SiteLinkFromFeed siteLinkFromFeed = feedItems.get(feedItemId);
        SitelinkFeedItem siteLinkFeedItem = new SitelinkFeedItem();
        siteLinkFeedItem.setSitelinkText(siteLinkFromFeed.text);
        if (siteLinkFromFeed.finalUrls != null && siteLinkFromFeed.finalUrls.length > 0) {
            siteLinkFeedItem.setSitelinkFinalUrls(new UrlList(siteLinkFromFeed.finalUrls));
            if (siteLinkFromFeed.finalMobileUrls != null && siteLinkFromFeed.finalMobileUrls.length > 0) {
                siteLinkFeedItem.setSitelinkFinalMobileUrls(new UrlList(siteLinkFromFeed.finalMobileUrls));
            }
            siteLinkFeedItem.setSitelinkTrackingUrlTemplate(siteLinkFromFeed.trackingUrlTemplate);
        } else {
            siteLinkFeedItem.setSitelinkUrl(siteLinkFromFeed.url);
        }
        siteLinkFeedItem.setSitelinkLine2(siteLinkFromFeed.line2);
        siteLinkFeedItem.setSitelinkLine3(siteLinkFromFeed.line3);
        extensionFeedItems.add(siteLinkFeedItem);
    }
    extensionSetting.setExtensions(extensionFeedItems.toArray(new ExtensionFeedItem[extensionFeedItems.size()]));
    extensionSetting.setPlatformRestrictions(platformRestrictions);
    campaignExtensionSetting.setExtensionSetting(extensionSetting);
    CampaignExtensionSettingOperation operation = new CampaignExtensionSettingOperation();
    operation.setOperand(campaignExtensionSetting);
    operation.setOperator(Operator.ADD);
    campaignExtensionSettingService.mutate(new CampaignExtensionSettingOperation[] { operation });
}
Also used : ExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.ExtensionSetting) CampaignExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSetting) CampaignExtensionSettingOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingOperation) ExtensionFeedItem(com.google.api.ads.adwords.axis.v201809.cm.ExtensionFeedItem) CampaignExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSetting) SitelinkFeedItem(com.google.api.ads.adwords.axis.v201809.cm.SitelinkFeedItem) ArrayList(java.util.ArrayList) CampaignExtensionSettingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingServiceInterface) UrlList(com.google.api.ads.adwords.axis.v201809.cm.UrlList)

Example 14 with Campaign

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

the class MigrateToExtensionSettings method getFeedItemIdsForCampaign.

/**
 * Returns the list of feed item IDs that are used by a campaign through a given campaign feed.
 */
private static Set<Long> getFeedItemIdsForCampaign(CampaignFeed campaignFeed) throws RemoteException {
    Set<Long> feedItemIds = Sets.newHashSet();
    FunctionOperator functionOperator = campaignFeed.getMatchingFunction().getOperator();
    if (FunctionOperator.IN.equals(functionOperator)) {
        // Check if matchingFunction is of the form IN(FEED_ITEM_ID,{xxx,xxx}).
        // Extract feed items if applicable.
        feedItemIds.addAll(getFeedItemIdsFromArgument(campaignFeed.getMatchingFunction()));
    } else if (FunctionOperator.AND.equals(functionOperator)) {
        // Check if matchingFunction is of the form IN(FEED_ITEM_ID,{xxx,xxx}).
        // Extract feed items if applicable.
        Arrays.stream(campaignFeed.getMatchingFunction().getLhsOperand()).filter(FunctionOperand.class::isInstance).map(argument -> (FunctionOperand) argument).filter(operand -> FunctionOperator.IN.equals(operand.getValue().getOperator())).forEach(operand -> feedItemIds.addAll(getFeedItemIdsFromArgument(operand.getValue())));
    } else {
    // There are no other matching functions involving feed item IDs.
    }
    return feedItemIds;
}
Also used : Arrays(java.util.Arrays) UrlList(com.google.api.ads.adwords.axis.v201809.cm.UrlList) CampaignFeedOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedOperation) CampaignExtensionSettingOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingOperation) CampaignFeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedServiceInterface) HashMultimap(com.google.common.collect.HashMultimap) OfflineCredentials(com.google.api.ads.common.lib.auth.OfflineCredentials) Map(java.util.Map) ApiException(com.google.api.ads.adwords.axis.v201809.cm.ApiException) FeedItem(com.google.api.ads.adwords.axis.v201809.cm.FeedItem) FeedPage(com.google.api.ads.adwords.axis.v201809.cm.FeedPage) OAuthException(com.google.api.ads.common.lib.exception.OAuthException) AdWordsServices(com.google.api.ads.adwords.axis.factory.AdWordsServices) Set(java.util.Set) Operator(com.google.api.ads.adwords.axis.v201809.cm.Operator) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RemoteException(java.rmi.RemoteException) CampaignExtensionSettingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSettingServiceInterface) List(java.util.List) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) AdWordsServicesInterface(com.google.api.ads.adwords.lib.factory.AdWordsServicesInterface) ApiError(com.google.api.ads.adwords.axis.v201809.cm.ApiError) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) FunctionOperand(com.google.api.ads.adwords.axis.v201809.cm.FunctionOperand) FunctionArgumentOperand(com.google.api.ads.adwords.axis.v201809.cm.FunctionArgumentOperand) ExtensionFeedItem(com.google.api.ads.adwords.axis.v201809.cm.ExtensionFeedItem) FunctionOperator(com.google.api.ads.adwords.axis.v201809.cm.FunctionOperator) FeedMapping(com.google.api.ads.adwords.axis.v201809.cm.FeedMapping) FeedItemAttributeValue(com.google.api.ads.adwords.axis.v201809.cm.FeedItemAttributeValue) AttributeFieldMapping(com.google.api.ads.adwords.axis.v201809.cm.AttributeFieldMapping) Multimap(com.google.common.collect.Multimap) FeedServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedServiceInterface) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) FeedItemOperation(com.google.api.ads.adwords.axis.v201809.cm.FeedItemOperation) ExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.ExtensionSetting) CampaignExtensionSetting(com.google.api.ads.adwords.axis.v201809.cm.CampaignExtensionSetting) Function(com.google.api.ads.adwords.axis.v201809.cm.Function) Credential(com.google.api.client.auth.oauth2.Credential) CampaignFeed(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeed) RequestContextOperandContextType(com.google.api.ads.adwords.axis.v201809.cm.RequestContextOperandContextType) FeedMappingServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingServiceInterface) CampaignFeedPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignFeedPage) Feed(com.google.api.ads.adwords.axis.v201809.cm.Feed) ConfigurationLoadException(com.google.api.ads.common.lib.conf.ConfigurationLoadException) ExtensionSettingPlatform(com.google.api.ads.adwords.axis.v201809.cm.ExtensionSettingPlatform) FeedMappingPage(com.google.api.ads.adwords.axis.v201809.cm.FeedMappingPage) SitelinkFeedItem(com.google.api.ads.adwords.axis.v201809.cm.SitelinkFeedItem) Maps(com.google.common.collect.Maps) DEFAULT_CONFIGURATION_FILENAME(com.google.api.ads.common.lib.utils.Builder.DEFAULT_CONFIGURATION_FILENAME) RequestContextOperand(com.google.api.ads.adwords.axis.v201809.cm.RequestContextOperand) FeedItemPage(com.google.api.ads.adwords.axis.v201809.cm.FeedItemPage) ConstantOperand(com.google.api.ads.adwords.axis.v201809.cm.ConstantOperand) FeedItemServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.FeedItemServiceInterface) FeedType(com.google.api.ads.adwords.axis.v201809.cm.FeedType) Api(com.google.api.ads.common.lib.auth.OfflineCredentials.Api) FunctionOperand(com.google.api.ads.adwords.axis.v201809.cm.FunctionOperand) FunctionOperator(com.google.api.ads.adwords.axis.v201809.cm.FunctionOperator)

Example 15 with Campaign

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

Aggregations

Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)23 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)17 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)16 Budget (com.google.api.ads.adwords.axis.v201809.cm.Budget)15 ArrayList (java.util.ArrayList)14 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)11 BiddingStrategyConfiguration (com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration)11 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)11 Money (com.google.api.ads.adwords.axis.v201809.cm.Money)10 AdGroup (com.google.api.ads.adwords.axis.v201809.cm.AdGroup)9 CampaignReturnValue (com.google.api.ads.adwords.axis.v201809.cm.CampaignReturnValue)9 AdGroupOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupOperation)6 CampaignCriterionOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionOperation)6 CampaignPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignPage)6 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)5 AdGroupServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupServiceInterface)5 BudgetOperation (com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation)5 CampaignCriterion (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion)5 CampaignCriterionServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface)5 AdGroupAdOperation (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdOperation)4