com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration Java Examples

The following examples show how to use com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration. 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: InstalledOAuth2Authenticator.java    From adwords-alerting with Apache License 2.0 6 votes vote down vote up
/**
 * Get an immutable adwords session from the authentication info.
 */
@Override
public ImmutableAdWordsSession authenticate() throws OAuthException, ValidationException {
  // For easy processing, skip report header and summary (but keep column header).
  ReportingConfiguration reportingConfig =
      new ReportingConfiguration.Builder()
          .skipReportHeader(true)
          .skipColumnHeader(false)
          .skipReportSummary(true)
          .includeZeroImpressions(true)
          .build();
  
  return new AdWordsSession.Builder()
      .withOAuth2Credential(getOAuth2Credential())
      .withUserAgent(this.userAgent)
      .withClientCustomerId(this.managerAccountId)
      .withDeveloperToken(this.developerToken)
      .withReportingConfiguration(reportingConfig)
      .buildImmutable();
}
 
Example #2
Source File: AdWordsSessionTest.java    From googleads-java-lib with Apache License 2.0 6 votes vote down vote up
public AdWordsSessionTest(boolean isImmutable) {
  this.isImmutable = isImmutable;
  this.credential = new Credential(BearerToken.authorizationHeaderAccessMethod());

  this.reportingConfiguration =
      new ReportingConfiguration.Builder().skipReportHeader(true).skipReportSummary(true).build();

  this.allSettingsBuilder =
      new AdWordsSession.Builder()
          .withClientCustomerId("customer id")
          .withDeveloperToken("developer token")
          .withEndpoint("https://www.google.com")
          .enablePartialFailure()
          .enableValidateOnly()
          .withOAuth2Credential(credential)
          .withUserAgent("user agent")
          .withReportingConfiguration(reportingConfiguration);
}
 
Example #3
Source File: AdWordsSessionTest.java    From googleads-java-lib with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuilder_withReportingConfiguration() throws Exception {
  ReportingConfiguration reportingConfiguration =
      new ReportingConfiguration.Builder().skipReportHeader(true).skipReportSummary(true).build();

  AdWordsSession adWordsSession =
      build(
          new AdWordsSession.Builder()
              .withUserAgent("FooBar")
              .withEndpoint("https://www.google.com")
              .withOAuth2Credential(credential)
              .withDeveloperToken("developerToken")
              .withReportingConfiguration(reportingConfiguration));

  ReportingConfiguration sessionReportingConfig = adWordsSession.getReportingConfiguration();
  assertNotNull(
      "reporting configuration should not be null when passed to the builder",
      sessionReportingConfig);
}
 
Example #4
Source File: ReportRequestFactoryHelper.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the http headers object for this request, populated from data in
 * the session.
 * @throws AuthenticationException If OAuth authorization fails.
 */
private HttpHeaders createHeaders(String reportUrl, String version)
    throws AuthenticationException {
  HttpHeaders httpHeaders = new HttpHeaders();
  httpHeaders.setAuthorization(
      authorizationHeaderProvider.getAuthorizationHeader(session, reportUrl));
  httpHeaders.setUserAgent(userAgentCombiner.getUserAgent(session.getUserAgent()));
  httpHeaders.set("developerToken", session.getDeveloperToken());
  httpHeaders.set("clientCustomerId", session.getClientCustomerId());
  ReportingConfiguration reportingConfiguration = session.getReportingConfiguration();
  if (reportingConfiguration != null) {
    reportingConfiguration.validate(version);
    if (reportingConfiguration.isSkipReportHeader() != null) {
      httpHeaders.set("skipReportHeader",
          Boolean.toString(reportingConfiguration.isSkipReportHeader()));
    }
    if (reportingConfiguration.isSkipColumnHeader() != null) {
      httpHeaders.set("skipColumnHeader",
          Boolean.toString(reportingConfiguration.isSkipColumnHeader()));
    }
    if (reportingConfiguration.isSkipReportSummary() != null) {
      httpHeaders.set("skipReportSummary",
          Boolean.toString(reportingConfiguration.isSkipReportSummary()));
    }
    if (reportingConfiguration.isIncludeZeroImpressions() != null) {
      httpHeaders.set(
          "includeZeroImpressions",
          Boolean.toString(reportingConfiguration.isIncludeZeroImpressions()));
    }
    if (reportingConfiguration.isUseRawEnumValues() != null) {
      httpHeaders.set(
          "useRawEnumValues",
          Boolean.toString(reportingConfiguration.isUseRawEnumValues()));
    }
  }
  return httpHeaders;
}
 
