Search in sources :

Example 16 with ReportQuery

use of com.google.api.ads.admanager.axis.v202202.ReportQuery 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 17 with ReportQuery

use of com.google.api.ads.admanager.axis.v202202.ReportQuery 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 18 with ReportQuery

use of com.google.api.ads.admanager.axis.v202202.ReportQuery in project googleads-java-lib by googleads.

the class RunReportWithCustomFields method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param customFieldId the ID of the custom field to include in the report.
 * @param customDimensionKeyId the ID of the custom key to include as a dimension in the report.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws IOException if unable to write the response to a file.
 * @throws InterruptedException if the thread is interrupted while waiting for the report to
 *     complete.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, long customFieldId, long customDimensionKeyId) throws IOException, InterruptedException {
    // Get the ReportService.
    ReportServiceInterface reportService = adManagerServices.get(session, ReportServiceInterface.class);
    // Create report query.
    ReportQuery reportQuery = new ReportQuery();
    reportQuery.setDimensions(new Dimension[] { Dimension.DATE, Dimension.CUSTOM_DIMENSION, Dimension.LINE_ITEM_ID });
    reportQuery.setColumns(new Column[] { Column.AD_SERVER_IMPRESSIONS });
    // Set the dynamic date range type or a custom start and end date.
    reportQuery.setDateRangeType(DateRangeType.LAST_MONTH);
    // Set the custom field IDs.
    reportQuery.setCustomFieldIds(new long[] { customFieldId });
    // Set the custom dimension IDs.
    reportQuery.setCustomDimensionKeyIds(new long[] { customDimensionKeyId });
    // Create report job.
    ReportJob reportJob = new ReportJob();
    reportJob.setReportQuery(reportQuery);
    // Run report job.
    reportJob = reportService.runReportJob(reportJob);
    // Create report downloader.
    ReportDownloader reportDownloader = new ReportDownloader(reportService, reportJob.getId());
    // Wait for the report to be ready.
    reportDownloader.waitForReportReady();
    // Change to your file location.
    File file = File.createTempFile("custom-field-report-", ".csv.gz");
    System.out.printf("Downloading report to %s ...", file.toString());
    // Download the report.
    ReportDownloadOptions options = new ReportDownloadOptions();
    options.setExportFormat(ExportFormat.CSV_DUMP);
    options.setUseGzipCompression(true);
    URL url = reportDownloader.getDownloadUrl(options);
    Resources.asByteSource(url).copyTo(Files.asByteSink(file));
    System.out.println("done.");
}
Also used : ReportDownloader(com.google.api.ads.admanager.axis.utils.v202202.ReportDownloader) ReportDownloadOptions(com.google.api.ads.admanager.axis.v202202.ReportDownloadOptions) ReportQuery(com.google.api.ads.admanager.axis.v202202.ReportQuery) ReportServiceInterface(com.google.api.ads.admanager.axis.v202202.ReportServiceInterface) ReportJob(com.google.api.ads.admanager.axis.v202202.ReportJob) File(java.io.File) URL(java.net.URL)

Example 19 with ReportQuery

use of com.google.api.ads.admanager.axis.v202202.ReportQuery in project googleads-java-lib by googleads.

the class RunInventoryReport method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices 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.
 * @throws IOException if the report's contents could not be written to a temp file.
 * @throws InterruptedException if the thread was interrupted while waiting for the report to be
 *     ready.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws IOException, InterruptedException {
    // Get the ReportService.
    ReportServiceInterface reportService = adManagerServices.get(session, ReportServiceInterface.class);
    // Create report query.
    ReportQuery reportQuery = new ReportQuery();
    reportQuery.setDimensions(new Dimension[] { Dimension.AD_UNIT_ID, Dimension.AD_UNIT_NAME });
    reportQuery.setColumns(new Column[] { Column.AD_SERVER_IMPRESSIONS, Column.AD_SERVER_CLICKS, Column.AD_EXCHANGE_IMPRESSIONS, Column.AD_EXCHANGE_CLICKS, Column.TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS, Column.TOTAL_LINE_ITEM_LEVEL_CPM_AND_CPC_REVENUE });
    // Set the ad unit view to hierarchical.
    reportQuery.setAdUnitView(ReportQueryAdUnitView.HIERARCHICAL);
    // Set the dynamic date range type or a custom start and end date.
    reportQuery.setDateRangeType(DateRangeType.YESTERDAY);
    // Create report job.
    ReportJob reportJob = new ReportJob();
    reportJob.setReportQuery(reportQuery);
    // Run report job.
    reportJob = reportService.runReportJob(reportJob);
    // Create report downloader.
    ReportDownloader reportDownloader = new ReportDownloader(reportService, reportJob.getId());
    // Wait for the report to be ready.
    reportDownloader.waitForReportReady();
    // Change to your file location.
    File file = File.createTempFile("inventory-report-", ".csv.gz");
    System.out.printf("Downloading report to %s ...", file.toString());
    // Download the report.
    ReportDownloadOptions options = new ReportDownloadOptions();
    options.setExportFormat(ExportFormat.CSV_DUMP);
    options.setUseGzipCompression(true);
    URL url = reportDownloader.getDownloadUrl(options);
    Resources.asByteSource(url).copyTo(Files.asByteSink(file));
    System.out.println("done.");
}
Also used : ReportDownloader(com.google.api.ads.admanager.axis.utils.v202202.ReportDownloader) ReportDownloadOptions(com.google.api.ads.admanager.axis.v202202.ReportDownloadOptions) ReportQuery(com.google.api.ads.admanager.axis.v202202.ReportQuery) ReportServiceInterface(com.google.api.ads.admanager.axis.v202202.ReportServiceInterface) ReportJob(com.google.api.ads.admanager.axis.v202202.ReportJob) File(java.io.File) URL(java.net.URL)

