Search in sources :

Example 26 with Selector

use of com.google.api.ads.adwords.jaxws.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 27 with Selector

use of com.google.api.ads.adwords.jaxws.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 28 with Selector

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

the class AddAudience 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 UserListService.
    AdwordsUserListServiceInterface userListService = adWordsServices.get(session, AdwordsUserListServiceInterface.class);
    // Get the ConversionTrackerService.
    ConversionTrackerServiceInterface conversionTrackerService = adWordsServices.get(session, ConversionTrackerServiceInterface.class);
    // Create conversion type (tag).
    UserListConversionType conversionType = new UserListConversionType();
    conversionType.setName("Mars cruise customers #" + System.currentTimeMillis());
    // Create remarketing user list.
    BasicUserList userList = new BasicUserList();
    userList.setName("Mars cruise customers #" + System.currentTimeMillis());
    userList.setDescription("A list of mars cruise customers in the last year");
    userList.setMembershipLifeSpan(365L);
    userList.setConversionTypes(new UserListConversionType[] { conversionType });
    // You can optionally provide these field(s).
    userList.setStatus(UserListMembershipStatus.OPEN);
    // Create operations.
    UserListOperation operation = new UserListOperation();
    operation.setOperand(userList);
    operation.setOperator(Operator.ADD);
    UserListOperation[] operations = new UserListOperation[] { operation };
    // Add user list.
    UserListReturnValue result = userListService.mutate(operations);
    // Display results.
    // Capture the ID(s) of the conversion.
    List<String> conversionIds = new ArrayList<>();
    for (UserList userListResult : result.getValue()) {
        if (userListResult instanceof BasicUserList) {
            BasicUserList remarketingUserList = (BasicUserList) userListResult;
            for (UserListConversionType userListConversionType : remarketingUserList.getConversionTypes()) {
                conversionIds.add(userListConversionType.getId().toString());
            }
        }
    }
    // Create predicate and selector.
    Selector selector = new SelectorBuilder().fields("Id", "GoogleGlobalSiteTag", "GoogleEventSnippet").in(AdwordsUserListField.Id, conversionIds.toArray(new String[0])).build();
    // Get all conversion trackers.
    Map<Long, AdWordsConversionTracker> conversionTrackers = new HashMap<Long, AdWordsConversionTracker>();
    ConversionTrackerPage page = conversionTrackerService.get(selector);
    if (page != null && page.getEntries() != null) {
        conversionTrackers = Arrays.stream(page.getEntries()).collect(Collectors.toMap(conversionTracker -> conversionTracker.getId(), conversionTracker -> (AdWordsConversionTracker) conversionTracker));
    }
    // Display user lists.
    for (UserList userListResult : result.getValue()) {
        System.out.printf("User list with name '%s' and ID %d was added.%n", userListResult.getName(), userListResult.getId());
        // Display user list associated conversion code snippets.
        if (userListResult instanceof BasicUserList) {
            BasicUserList remarketingUserList = (BasicUserList) userListResult;
            for (UserListConversionType userListConversionType : remarketingUserList.getConversionTypes()) {
                ConversionTracker conversionTracker = conversionTrackers.get(userListConversionType.getId());
                System.out.printf("Google global site tag:%n%s%n%n", conversionTracker.getGoogleGlobalSiteTag());
                System.out.printf("Google event snippet:%n%s%n%n", conversionTracker.getGoogleEventSnippet());
            }
        }
    }
}
Also used : ConversionTrackerPage(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerPage) AdwordsUserListServiceInterface(com.google.api.ads.adwords.axis.v201809.rm.AdwordsUserListServiceInterface) AdWordsConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker) ConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.ConversionTracker) AdWordsConversionTracker(com.google.api.ads.adwords.axis.v201809.cm.AdWordsConversionTracker) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BasicUserList(com.google.api.ads.adwords.axis.v201809.rm.BasicUserList) UserListOperation(com.google.api.ads.adwords.axis.v201809.rm.UserListOperation) UserListReturnValue(com.google.api.ads.adwords.axis.v201809.rm.UserListReturnValue) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) ConversionTrackerServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.ConversionTrackerServiceInterface) UserList(com.google.api.ads.adwords.axis.v201809.rm.UserList) BasicUserList(com.google.api.ads.adwords.axis.v201809.rm.BasicUserList) UserListConversionType(com.google.api.ads.adwords.axis.v201809.rm.UserListConversionType) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 29 with Selector

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

the class GetCampaignTargetingCriteria 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.
    CampaignCriterionServiceInterface campaignCriterionService = adWordsServices.get(session, CampaignCriterionServiceInterface.class);
    int offset = 0;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(CampaignCriterionField.CampaignId, CampaignCriterionField.Id, CampaignCriterionField.CriteriaType, CampaignCriterionField.PlatformName, CampaignCriterionField.LanguageName, CampaignCriterionField.LocationName, CampaignCriterionField.KeywordText).in(CampaignCriterionField.CriteriaType, "KEYWORD", "LANGUAGE", "LOCATION", "PLATFORM").offset(0).limit(PAGE_SIZE).build();
    CampaignCriterionPage page = null;
    do {
        page = campaignCriterionService.get(selector);
        if (page.getEntries() != null) {
            // Display campaigns.
            for (CampaignCriterion campaignCriterion : page.getEntries()) {
                System.out.printf("Campaign criterion with campaign ID %d, criterion ID %d, " + "and type '%s' was found.%n", campaignCriterion.getCampaignId(), campaignCriterion.getCriterion().getId(), campaignCriterion.getCriterion().getCriterionType());
            }
        } else {
            System.out.println("No campaign criteria were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
    } while (offset < page.getTotalNumEntries());
}
Also used : SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) CampaignCriterion(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterion) CampaignCriterionServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionServiceInterface) CampaignCriterionPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignCriterionPage) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 30 with Selector

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

the class SelectorBuilderTest method testPredicatesBuild.

/**
 * Tests the predicate build for the ID field.
 */
@Test
public void testPredicatesBuild() {
    SelectorBuilder builder = new SelectorBuilder();
    builder = builder.equalsId(10L);
    Selector selector = builder.build();
    assertNotNull(selector.getPredicates());
    assertEquals(1, selector.getPredicates().length);
    Predicate predicate = selector.getPredicates()[0];
    assertEquals("Id", predicate.getField());
    assertEquals(PredicateOperator.EQUALS, predicate.getOperator());
    assertNotNull(predicate.getValues());
    assertEquals(1, predicate.getValues().length);
    assertEquals("10", predicate.getValues()[0]);
    checkUtilitiesState(false);
}
Also used : Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector) Predicate(com.google.api.ads.adwords.axis.v201809.cm.Predicate) Test(org.junit.Test)

Aggregations

Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)36 Test (org.junit.Test)26 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)21 Selector (com.google.api.ads.adwords.jaxws.v201809.cm.Selector)14 SimpleDateFormat (java.text.SimpleDateFormat)7 DateFormat (java.text.DateFormat)6 DateTime (org.joda.time.DateTime)6 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.axis.v201809.cm.OrderBy)4 Predicate (com.google.api.ads.adwords.axis.v201809.cm.Predicate)4 OrderBy (com.google.api.ads.adwords.jaxws.v201809.cm.OrderBy)4 Predicate (com.google.api.ads.adwords.jaxws.v201809.cm.Predicate)4 ArrayList (java.util.ArrayList)4 AdGroupAd (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAd)3 AdGroupAdPage (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdPage)3 AdGroupAdServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.AdGroupAdServiceInterface)3 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)3 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)3 Parameter (com.beust.jcommander.Parameter)2