Example #5
Source File: AdWordsSession.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/**
 * Reads properties from the provided {@link Configuration} object.<br><br>
 * Known properties:
 * <ul>
 * <li>api.adwords.clientCustomerId</li>
 * <li>api.adwords.userAgent</li>
 * <li>api.adwords.developerToken</li>
 * <li>api.adwords.isPartialFailure</li>
 * <li>api.adwords.endpoint</li>
 * <li>api.adwords.reporting.skipHeader</li>
 * <li>api.adwords.reporting.skipColumnHeader</li>
 * <li>api.adwords.reporting.skipSummary</li>
 * </ul>
 *
 * @param config
 * @return Builder populated from the Configuration
 */
@Override
public Builder from(Configuration config) {
  this.clientCustomerId = config.getString("api.adwords.clientCustomerId", null);
  this.userAgent = config.getString("api.adwords.userAgent", null);
  this.developerToken = config.getString("api.adwords.developerToken", null);
  this.isPartialFailure = config.getBoolean("api.adwords.isPartialFailure", null);
  this.endpoint = config.getString("api.adwords.endpoint", null);

  // Only create a ReportConfiguration for this object if at least one reporting
  // configuration config value is present.
  Boolean isSkipReportHeader = config.getBoolean("api.adwords.reporting.skipHeader", null);
  Boolean isSkipColumnHeader =
      config.getBoolean("api.adwords.reporting.skipColumnHeader", null);
  Boolean isSkipReportSummary = config.getBoolean("api.adwords.reporting.skipSummary", null);
  Boolean isUseRawEnumValues =
      config.getBoolean("api.adwords.reporting.useRawEnumValues", null);
  Integer reportDownloadTimeout = config.getInteger("api.adwords.reportDownloadTimeout", null);
  this.reportingConfiguration =
      new ReportingConfiguration.Builder()
          .skipReportHeader(isSkipReportHeader)
          .skipColumnHeader(isSkipColumnHeader)
          .skipReportSummary(isSkipReportSummary)
          .useRawEnumValues(isUseRawEnumValues)
          .reportDownloadTimeout(reportDownloadTimeout)
          .build();

  return this;
}
 
Example #6
Source File: AdWordsSessionTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
/** Tests that the builder correctly reads properties from a configuration. */
@Test
public void testReadPropertiesFromConfiguration() throws ValidationException {
  PropertiesConfiguration config = new PropertiesConfiguration();
  config.setProperty("api.adwords.clientCustomerId", "1234567890");
  config.setProperty("api.adwords.userAgent", "FooBar");
  config.setProperty("api.adwords.developerToken", "devTokendevTokendevTok");
  config.setProperty("api.adwords.isPartialFailure", "false");

  AdWordsSession session =
      build(new AdWordsSession.Builder().from(config).withOAuth2Credential(credential));
  assertEquals("1234567890", session.getClientCustomerId());
  assertEquals("FooBar", session.getUserAgent());
  assertEquals("devTokendevTokendevTok", session.getDeveloperToken());
  assertFalse(session.isPartialFailure());
  ReportingConfiguration reportingConfig = session.getReportingConfiguration();
  assertNotNull("reporting configuration is null", reportingConfig);
  // Verify that the ReportingConfiguration's attributes are set to the expected default value
  // (null).
  assertNull(
      "include zero impressions is not null when no reporting options in config",
      reportingConfig.isIncludeZeroImpressions());
  assertNull(
      "skip column header is not null, but no reporting options in config",
      reportingConfig.isSkipColumnHeader());
  assertNull(
      "skip report header is not null, but no reporting options in config",
      reportingConfig.isSkipReportHeader());
  assertNull(
      "skip report summary is not null, but no reporting options in config",
      reportingConfig.isSkipReportSummary());
  assertNull(
      "use raw enum values is not null, but no reporting options in config",
      reportingConfig.isUseRawEnumValues());
  assertNull(
      "download timeout is not null, but no reporting options in config",
      reportingConfig.getReportDownloadTimeout());
}
 
