Search in sources :

Example 6 with ReportingConfiguration

use of com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration 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 7 with ReportingConfiguration

use of com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration 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 8 with ReportingConfiguration

use of com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration in project googleads-java-lib by googleads.

the class ReportRequestFactoryHelper method createHeaders.

/**
 * 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;
}
Also used : HttpHeaders(com.google.api.client.http.HttpHeaders) ReportingConfiguration(com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration)

Example 9 with ReportingConfiguration

use of com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration in project googleads-java-lib by googleads.

the class ReportRequestFactoryHelperTest method data.

@Parameters(name = "version={0}, reportingConfiguration={1}")
public static Collection<Object[]> data() {
    Collection<Object[]> parameters = new ArrayList<Object[]>();
    List<Boolean> booleanValues = Arrays.<Boolean>asList(true, false, null);
    List<String> versions = Arrays.<String>asList("v201809");
    List<Integer> timeouts = Arrays.<Integer>asList(5_000_000, null);
    // Create a reporting configuration for all combinations of attribute values from the lists
    // above.
    List<ReportingConfiguration> reportingConfigs = new ArrayList<>();
    for (Boolean isSkipReportHeader : booleanValues) {
        for (Boolean isSkipColumnHeader : booleanValues) {
            for (Boolean isSkipReportSummary : booleanValues) {
                for (Boolean isIncludeZeroImpressions : booleanValues) {
                    for (Boolean isUseRawEnumValues : booleanValues) {
                        for (Integer timeout : timeouts) {
                            ReportingConfiguration reportingConfig = null;
                            if (isSkipReportHeader != null || isSkipColumnHeader != null || isSkipReportSummary != null || isIncludeZeroImpressions != null || isUseRawEnumValues != null || timeout != null) {
                                reportingConfig = new ReportingConfiguration.Builder().skipReportHeader(isSkipReportHeader).skipColumnHeader(isSkipColumnHeader).skipReportSummary(isSkipReportSummary).includeZeroImpressions(isIncludeZeroImpressions).useRawEnumValues(isUseRawEnumValues).reportDownloadTimeout(timeout).build();
                            }
                            reportingConfigs.add(reportingConfig);
                        }
                    }
                }
            }
        }
    }
    versions.forEach(version -> reportingConfigs.forEach(reportingConfig -> parameters.add(new Object[] { version, reportingConfig })));
    // Also test the case where the reporting config is null.
    reportingConfigs.add(null);
    return parameters;
}
Also used : LowLevelHttpRequest(com.google.api.client.http.LowLevelHttpRequest) Arrays(java.util.Arrays) GenericAdWordsServices(com.google.api.ads.adwords.lib.utils.testing.GenericAdWordsServices) AuthenticationException(com.google.api.ads.common.lib.exception.AuthenticationException) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) UserAgentCombiner(com.google.api.ads.common.lib.useragent.UserAgentCombiner) HttpRequest(com.google.api.client.http.HttpRequest) ArrayList(java.util.ArrayList) MockitoAnnotations(org.mockito.MockitoAnnotations) GenericUrl(com.google.api.client.http.GenericUrl) Credential(com.google.api.client.auth.oauth2.Credential) Nullable(javax.annotation.Nullable) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) AuthorizationHeaderProvider(com.google.api.ads.common.lib.auth.AuthorizationHeaderProvider) HttpHeaders(com.google.api.client.http.HttpHeaders) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) Assert.assertNotNull(org.junit.Assert.assertNotNull) Collection(java.util.Collection) HttpTransport(com.google.api.client.http.HttpTransport) Assert.assertTrue(org.junit.Assert.assertTrue) AdWordsLibConfiguration(com.google.api.ads.adwords.lib.conf.AdWordsLibConfiguration) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) List(java.util.List) AdWordsSession(com.google.api.ads.adwords.lib.client.AdWordsSession) ValidationException(com.google.api.ads.common.lib.exception.ValidationException) Assert.assertFalse(org.junit.Assert.assertFalse) ReportingConfiguration(com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ReportingConfiguration(com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration) Parameters(org.junit.runners.Parameterized.Parameters)

Aggregations

ReportingConfiguration (com.google.api.ads.adwords.lib.client.reporting.ReportingConfiguration)9 ReportDownloadResponse (com.google.api.ads.adwords.lib.utils.ReportDownloadResponse)4 ImmutableAdWordsSession (com.google.api.ads.adwords.lib.client.AdWordsSession.ImmutableAdWordsSession)3 ReportDefinition (com.google.api.ads.adwords.lib.jaxb.v201809.ReportDefinition)3 ReportDownloaderInterface (com.google.api.ads.adwords.lib.utils.v201809.ReportDownloaderInterface)3 Builder (com.google.api.ads.adwords.lib.client.AdWordsSession.Builder)2 Selector (com.google.api.ads.adwords.lib.jaxb.v201809.Selector)2 ReportQuery (com.google.api.ads.adwords.lib.utils.v201809.ReportQuery)2 ValidationException (com.google.api.ads.common.lib.exception.ValidationException)2 HttpHeaders (com.google.api.client.http.HttpHeaders)2 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 ApiException (com.google.api.ads.adwords.axis.v201809.cm.ApiException)1 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