Search in sources :

Example 26 with Selector

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

the class SelectorBuilderTest method testSelectorBuilderImmutability.

/**
 * Tests the immutability of the selector.
 */
@Test
public void testSelectorBuilderImmutability() {
    DateFormat dateFormat = new SimpleDateFormat(SelectorBuilderImpl.DEFAULT_DATE_FORMAT);
    SelectorBuilder builder = new SelectorBuilder();
    DateTime start = new DateTime(2013, 3, 25, 0, 0, 0, 0);
    DateTime end = new DateTime(2013, 3, 26, 0, 0, 0, 0);
    String formatStart = dateFormat.format(start.toDate());
    String formatEnd = dateFormat.format(end.toDate());
    builder = builder.fields("Id", "Name", "Status").forDateRange(start, end);
    Selector selector = builder.build();
    assertEquals(formatStart, selector.getDateRange().getMin());
    assertEquals(formatEnd, selector.getDateRange().getMax());
    String formatEndPlusOne = dateFormat.format(end.plusDays(1).toDate());
    DateRange dateRange = new DateRange();
    dateRange.setMin(formatStart);
    dateRange.setMax(formatEndPlusOne);
    selector.setDateRange(dateRange);
    assertEquals(formatStart, selector.getDateRange().getMin());
    assertEquals(formatEndPlusOne, selector.getDateRange().getMax());
    Selector selectorRebuilt = builder.fields("Id", "Name", "Status").forDateRange(start, end).build();
    assertEquals(formatStart, selectorRebuilt.getDateRange().getMin());
    assertEquals(formatEnd, selectorRebuilt.getDateRange().getMax());
    checkUtilitiesState(false);
}
Also used : DateRange(com.google.api.ads.adwords.jaxws.v201809.cm.DateRange) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) DateTime(org.joda.time.DateTime) Selector(com.google.api.ads.adwords.jaxws.v201809.cm.Selector) Test(org.junit.Test)

Example 27 with Selector

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

the class AddDynamicPageFeed method updateCampaignDsaSetting.

/**
 * Updates the campaign DSA setting to add DSA pagefeeds.
 */
private static void updateCampaignDsaSetting(AdWordsServicesInterface adWordsServices, AdWordsSession session, Long campaignId, DSAFeedDetails feedDetails) throws ApiException, RemoteException {
    // Get the CampaignService.
    CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
    Selector selector = new SelectorBuilder().fields(CampaignField.Id, CampaignField.Settings).equalsId(campaignId).build();
    CampaignPage campaignPage = campaignService.get(selector);
    if (campaignPage.getEntries() == null || campaignPage.getTotalNumEntries() == 0) {
        throw new IllegalArgumentException("No campaign found with ID: " + campaignId);
    }
    Campaign campaign = campaignPage.getEntries(0);
    if (campaign.getSettings() == null) {
        throw new IllegalArgumentException("Campaign with ID " + campaignId + " is not a DSA campaign.");
    }
    DynamicSearchAdsSetting dsaSetting = (DynamicSearchAdsSetting) Arrays.stream(campaign.getSettings()).filter(DynamicSearchAdsSetting.class::isInstance).findFirst().orElse(null);
    if (dsaSetting == null) {
        throw new IllegalArgumentException("Campaign with ID " + campaignId + " is not a DSA campaign.");
    }
    // Use a page feed to specify precisely which URLs to use with your
    // Dynamic Search Ads.
    PageFeed pageFeed = new PageFeed();
    pageFeed.setFeedIds(new long[] { feedDetails.feedId });
    dsaSetting.setPageFeed(pageFeed);
    // Optional: Specify whether only the supplied URLs should be used with your
    // Dynamic Search Ads.
    dsaSetting.setUseSuppliedUrlsOnly(true);
    Campaign updatedCampaign = new Campaign();
    updatedCampaign.setId(campaignId);
    updatedCampaign.setSettings(campaign.getSettings());
    CampaignOperation operation = new CampaignOperation();
    operation.setOperand(updatedCampaign);
    operation.setOperator(Operator.SET);
    updatedCampaign = campaignService.mutate(new CampaignOperation[] { operation }).getValue(0);
    System.out.printf("DSA page feed for campaign ID %d was updated with feed ID %d.%n", updatedCampaign.getId(), feedDetails.feedId);
}
Also used : CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) PageFeed(com.google.api.ads.adwords.axis.v201809.cm.PageFeed) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) CampaignOperation(com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) DynamicSearchAdsSetting(com.google.api.ads.adwords.axis.v201809.cm.DynamicSearchAdsSetting) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 28 with Selector

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

