Search in sources :

Example 1 with ReportDownloadResponse

use of com.google.api.ads.adwords.lib.utils.ReportDownloadResponse in project googleads-java-lib by googleads.

the class ReportDownloaderTest method downloadReport.

/**
 * Invokes {@link ReportDownloader#downloadReport(ReportDefinition)} or
 * {@link ReportDownloader#downloadReport(String, DownloadFormat)}, depending on whether this
 * instance is configured to use AWQL.
 *
 * @param downloadFormat the DownloadFormat for the request
 * @param rawResponse the response to return from the mocked ad hoc helper
 */
private ReportDownloadResponse downloadReport(DownloadFormat downloadFormat, RawReportDownloadResponse rawResponse, String expectedErrorText) throws ReportException, ReportDownloadResponseException {
    if (rawResponse.getHttpStatus() == 200) {
        // Response indicates success, so return a ReportDownloadResponse.
        when(adHocDownloadHelper.downloadReport(ArgumentMatchers.any(ReportRequest.class), ArgumentMatchers.any(Builder.class))).thenReturn(new ReportDownloadResponse(rawResponse));
    } else {
        // Response indicates failure, so throw an exception.
        when(adHocDownloadHelper.downloadReport(ArgumentMatchers.any(ReportRequest.class), ArgumentMatchers.any(Builder.class))).thenThrow(new Builder().build(rawResponse.getHttpStatus(), expectedErrorText));
    }
    if (isUseAwql) {
        return reportDownloader.downloadReport(AWQL_REQUEST, downloadFormat);
    } else {
        ReportDefinition reportDefinition = new ReportDefinition();
        reportDefinition.setSelector(new Selector());
        reportDefinition.getSelector().getFields().addAll(Arrays.asList("CampaignId", "CampaignName", "Impressions"));
        reportDefinition.setDateRangeType(ReportDefinitionDateRangeType.LAST_7_DAYS);
        reportDefinition.setReportName("Custom report");
        reportDefinition.setReportType(ReportDefinitionReportType.CAMPAIGN_PERFORMANCE_REPORT);
        return reportDownloader.downloadReport(reportDefinition);
    }
}
Also used : RawReportDownloadResponse(com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse) ReportDownloadResponse(com.google.api.ads.adwords.lib.utils.ReportDownloadResponse) ReportRequest(com.google.api.ads.adwords.lib.utils.ReportRequest) Builder(com.google.api.ads.adwords.lib.utils.v201809.DetailedReportDownloadResponseException.Builder) ReportDefinition(com.google.api.ads.adwords.lib.jaxb.v201809.ReportDefinition) Selector(com.google.api.ads.adwords.lib.jaxb.v201809.Selector)

Example 2 with ReportDownloadResponse

use of com.google.api.ads.adwords.lib.utils.ReportDownloadResponse in project googleads-java-lib by googleads.

the class DownloadCriteriaReportWithAwql 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 {
    // Create query.
    ReportQuery query = new ReportQuery.Builder().fields("CampaignId", "AdGroupId", "Id", "Criteria", "CriteriaType", "Impressions", "Clicks", "Cost").from(ReportDefinitionReportType.CRITERIA_PERFORMANCE_REPORT).where("Status").in("ENABLED", "PAUSED").during(ReportDefinitionDateRangeType.LAST_7_DAYS).build();
    // Optional: Set the reporting configuration of the session to suppress header, column name, or
    // summary rows in the report output. You can also configure this via your ads.properties
    // configuration file. See AdWordsSession.Builder.from(Configuration) for details.
    // In addition, you can set whether you want to explicitly include or exclude zero impression
    // rows.
    ReportingConfiguration reportingConfiguration = new ReportingConfiguration.Builder().skipReportHeader(false).skipColumnHeader(false).skipReportSummary(false).includeZeroImpressions(true).build();
    session.setReportingConfiguration(reportingConfiguration);
    ReportDownloaderInterface reportDownloader = adWordsServices.getUtility(session, ReportDownloaderInterface.class);
    // Set the property api.adwords.reportDownloadTimeout or call
    // ReportDownloader.setReportDownloadTimeout to set a timeout (in milliseconds)
    // for CONNECT and READ in report downloads.
    ReportDownloadResponse response = reportDownloader.downloadReport(query.toString(), DownloadFormat.CSV);
    response.saveToFile(reportFile);
    System.out.printf("Report successfully downloaded to: %s%n", reportFile);
}
Also used : ReportDownloadResponse(com.google.api.ads.adwords.lib.utils.ReportDownloadResponse) ReportQuery(com.google.api.ads.adwords.lib.utils.v201809.ReportQuery) ReportDownloaderInterface(com.google.api.ads.adwords.lib.utils.v201809.ReportDownloaderInterface) ReportingConfiguration(com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration)