Example #7
Source File: ReportProcessor.java    From aw-reporting with Apache License 2.0 5 votes vote down vote up
private ReportingConfiguration getReportingConfiguration(Properties properties) {
  ReportingConfiguration reportingConfig =
      new ReportingConfiguration.Builder()
          .skipReportHeader(true)
          .skipColumnHeader(false)
          .skipReportSummary(true)
          .includeZeroImpressions(getIncludeZeroImpressions(properties))
          .useRawEnumValues(getUseRawEnumValues(properties))
          .build();
  return reportingConfig;
}
 
Example #8
Source File: AdWordsSession.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Gets the reporting configuration.
 */
@Nullable
public ReportingConfiguration getReportingConfiguration() {
  return reportingConfiguration;
}
 
Example #9
Source File: AdWordsSession.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Sets the reporting configuration.
 */
public void setReportingConfiguration(@Nullable ReportingConfiguration reportingConfiguration) {
  this.reportingConfiguration = reportingConfiguration;
}
 
Example #10
Source File: AdWordsSession.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
@Override
public final void setReportingConfiguration(
    @Nullable ReportingConfiguration reportingConfiguration) {
  throwUnsupportedOperationException("reportingConfiguration");
}
 
Example #11
Source File: AdWordsSession.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
public Builder withReportingConfiguration(ReportingConfiguration reportingConfiguration) {
  this.reportingConfiguration = reportingConfiguration;
  return this;
}
 
Example #12
Source File: ReportRequestFactoryHelperTest.java    From googleads-java-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Values for these arguments are supplied by the {@link #data()} method.
 *
 * @param version version of the AdWords API
 */
public ReportRequestFactoryHelperTest(
    String version, ReportingConfiguration reportingConfiguration) {
  this.version = version;
  this.reportingConfiguration = reportingConfiguration;
}
 
Example #13
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 #14
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 #15
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);
}
 
Example #16
Source File: ReportProcessor.java    From aw-reporting with Apache License 2.0 4 votes vote down vote up
/**
 * Generate all the mapped reports of (optionally) given account IDs under the specified
 * manager account.
 *
 * @param topCustomerId the top customer account id.
 * @param dateRangeAndType the date range and type.
 * @param accountIdsSet the account IDs, if missing it means all accounts under the topCustomerId
 * @param properties the properties file
 * @throws ReportProcessingException error invoking the API calls.
 */
public void generateReportsForManagerAccount(
    String topCustomerId,
    DateRangeAndType dateRangeAndType,
    Set<Long> accountIdsSet,
    Properties properties)
    throws ReportProcessingException {
  logger.info("*** Retrieving account IDs ***");

  if (accountIdsSet == null || accountIdsSet.size() == 0) {
    boolean excludeHiddenAccounts = getExcludeHiddenAccounts(properties);
    accountIdsSet = retrieveAccountIds(topCustomerId, excludeHiddenAccounts);
  } else {
    logger.info("Accounts loaded from file.");
  }

  ReportingConfiguration reportingConfig = getReportingConfiguration(properties);
  sessionBuilder.withReportingConfiguration(reportingConfig);

  logger.info("*** Generating Reports for " + accountIdsSet.size() + " accounts ***");
  Stopwatch stopwatch = Stopwatch.createStarted();
  Set<ReportDefinitionReportType> reports = csvReportEntitiesMapping.getDefinedReports();

  // reports
  Set<Object> propertiesKeys = properties.keySet();
  for (Object key : propertiesKeys) {

    String reportDefinitionKey = key.toString();
    ReportDefinitionReportType reportType = extractReportTypeFromKey(reportDefinitionKey);
    if (reportType != null && reports.contains(reportType)) {
      try {
        downloadAndProcess(
            topCustomerId,
            reportType,
            dateRangeAndType,
            accountIdsSet,
            reportDefinitionKey,
            properties);
      } catch (ReportProcessingException e) {
        logger.error("Unable to download and process " + reportType + " for " + topCustomerId, e);
      }
    }
  }

  stopwatch.stop();
  logger.info(
      "*** Finished processing all reports in "
          + stopwatch.elapsed(TimeUnit.SECONDS)
          + " seconds ***\n");
}