the class GetAccountChanges 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 the CampaignService.
    CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
    // Get the CustomerSyncService.
    CustomerSyncServiceInterface customerSyncService = adWordsServices.get(session, CustomerSyncServiceInterface.class);
    // Get a list of all campaign IDs.
    List<Long> campaignIds = new ArrayList<>();
    Selector selector = new SelectorBuilder().fields(CampaignField.Id).build();
    CampaignPage campaigns = campaignService.get(selector);
    if (campaigns.getEntries() != null) {
        Arrays.stream(campaigns.getEntries()).forEach(campaign -> campaignIds.add(campaign.getId()));
    }
    // Create date time range for the past 24 hours.
    DateTimeRange dateTimeRange = new DateTimeRange();
    dateTimeRange.setMin(new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24)));
    dateTimeRange.setMax(new SimpleDateFormat("yyyyMMdd HHmmss").format(new Date()));
    // Create selector.
    CustomerSyncSelector customerSyncSelector = new CustomerSyncSelector();
    customerSyncSelector.setDateTimeRange(dateTimeRange);
    customerSyncSelector.setCampaignIds(ArrayUtils.toPrimitive(campaignIds.toArray(new Long[] {})));
    // Get all account changes for campaign.
    CustomerChangeData accountChanges = customerSyncService.get(customerSyncSelector);
    // Display changes.
    if (accountChanges != null && accountChanges.getChangedCampaigns() != null) {
        System.out.printf("Most recent change: %s%n", accountChanges.getLastChangeTimestamp());
        for (CampaignChangeData campaignChanges : accountChanges.getChangedCampaigns()) {
            System.out.printf("Campaign with ID %d was changed:%n", campaignChanges.getCampaignId());
            System.out.printf("\tCampaign changed status: '%s'%n", campaignChanges.getCampaignChangeStatus());
            if (!ChangeStatus.NEW.equals(campaignChanges.getCampaignChangeStatus())) {
                System.out.printf("\tAdded campaign criteria: %s%n", getFormattedList(campaignChanges.getAddedCampaignCriteria()));
                System.out.printf("\tRemoved campaign criteria: %s%n", getFormattedList(campaignChanges.getRemovedCampaignCriteria()));
                if (campaignChanges.getChangedAdGroups() != null) {
                    for (AdGroupChangeData adGroupChanges : campaignChanges.getChangedAdGroups()) {
                        System.out.printf("\tAd group with ID %d was changed:%n", adGroupChanges.getAdGroupId());
                        System.out.printf("\t\tAd group changed status: %s%n", adGroupChanges.getAdGroupChangeStatus());
                        if (!ChangeStatus.NEW.equals(adGroupChanges.getAdGroupChangeStatus())) {
                            System.out.printf("\t\tAds changed: %s%n", getFormattedList(adGroupChanges.getChangedAds()));
                            System.out.printf("\t\tCriteria changed: %s%n", getFormattedList(adGroupChanges.getChangedCriteria()));
                            System.out.printf("\t\tCriteria removed: %s%n", getFormattedList(adGroupChanges.getRemovedCriteria()));
                        }
                    }
                }
            }
            System.out.println("");
        }
    } else {
        System.out.println("No account changes were found.");
    }
}
Also used : CustomerChangeData(com.google.api.ads.adwords.axis.v201809.ch.CustomerChangeData) ArrayList(java.util.ArrayList) CampaignChangeData(com.google.api.ads.adwords.axis.v201809.ch.CampaignChangeData) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) Date(java.util.Date) CustomerSyncSelector(com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncSelector) CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) AdGroupChangeData(com.google.api.ads.adwords.axis.v201809.ch.AdGroupChangeData) CustomerSyncServiceInterface(com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncServiceInterface) DateTimeRange(com.google.api.ads.adwords.axis.v201809.cm.DateTimeRange) SimpleDateFormat(java.text.SimpleDateFormat) CustomerSyncSelector(com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncSelector) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 29 with Selector

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