Example 3 with ReportDownloadResponse

use of com.google.api.ads.adwords.lib.utils.ReportDownloadResponse in project googleads-java-lib by googleads.

the class StreamCriteriaReportResults method runExample.

/**
 * Runs the example.
 *
 * @param adWordsServices the services factory.
 * @param session the session.
 * @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 read from the response.
 */
public static void runExample(AdWordsServicesInterface adWordsServices, AdWordsSession session) throws ReportDownloadResponseException, ReportException, IOException {
    // Create the query.
    ReportQuery query = new ReportQuery.Builder().fields("Id", "AdNetworkType1", "Impressions").from(ReportDefinitionReportType.CRITERIA_PERFORMANCE_REPORT).where("Status").in("ENABLED", "PAUSED").during(ReportDefinitionDateRangeType.LAST_7_DAYS).build();
    // Optional: Set the reporting configuration of the session to suppress header, column name, or
    // summary rows in the report output. You can also configure this via your ads.properties
    // configuration file. See AdWordsSession.Builder.from(Configuration) for details.
    // In addition, you can set whether you want to explicitly include or exclude zero impression
    // rows.
    ReportingConfiguration reportingConfiguration = new ReportingConfiguration.Builder().skipReportHeader(true).skipColumnHeader(true).skipReportSummary(true).includeZeroImpressions(false).build();
    session.setReportingConfiguration(reportingConfiguration);
    ReportDownloaderInterface reportDownloader = adWordsServices.getUtility(session, ReportDownloaderInterface.class);
    BufferedReader reader = null;
    try {
        // Set the property api.adwords.reportDownloadTimeout or call
        // ReportDownloader.setReportDownloadTimeout to set a timeout (in milliseconds)
        // for CONNECT and READ in report downloads.
        final ReportDownloadResponse response = reportDownloader.downloadReport(query.toString(), DownloadFormat.CSV);
        // Read the response as a BufferedReader.
        reader = new BufferedReader(new InputStreamReader(response.getInputStream(), UTF_8));
        // Map to store total impressions by ad network type 1.
        Map<String, Long> impressionsByAdNetworkType1 = Maps.newTreeMap();
        // Stream the results one line at a time and perform any line-specific processing.
        String line;
        Splitter splitter = Splitter.on(',');
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
            // Split the line into a list of field values.
            List<String> values = splitter.splitToList(line);
            // Update the total impressions for the ad network type 1 value.
            String adNetworkType1 = values.get(1);
            Long impressions = Longs.tryParse(values.get(2));
            if (impressions != null) {
                Long impressionsTotal = impressionsByAdNetworkType1.get(adNetworkType1);
                impressionsTotal = impressionsTotal == null ? 0L : impressionsTotal;
                impressionsByAdNetworkType1.put(adNetworkType1, impressionsTotal + impressions);
            }
        }
        // Print the impressions totals by ad network type 1.
        System.out.println();
        System.out.printf("Total impressions by ad network type 1:%n%s%n", Joiner.on(SystemUtils.LINE_SEPARATOR).join(impressionsByAdNetworkType1.entrySet()));
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}
Also used : Splitter(com.google.common.base.Splitter) InputStreamReader(java.io.InputStreamReader) ReportQuery(com.google.api.ads.adwords.lib.utils.v201809.ReportQuery) ReportDownloaderInterface(com.google.api.ads.adwords.lib.utils.v201809.ReportDownloaderInterface) ReportDownloadResponse(com.google.api.ads.adwords.lib.utils.ReportDownloadResponse) BufferedReader(java.io.BufferedReader) ReportingConfiguration(com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration)