Example 20 with ReportQuery

use of com.google.api.ads.admanager.axis.v202202.ReportQuery in project googleads-java-lib by googleads.

the class RunReachReport method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices 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.
 * @throws IOException if unable to write the response to a file.
 * @throws InterruptedException if the thread is interrupted while waiting for the report to
 *     complete.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws IOException, InterruptedException {
    // Get the ReportService.
    ReportServiceInterface reportService = adManagerServices.get(session, ReportServiceInterface.class);
    // Create report query.
    ReportQuery reportQuery = new ReportQuery();
    reportQuery.setDimensions(new Dimension[] { Dimension.COUNTRY_NAME, Dimension.LINE_ITEM_ID, Dimension.LINE_ITEM_NAME });
    reportQuery.setColumns(new Column[] { Column.UNIQUE_REACH_FREQUENCY, Column.UNIQUE_REACH_IMPRESSIONS, Column.UNIQUE_REACH });
    // Set the dynamic date range type or a custom start and end date that is
    // the beginning of the week (Sunday) to the end of the week (Saturday), or
    // the first of the month to the end of the month.
    reportQuery.setDateRangeType(DateRangeType.REACH_LIFETIME);
    // Create report job.
    ReportJob reportJob = new ReportJob();
    reportJob.setReportQuery(reportQuery);
    // Run report job.
    reportJob = reportService.runReportJob(reportJob);
    // Create report downloader.
    ReportDownloader reportDownloader = new ReportDownloader(reportService, reportJob.getId());
    // Wait for the report to be ready.
    reportDownloader.waitForReportReady();
    // Change to your file location.
    File file = File.createTempFile("reach-report-", ".csv.gz");
    System.out.printf("Downloading report to %s ...", file.toString());
    // Download the report.
    ReportDownloadOptions options = new ReportDownloadOptions();
    options.setExportFormat(ExportFormat.CSV_DUMP);
    options.setUseGzipCompression(true);
    URL url = reportDownloader.getDownloadUrl(options);
    Resources.asByteSource(url).copyTo(Files.asByteSink(file));
    System.out.println("done.");
}
Also used : ReportDownloader(com.google.api.ads.admanager.axis.utils.v202108.ReportDownloader) ReportDownloadOptions(com.google.api.ads.admanager.axis.v202108.ReportDownloadOptions) ReportQuery(com.google.api.ads.admanager.axis.v202108.ReportQuery) ReportServiceInterface(com.google.api.ads.admanager.axis.v202108.ReportServiceInterface) ReportJob(com.google.api.ads.admanager.axis.v202108.ReportJob) File(java.io.File) URL(java.net.URL)

Aggregations

File (java.io.File)24 URL (java.net.URL)24 ReportDownloader (com.google.api.ads.admanager.axis.utils.v202108.ReportDownloader)8 ReportDownloader (com.google.api.ads.admanager.axis.utils.v202111.ReportDownloader)8 ReportDownloader (com.google.api.ads.admanager.axis.utils.v202202.ReportDownloader)8 ReportDownloadOptions (com.google.api.ads.admanager.axis.v202108.ReportDownloadOptions)8 ReportJob (com.google.api.ads.admanager.axis.v202108.ReportJob)8 ReportQuery (com.google.api.ads.admanager.axis.v202108.ReportQuery)8 ReportServiceInterface (com.google.api.ads.admanager.axis.v202108.ReportServiceInterface)8 ReportDownloadOptions (com.google.api.ads.admanager.axis.v202111.ReportDownloadOptions)8 ReportJob (com.google.api.ads.admanager.axis.v202111.ReportJob)8 ReportQuery (com.google.api.ads.admanager.axis.v202111.ReportQuery)8 ReportServiceInterface (com.google.api.ads.admanager.axis.v202111.ReportServiceInterface)8 ReportDownloadOptions (com.google.api.ads.admanager.axis.v202202.ReportDownloadOptions)8 ReportJob (com.google.api.ads.admanager.axis.v202202.ReportJob)8 ReportQuery (com.google.api.ads.admanager.axis.v202202.ReportQuery)8 ReportServiceInterface (com.google.api.ads.admanager.axis.v202202.ReportServiceInterface)8 ReportCallback (com.google.api.ads.admanager.lib.utils.ReportCallback)3 ConfigurationLoadException (com.google.api.ads.common.lib.conf.ConfigurationLoadException)3 OAuthException (com.google.api.ads.common.lib.exception.OAuthException)3