the class GetAccountHierarchy 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 the ServicedAccountService.
    ManagedCustomerServiceInterface managedCustomerService = adWordsServices.get(session, ManagedCustomerServiceInterface.class);
    // Create selector builder.
    int offset = 0;
    SelectorBuilder selectorBuilder = new SelectorBuilder().fields(ManagedCustomerField.CustomerId, ManagedCustomerField.Name).offset(offset).limit(PAGE_SIZE);
    // Get results.
    ManagedCustomerPage page;
    // Map from customerId to customer node.
    Map<Long, ManagedCustomerTreeNode> customerIdToCustomerNode = Maps.newHashMap();
    // Map from each parent customer ID to its set of linked child customer IDs.
    SortedSetMultimap<Long, Long> parentIdToChildIds = TreeMultimap.create();
    do {
        page = managedCustomerService.get(selectorBuilder.build());
        if (page.getEntries() != null) {
            // Create account tree nodes for each customer.
            for (ManagedCustomer customer : page.getEntries()) {
                ManagedCustomerTreeNode node = new ManagedCustomerTreeNode();
                node.account = customer;
                customerIdToCustomerNode.put(customer.getCustomerId(), node);
            }
            // Update the map of parent customer ID to child customer IDs.
            if (page.getLinks() != null) {
                for (ManagedCustomerLink link : page.getLinks()) {
                    parentIdToChildIds.put(link.getManagerCustomerId(), link.getClientCustomerId());
                }
            }
        }
        offset += PAGE_SIZE;
        selectorBuilder.increaseOffsetBy(PAGE_SIZE);
    } while (offset < page.getTotalNumEntries());
    // of its parentNode.
    for (Entry<Long, Long> parentIdEntry : parentIdToChildIds.entries()) {
        ManagedCustomerTreeNode parentNode = customerIdToCustomerNode.get(parentIdEntry.getKey());
        ManagedCustomerTreeNode childNode = customerIdToCustomerNode.get(parentIdEntry.getValue());
        childNode.parentNode = parentNode;
        parentNode.childAccounts.add(childNode);
    }
    // Find the root account node in the tree.
    ManagedCustomerTreeNode rootNode = customerIdToCustomerNode.values().stream().filter(node -> node.parentNode == null).findFirst().orElse(null);
    // Display serviced account graph.
    if (rootNode != null) {
        // Display account tree.
        System.out.println("CustomerId, Name");
        System.out.println(rootNode.toTreeString(0, new StringBuffer()));
    } else {
        System.out.println("No serviced accounts were found.");
    }
}
Also used : SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ManagedCustomer(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomer) ManagedCustomerLink(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerLink) ManagedCustomerServiceInterface(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerServiceInterface) ManagedCustomerPage(com.google.api.ads.adwords.axis.v201809.mcm.ManagedCustomerPage)

Example 30 with Selector

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

