Search in sources :

Example 1 with CampaignPage

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

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

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

the class AdvancedCreateCredentialFromScratch method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @param reportFile the output file for the report contents.
 * @throws DetailedReportDownloadResponseException if the report request failed with a detailed
 *     error from the reporting service.
 * @throws ReportDownloadResponseException if the report request failed with a general error from
 *     the reporting service.
 * @throws ReportException if the report request failed due to a transport layer error.
 * @throws IOException if the report's contents could not be written to {@code reportFile}.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session, String reportFile) throws ReportDownloadResponseException, ReportException, IOException {
    // Get the CampaignService.
    CampaignServiceInterface campaignService = adWordsServices.get(session, CampaignServiceInterface.class);
    // Create selector to retrieve the first 100 campaigns.
    Selector selector = new Selector();
    selector.setFields(new String[] { "Id", "Name" });
    Paging paging = new Paging();
    paging.setStartIndex(0);
    paging.setNumberResults(100);
    // Get the first page of campaigns.
    CampaignPage page = campaignService.get(selector);
    System.out.printf("Found %d total campaigns.%n", page.getTotalNumEntries());
    // Display campaigns.
    if (page.getEntries() != null) {
        for (Campaign campaign : page.getEntries()) {
            System.out.printf("Campaign with name '%s' and ID %d was found.%n", campaign.getName(), campaign.getId());
        }
    } else {
        System.out.println("No campaigns were found.");
    }
    // Create selector.
    com.google.api.ads.adwords.lib.jaxb.v201809.Selector reportSelector = new com.google.api.ads.adwords.lib.jaxb.v201809.Selector();
    reportSelector.getFields().addAll(Arrays.asList("CampaignId", "AdGroupId", "Id", "CriteriaType", "Criteria", "Impressions", "Clicks", "Cost"));
    // Create report definition.
    ReportDefinition reportDefinition = new ReportDefinition();
    reportDefinition.setReportName("Criteria performance report #" + System.currentTimeMillis());
    reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.YESTERDAY);
    reportDefinition.setReportType(ReportDefinitionReportType.CRITERIA_PERFORMANCE_REPORT);
    reportDefinition.setDownloadFormat(DownloadFormat.CSV);
    reportDefinition.setSelector(reportSelector);
    ReportingConfiguration reportingConfig = new ReportingConfiguration.Builder().includeZeroImpressions(false).build();
    session.setReportingConfiguration(reportingConfig);
    ReportDownloadResponse response = new ReportDownloader(session).downloadReport(reportDefinition);
    FileOutputStream fos = new FileOutputStream(new File(reportFile));
    Streams.copy(response.getInputStream(), fos);
    fos.close();
    System.out.printf("Report successfully downloaded: %s%n", reportFile);
}
Also used : Paging(com.google.api.ads.adwords.axis.v201809.cm.Paging) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) ReportDownloadResponse(com.google.api.ads.adwords.lib.utils.ReportDownloadResponse) ReportDownloader(com.google.api.ads.adwords.lib.utils.v201809.ReportDownloader) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ReportDefinition(com.google.api.ads.adwords.lib.jaxb.v201809.ReportDefinition) ReportingConfiguration(com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Example 4 with CampaignPage

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

the class GetCampaignsWithAwql 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);
    ServiceQuery serviceQuery = new ServiceQuery.Builder().fields(CampaignField.Id, CampaignField.Name, CampaignField.Status).orderBy(CampaignField.Name).limit(0, PAGE_SIZE).build();
    CampaignPage page = null;
    do {
        serviceQuery.nextPage(page);
        // Get all campaigns.
        page = campaignService.query(serviceQuery.toString());
        // Display campaigns.
        if (page.getEntries() != null) {
            for (Campaign campaign : page.getEntries()) {
                System.out.printf("Campaign with name '%s' and ID %d was found.%n", campaign.getName(), campaign.getId());
            }
        } else {
            System.out.println("No campaigns were found.");
        }
    } while (serviceQuery.hasNext(page));
}
Also used : CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) ServiceQuery(com.google.api.ads.adwords.axis.utils.v201809.ServiceQuery)

Example 5 with CampaignPage

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

the class GetCampaigns 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);
    int offset = 0;
    // Create selector.
    SelectorBuilder builder = new SelectorBuilder();
    Selector selector = builder.fields(CampaignField.Id, CampaignField.Name).orderAscBy(CampaignField.Name).offset(offset).limit(PAGE_SIZE).build();
    CampaignPage page;
    do {
        // Get all campaigns.
        page = campaignService.get(selector);
        // Display campaigns.
        if (page.getEntries() != null) {
            for (Campaign campaign : page.getEntries()) {
                System.out.printf("Campaign with name '%s' and ID %d was found.%n", campaign.getName(), campaign.getId());
            }
        } else {
            System.out.println("No campaigns were found.");
        }
        offset += PAGE_SIZE;
        selector = builder.increaseOffsetBy(PAGE_SIZE).build();
    } while (offset < page.getTotalNumEntries());
}
Also used : CampaignServiceInterface(com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface) Campaign(com.google.api.ads.adwords.axis.v201809.cm.Campaign) SelectorBuilder(com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder) CampaignPage(com.google.api.ads.adwords.axis.v201809.cm.CampaignPage) Selector(com.google.api.ads.adwords.axis.v201809.cm.Selector)

Aggregations

CampaignPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignPage)6 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)6 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)5 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)5 SelectorBuilder (com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder)4 Parameter (com.beust.jcommander.Parameter)1 AdWordsServices (com.google.api.ads.adwords.axis.factory.AdWordsServices)1 ServiceQuery (com.google.api.ads.adwords.axis.utils.v201809.ServiceQuery)1 AdGroupChangeData (com.google.api.ads.adwords.axis.v201809.ch.AdGroupChangeData)1 CampaignChangeData (com.google.api.ads.adwords.axis.v201809.ch.CampaignChangeData)1 CustomerChangeData (com.google.api.ads.adwords.axis.v201809.ch.CustomerChangeData)1 CustomerSyncSelector (com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncSelector)1 CustomerSyncServiceInterface (com.google.api.ads.adwords.axis.v201809.ch.CustomerSyncServiceInterface)1 ApiError (com.google.api.ads.adwords.axis.v201809.cm.ApiError)1 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 CampaignOperation (com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation)1 DateTimeRange (com.google.api.ads.adwords.axis.v201809.cm.DateTimeRange)1 DynamicSearchAdsSetting (com.google.api.ads.adwords.axis.v201809.cm.DynamicSearchAdsSetting)1 PageFeed (com.google.api.ads.adwords.axis.v201809.cm.PageFeed)1 Paging (com.google.api.ads.adwords.axis.v201809.cm.Paging)1