Example 4 with ReportDownloadResponse

use of com.google.api.ads.adwords.lib.utils.ReportDownloadResponse in project googleads-java-lib by googleads.

the class DownloadCriteriaReportWithSelector 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 {
    // Create selector.
    Selector selector = new Selector();
    selector.getFields().addAll(Arrays.asList("CampaignId", "AdGroupId", "Id", "CriteriaType", "Criteria", "FinalUrls", "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);
    // Optional: Set the reporting configuration of the session to suppress header, column name, or
    // summary rows in the report output. You can also configure this via your ads.properties
    // configuration file. See AdWordsSession.Builder.from(Configuration) for details.
    // In addition, you can set whether you want to explicitly include or exclude zero impression
    // rows.
    ReportingConfiguration reportingConfiguration = new ReportingConfiguration.Builder().skipReportHeader(false).skipColumnHeader(false).skipReportSummary(false).includeZeroImpressions(false).build();
    session.setReportingConfiguration(reportingConfiguration);
    reportDefinition.setSelector(selector);
    ReportDownloaderInterface reportDownloader = adWordsServices.getUtility(session, ReportDownloaderInterface.class);
    // Set the property api.adwords.reportDownloadTimeout or call
    // ReportDownloader.setReportDownloadTimeout to set a timeout (in milliseconds)
    // for CONNECT and READ in report downloads.
    ReportDownloadResponse response = reportDownloader.downloadReport(reportDefinition);
    response.saveToFile(reportFile);
    System.out.printf("Report successfully downloaded to: %s%n", reportFile);
}
Also used : ReportDownloadResponse(com.google.api.ads.adwords.lib.utils.ReportDownloadResponse) ReportDownloaderInterface(com.google.api.ads.adwords.lib.utils.v201809.ReportDownloaderInterface) 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.lib.jaxb.v201809.Selector)

Example 5 with ReportDownloadResponse

use of com.google.api.ads.adwords.lib.utils.ReportDownloadResponse 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)

Aggregations

ReportDownloadResponse (com.google.api.ads.adwords.lib.utils.ReportDownloadResponse)6 ReportingConfiguration (com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration)4 ReportDefinition (com.google.api.ads.adwords.lib.jaxb.v201809.ReportDefinition)3 ReportDownloaderInterface (com.google.api.ads.adwords.lib.utils.v201809.ReportDownloaderInterface)3 Selector (com.google.api.ads.adwords.lib.jaxb.v201809.Selector)2 RawReportDownloadResponse (com.google.api.ads.adwords.lib.utils.RawReportDownloadResponse)2 ReportQuery (com.google.api.ads.adwords.lib.utils.v201809.ReportQuery)2 Campaign (com.google.api.ads.adwords.axis.v201809.cm.Campaign)1 CampaignPage (com.google.api.ads.adwords.axis.v201809.cm.CampaignPage)1 CampaignServiceInterface (com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface)1 Paging (com.google.api.ads.adwords.axis.v201809.cm.Paging)1 Selector (com.google.api.ads.adwords.axis.v201809.cm.Selector)1 ReportRequest (com.google.api.ads.adwords.lib.utils.ReportRequest)1 Builder (com.google.api.ads.adwords.lib.utils.v201809.DetailedReportDownloadResponseException.Builder)1 ReportDownloader (com.google.api.ads.adwords.lib.utils.v201809.ReportDownloader)1 Splitter (com.google.common.base.Splitter)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1