the class GetKeywordIdeas method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param adGroupId the optional ID of the seed ad group.
 * @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, @Nullable Long adGroupId) throws RemoteException {
    // Get the TargetingIdeaService.
    TargetingIdeaServiceInterface targetingIdeaService = adWordsServices.get(session, TargetingIdeaServiceInterface.class);
    // Create selector.
    TargetingIdeaSelector selector = new TargetingIdeaSelector();
    selector.setRequestType(RequestType.IDEAS);
    selector.setIdeaType(IdeaType.KEYWORD);
    selector.setRequestedAttributeTypes(new AttributeType[] { AttributeType.KEYWORD_TEXT, AttributeType.SEARCH_VOLUME, AttributeType.AVERAGE_CPC, AttributeType.COMPETITION, AttributeType.CATEGORY_PRODUCTS_AND_SERVICES });
    // Set selector paging (required for targeting idea service).
    Paging paging = new Paging();
    paging.setStartIndex(0);
    paging.setNumberResults(10);
    selector.setPaging(paging);
    List<SearchParameter> searchParameters = new ArrayList<>();
    // Create related to query search parameter.
    RelatedToQuerySearchParameter relatedToQuerySearchParameter = new RelatedToQuerySearchParameter();
    relatedToQuerySearchParameter.setQueries(new String[] { "bakery", "pastries", "birthday cake" });
    searchParameters.add(relatedToQuerySearchParameter);
    // Language setting (optional).
    // The ID can be found in the documentation:
    // https://developers.google.com/adwords/api/docs/appendix/languagecodes
    // See the documentation for limits on the number of allowed language parameters:
    // https://developers.google.com/adwords/api/docs/reference/latest/TargetingIdeaService.LanguageSearchParameter
    LanguageSearchParameter languageParameter = new LanguageSearchParameter();
    Language english = new Language();
    english.setId(1000L);
    languageParameter.setLanguages(new Language[] { english });
    searchParameters.add(languageParameter);
    // Create network search parameter (optional).
    NetworkSetting networkSetting = new NetworkSetting();
    networkSetting.setTargetGoogleSearch(true);
    networkSetting.setTargetSearchNetwork(false);
    networkSetting.setTargetContentNetwork(false);
    networkSetting.setTargetPartnerSearchNetwork(false);
    NetworkSearchParameter networkSearchParameter = new NetworkSearchParameter();
    networkSearchParameter.setNetworkSetting(networkSetting);
    searchParameters.add(networkSearchParameter);
    // Optional: Use an existing ad group to generate ideas.
    if (adGroupId != null) {
        SeedAdGroupIdSearchParameter seedAdGroupIdSearchParameter = new SeedAdGroupIdSearchParameter();
        seedAdGroupIdSearchParameter.setAdGroupId(adGroupId);
        searchParameters.add(seedAdGroupIdSearchParameter);
    }
    selector.setSearchParameters(searchParameters.toArray(new SearchParameter[searchParameters.size()]));
    // Get keyword ideas.
    TargetingIdeaPage page = targetingIdeaService.get(selector);
    // Display keyword ideas.
    for (TargetingIdea targetingIdea : page.getEntries()) {
        Map<AttributeType, Attribute> data = Maps.toMap(targetingIdea.getData());
        StringAttribute keyword = (StringAttribute) data.get(AttributeType.KEYWORD_TEXT);
        IntegerSetAttribute categories = (IntegerSetAttribute) data.get(AttributeType.CATEGORY_PRODUCTS_AND_SERVICES);
        String categoriesString = "(none)";
        if (categories != null && categories.getValue() != null) {
            categoriesString = Joiner.on(", ").join(Ints.asList(categories.getValue()));
        }
        Long averageMonthlySearches = ((LongAttribute) data.get(AttributeType.SEARCH_VOLUME)).getValue();
        Money averageCpc = ((MoneyAttribute) data.get(AttributeType.AVERAGE_CPC)).getValue();
        Double competition = ((DoubleAttribute) data.get(AttributeType.COMPETITION)).getValue();
        System.out.printf("Keyword with text '%s', average monthly search volume %d, " + "average CPC %d, and competition %.2f " + "was found with categories: %s%n", keyword.getValue(), averageMonthlySearches, averageCpc.getMicroAmount(), competition, categoriesString);
    }
    if (page.getEntries() == null) {
        System.out.println("No related keywords were found.");
    }
}
Also used : SeedAdGroupIdSearchParameter(com.google.api.ads.adwords.axis.v201809.o.SeedAdGroupIdSearchParameter) TargetingIdea(com.google.api.ads.adwords.axis.v201809.o.TargetingIdea) DoubleAttribute(com.google.api.ads.adwords.axis.v201809.o.DoubleAttribute) Attribute(com.google.api.ads.adwords.axis.v201809.o.Attribute) LongAttribute(com.google.api.ads.adwords.axis.v201809.o.LongAttribute) MoneyAttribute(com.google.api.ads.adwords.axis.v201809.o.MoneyAttribute) StringAttribute(com.google.api.ads.adwords.axis.v201809.o.StringAttribute) IntegerSetAttribute(com.google.api.ads.adwords.axis.v201809.o.IntegerSetAttribute) ArrayList(java.util.ArrayList) StringAttribute(com.google.api.ads.adwords.axis.v201809.o.StringAttribute) Money(com.google.api.ads.adwords.axis.v201809.cm.Money) Language(com.google.api.ads.adwords.axis.v201809.cm.Language) TargetingIdeaPage(com.google.api.ads.adwords.axis.v201809.o.TargetingIdeaPage) AttributeType(com.google.api.ads.adwords.axis.v201809.o.AttributeType) LongAttribute(com.google.api.ads.adwords.axis.v201809.o.LongAttribute) MoneyAttribute(com.google.api.ads.adwords.axis.v201809.o.MoneyAttribute) LanguageSearchParameter(com.google.api.ads.adwords.axis.v201809.o.LanguageSearchParameter) NetworkSearchParameter(com.google.api.ads.adwords.axis.v201809.o.NetworkSearchParameter) SearchParameter(com.google.api.ads.adwords.axis.v201809.o.SearchParameter) RelatedToQuerySearchParameter(com.google.api.ads.adwords.axis.v201809.o.RelatedToQuerySearchParameter) SeedAdGroupIdSearchParameter(com.google.api.ads.adwords.axis.v201809.o.SeedAdGroupIdSearchParameter) TargetingIdeaSelector(com.google.api.ads.adwords.axis.v201809.o.TargetingIdeaSelector) Paging(com.google.api.ads.adwords.axis.v201809.cm.Paging) IntegerSetAttribute(com.google.api.ads.adwords.axis.v201809.o.IntegerSetAttribute) TargetingIdeaServiceInterface(com.google.api.ads.adwords.axis.v201809.o.TargetingIdeaServiceInterface) NetworkSearchParameter(com.google.api.ads.adwords.axis.v201809.o.NetworkSearchParameter) LanguageSearchParameter(com.google.api.ads.adwords.axis.v201809.o.LanguageSearchParameter) DoubleAttribute(com.google.api.ads.adwords.axis.v201809.o.DoubleAttribute) NetworkSetting(com.google.api.ads.adwords.axis.v201809.cm.NetworkSetting) RelatedToQuerySearchParameter(com.google.api.ads.adwords.axis.v201809.o.RelatedToQuerySearchParameter)

Aggregations

Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)36 Test (org.junit.Test)27 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)22 Selector (com.google.api.ads.adwords.jaxws.v201809.cm.Selector)14 ArrayList (java.util.ArrayList)9 SimpleDateFormat (java.text.SimpleDateFormat)7 DateFormat (java.text.DateFormat)6 DateTime (org.joda.time.DateTime)6 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)5 CampaignPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignPage)5 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)5 OrderBy (com.google.api.ads.adwords.jaxws.v201809.cm.OrderBy)4 Predicate (com.google.api.ads.adwords.jaxws.v201809.cm.Predicate)4 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)4 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)4 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)4 RemoteException (java.rmi.RemoteException)4 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)3 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)3 AdGroupAdPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage)3