Search in sources :

Example 26 with StatementBuilder

use of com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder in project googleads-java-lib by googleads.

the class GetRecentChanges method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param filePath file path where changes will be saved.
 * @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.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, String filePath) throws IOException {
    PublisherQueryLanguageServiceInterface pqlService = adManagerServices.get(session, PublisherQueryLanguageServiceInterface.class);
    // Create statement to select recent changes. Change_History only supports ordering by
    // descending ChangeDateTime. Offset is not supported. To page, use the change ID of
    // the earliest change as a pagination token. A date time range is required
    // when querying this table.
    DateTime endDateTime = DateTime.now();
    DateTime startDateTime = endDateTime.minusDays(1);
    StatementBuilder statementBuilder = new StatementBuilder().select("Id, ChangeDateTime, EntityId, EntityType, Operation, UserId").from("Change_History").where("ChangeDateTime < :endDateTime AND ChangeDateTime > :startDateTime").orderBy("ChangeDateTime DESC").withBindVariableValue("startDateTime", DateTimes.toDateTime(startDateTime)).withBindVariableValue("endDateTime", DateTimes.toDateTime(endDateTime)).limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Retrieve a small amount of changes at a time, paging through
    // until all changes have been retrieved.
    ResultSet combinedResultSet = null;
    ResultSet resultSet;
    int i = 0;
    do {
        resultSet = pqlService.select(statementBuilder.toStatement());
        // Combine result sets with previous ones.
        combinedResultSet = combinedResultSet == null ? resultSet : Pql.combineResultSets(combinedResultSet, resultSet);
        if (resultSet.getRows() != null && resultSet.getRows().length > 0) {
            // Get the earliest change ID in the result set.
            int numRows = resultSet.getRows().length;
            Row lastRow = resultSet.getRows(numRows - 1);
            String id = (String) Pql.getNativeValue(lastRow.getValues(0));
            System.out.printf("%d) %d changes prior to ID %s were found.%n", i++, numRows, id);
            // Use the earliest change ID in the result set to page.
            statementBuilder.where("Id < :id AND ChangeDateTime < :endDateTime AND ChangeDateTime > :startDateTime").withBindVariableValue("id", id);
        }
    } while (resultSet.getRows() != null && resultSet.getRows().length > 0);
    // Write the result set to a CSV.
    CsvFiles.writeCsv(Pql.resultSetToStringArrayList(combinedResultSet), filePath);
    System.out.printf("Recent changes saved to: %s%n", filePath);
}
Also used : PublisherQueryLanguageServiceInterface(com.google.api.ads.admanager.axis.v202108.PublisherQueryLanguageServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) ResultSet(com.google.api.ads.admanager.axis.v202108.ResultSet) Row(com.google.api.ads.admanager.axis.v202108.Row) DateTime(org.joda.time.DateTime)

Example 27 with StatementBuilder

use of com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder in project googleads-java-lib by googleads.

the class GetAllSites 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.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    // Get the SiteService.
    SiteServiceInterface siteService = adManagerServices.get(session, SiteServiceInterface.class);
    // Create a statement to get all sites.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get sites by statement.
        SitePage page = siteService.getSitesByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            int i = page.getStartIndex();
            for (Site site : page.getResults()) {
                System.out.printf("%d) Site with ID %d and URL '%s' was found.%n", i++, site.getId(), site.getUrl());
            }
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    System.out.printf("Number of results found: %d%n", totalResultSetSize);
}
Also used : Site(com.google.api.ads.admanager.axis.v202108.Site) SitePage(com.google.api.ads.admanager.axis.v202108.SitePage) SiteServiceInterface(com.google.api.ads.admanager.axis.v202108.SiteServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)

Example 28 with StatementBuilder

use of com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder in project googleads-java-lib by googleads.

