Java Code Examples for com.google.api.ads.adwords.lib.client.AdWordsSession#setReportingConfiguration()

The following examples show how to use com.google.api.ads.adwords.lib.client.AdWordsSession#setReportingConfiguration() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DownloadCriteriaReportWithAwql.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/**
 * 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)
          // Set to false to exclude rows with zero impressions.
          .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);
}
 
Example 2
Source File: DownloadCriteriaReportWithSelector.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/**
 * 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)
          // Enable to allow rows with zero impressions to show.
          .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);
}
 
Example 3
Source File: AdvancedCreateCredentialFromScratch.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/**
 * 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()
          // Enable to allow rows with zero impressions to show.
          .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);
}