the class GetAllLineItems 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.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session) throws IOException {
    // Get the PublisherQueryLanguageService.
    PublisherQueryLanguageServiceInterface pqlService = adManagerServices.get(session, PublisherQueryLanguageServiceInterface.class);
    // Create statement to select all line items.
    StatementBuilder statementBuilder = new StatementBuilder().select("Id, Name, Status").from("Line_Item").orderBy("Id ASC").offset(0).limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for result sets.
    ResultSet combinedResultSet = null;
    ResultSet resultSet;
    int i = 0;
    do {
        // Get all line items.
        resultSet = pqlService.select(statementBuilder.toStatement());
        // Combine result sets with previous ones.
        combinedResultSet = combinedResultSet == null ? resultSet : Pql.combineResultSets(combinedResultSet, resultSet);
        System.out.printf("%d) %d line items beginning at offset %d were found.%n", i++, resultSet.getRows() == null ? 0 : resultSet.getRows().length, statementBuilder.getOffset());
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (resultSet.getRows() != null && resultSet.getRows().length > 0);
    // Change to your file location.
    String filePath = File.createTempFile("Line-Items-", ".csv").toString();
    // Write the result set to a CSV.
    CsvFiles.writeCsv(Pql.resultSetToStringArrayList(combinedResultSet), filePath);
    System.out.printf("Line items saved to: %s%n", filePath);
}
Also used : PublisherQueryLanguageServiceInterface(com.google.api.ads.admanager.axis.v202108.PublisherQueryLanguageServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) ResultSet(com.google.api.ads.admanager.axis.v202108.ResultSet)

Example 29 with StatementBuilder

use of com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder in project googleads-java-lib by googleads.

the class RunDeliveryReportForOrder method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param orderId the ID of the order to run the report for.
 * @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 orderId) 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.ORDER_ID });
    reportQuery.setColumns(new Column[] { Column.AD_SERVER_IMPRESSIONS, Column.AD_SERVER_CLICKS, Column.AD_SERVER_CTR, Column.AD_SERVER_CPM_AND_CPC_REVENUE });
    reportQuery.setDimensionAttributes(new DimensionAttribute[] { DimensionAttribute.ORDER_TRAFFICKER, DimensionAttribute.ORDER_START_DATE_TIME, DimensionAttribute.ORDER_END_DATE_TIME });
    // Create statement to filter for an order.
    StatementBuilder statementBuilder = new StatementBuilder().where("ORDER_ID = :orderId").withBindVariableValue("orderId", orderId);
    // Set the filter statement.
    reportQuery.setStatement(statementBuilder.toStatement());
    // Set the start and end dates or choose a dynamic date range type.
    reportQuery.setDateRangeType(DateRangeType.CUSTOM_DATE);
    reportQuery.setStartDate(DateTimes.toDateTime("2013-05-01T00:00:00", "America/New_York").getDate());
    reportQuery.setEndDate(DateTimes.toDateTime("2013-05-31T00:00:00", "America/New_York").getDate());
    // 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("delivery-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) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) ReportJob(com.google.api.ads.admanager.axis.v202108.ReportJob) File(java.io.File) URL(java.net.URL)

Example 30 with StatementBuilder

use of com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder in project googleads-java-lib by googleads.

the class CreatePlacements method getAllAdUnits.

/**
 * Gets all ad units.
 */
public static List<AdUnit> getAllAdUnits(AdManagerServices adManagerServices, AdManagerSession session) throws RemoteException {
    List<AdUnit> adUnits = new ArrayList<>();
    // Get the InventoryService.
    InventoryServiceInterface inventoryService = adManagerServices.get(session, InventoryServiceInterface.class);
    // Create a statement to select all ad units.
    StatementBuilder statementBuilder = new StatementBuilder().orderBy("id ASC").limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Default for total result set size.
    int totalResultSetSize = 0;
    do {
        // Get ad units by statement.
        AdUnitPage page = inventoryService.getAdUnitsByStatement(statementBuilder.toStatement());
        if (page.getResults() != null) {
            totalResultSetSize = page.getTotalResultSetSize();
            Collections.addAll(adUnits, page.getResults());
        }
        statementBuilder.increaseOffsetBy(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    } while (statementBuilder.getOffset() < totalResultSetSize);
    return adUnits;
}
Also used : AdUnitPage(com.google.api.ads.admanager.axis.v202108.AdUnitPage) AdUnit(com.google.api.ads.admanager.axis.v202108.AdUnit) InventoryServiceInterface(com.google.api.ads.admanager.axis.v202108.InventoryServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder) ArrayList(java.util.ArrayList)

Aggregations

StatementBuilder (com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)120 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202111.StatementBuilder)120 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)120 UpdateResult (com.google.api.ads.admanager.axis.v202108.UpdateResult)18 UpdateResult (com.google.api.ads.admanager.axis.v202111.UpdateResult)18 UpdateResult (com.google.api.ads.admanager.axis.v202202.UpdateResult)18 ArrayList (java.util.ArrayList)18 CustomTargetingServiceInterface (com.google.api.ads.admanager.axis.v202108.CustomTargetingServiceInterface)8 InventoryServiceInterface (com.google.api.ads.admanager.axis.v202108.InventoryServiceInterface)8 PublisherQueryLanguageServiceInterface (com.google.api.ads.admanager.axis.v202108.PublisherQueryLanguageServiceInterface)8 ResultSet (com.google.api.ads.admanager.axis.v202108.ResultSet)8 InventoryServiceInterface (com.google.api.ads.admanager.axis.v202202.InventoryServiceInterface)8 AdUnit (com.google.api.ads.admanager.axis.v202108.AdUnit)7 AdUnitPage (com.google.api.ads.admanager.axis.v202108.AdUnitPage)7 CustomTargetingServiceInterface (com.google.api.ads.admanager.axis.v202202.CustomTargetingServiceInterface)6 CustomTargetingServiceInterface (com.google.api.ads.admanager.axis.v202111.CustomTargetingServiceInterface)5 InventoryServiceInterface (com.google.api.ads.admanager.axis.v202111.InventoryServiceInterface)5 ProposalServiceInterface (com.google.api.ads.admanager.axis.v202111.ProposalServiceInterface)5 PublisherQueryLanguageServiceInterface (com.google.api.ads.admanager.axis.v202111.PublisherQueryLanguageServiceInterface)5 ResultSet (com.google.api.ads.admanager.axis.v202111.ResultSet)5