// Copyright 2022 Luca Casonato. All rights reserved. MIT license. /** * DoubleClick Bid Manager API Client for Deno * =========================================== * * DoubleClick Bid Manager API allows users to manage and create campaigns and reports. * * Docs: https://developers.google.com/bid-manager/ * Source: https://googleapis.deno.dev/v1/doubleclickbidmanager:v2.ts */ import { auth, CredentialsClient, GoogleAuth, request } from "/_/base@v1/mod.ts"; export { auth, GoogleAuth }; export type { CredentialsClient }; /** * DoubleClick Bid Manager API allows users to manage and create campaigns and * reports. */ export class DoubleClickBidManager { #client: CredentialsClient | undefined; #baseUrl: string; constructor(client?: CredentialsClient, baseUrl: string = "https://doubleclickbidmanager.googleapis.com/v2/") { this.#client = client; this.#baseUrl = baseUrl; } /** * Creates a new query. * */ async queriesCreate(req: Query): Promise { const url = new URL(`${this.#baseUrl}queries`); const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return data as Query; } /** * Deletes an existing query as well as its generated reports. * * @param queryId Required. The ID of the query to delete. */ async queriesDelete(queryId: bigint): Promise { queryId = String(queryId); const url = new URL(`${this.#baseUrl}queries/${ queryId }`); const data = await request(url.href, { client: this.#client, method: "DELETE", }); } /** * Retrieves a query. * * @param queryId Required. The ID of the query to retrieve. */ async queriesGet(queryId: bigint): Promise { queryId = String(queryId); const url = new URL(`${this.#baseUrl}queries/${ queryId }`); const data = await request(url.href, { client: this.#client, method: "GET", }); return data as Query; } /** * Lists queries created by the current user. * */ async queriesList(opts: QueriesListOptions = {}): Promise { const url = new URL(`${this.#baseUrl}queries`); if (opts.orderBy !== undefined) { url.searchParams.append("orderBy", String(opts.orderBy)); } if (opts.pageSize !== undefined) { url.searchParams.append("pageSize", String(opts.pageSize)); } if (opts.pageToken !== undefined) { url.searchParams.append("pageToken", String(opts.pageToken)); } const data = await request(url.href, { client: this.#client, method: "GET", }); return data as ListQueriesResponse; } /** * Retrieves a report. * * @param queryId Required. The ID of the query that generated the report. * @param reportId Required. The ID of the query to retrieve. */ async queriesReportsGet(queryId: bigint, reportId: bigint): Promise { queryId = String(queryId); reportId = String(reportId); const url = new URL(`${this.#baseUrl}queries/${ queryId }/reports/${ reportId }`); const data = await request(url.href, { client: this.#client, method: "GET", }); return data as Report; } /** * Lists reports generated by the provided query. * * @param queryId Required. The ID of the query that generated the reports. */ async queriesReportsList(queryId: bigint, opts: QueriesReportsListOptions = {}): Promise { queryId = String(queryId); const url = new URL(`${this.#baseUrl}queries/${ queryId }/reports`); if (opts.orderBy !== undefined) { url.searchParams.append("orderBy", String(opts.orderBy)); } if (opts.pageSize !== undefined) { url.searchParams.append("pageSize", String(opts.pageSize)); } if (opts.pageToken !== undefined) { url.searchParams.append("pageToken", String(opts.pageToken)); } const data = await request(url.href, { client: this.#client, method: "GET", }); return data as ListReportsResponse; } /** * Runs an existing query to generate a report. * * @param queryId Required. The ID of the query to run. */ async queriesRun(queryId: bigint, req: RunQueryRequest, opts: QueriesRunOptions = {}): Promise { queryId = String(queryId); const url = new URL(`${this.#baseUrl}queries/${ queryId }:run`); if (opts.synchronous !== undefined) { url.searchParams.append("synchronous", String(opts.synchronous)); } const body = JSON.stringify(req); const data = await request(url.href, { client: this.#client, method: "POST", body, }); return data as Report; } } /** * The date range to be reported on. */ export interface DataRange { /** * If `CUSTOM_DATES` is assigned to range, this field specifies the end date * for the date range that is reported on. This field is required if using * `CUSTOM_DATES` range and will be ignored otherwise. */ customEndDate?: Date; /** * If `CUSTOM_DATES` is assigned to range, this field specifies the starting * date for the date range that is reported on. This field is required if * using `CUSTOM_DATES` range and will be ignored otherwise. */ customStartDate?: Date; /** * The preset date range to be reported on. If `CUSTOM_DATES` is assigned to * this field, fields custom_start_date and custom_end_date must be set to * specify the custom date range. */ range?: | "RANGE_UNSPECIFIED" | "CUSTOM_DATES" | "CURRENT_DAY" | "PREVIOUS_DAY" | "WEEK_TO_DATE" | "MONTH_TO_DATE" | "QUARTER_TO_DATE" | "YEAR_TO_DATE" | "PREVIOUS_WEEK" | "PREVIOUS_MONTH" | "PREVIOUS_QUARTER" | "PREVIOUS_YEAR" | "LAST_7_DAYS" | "LAST_30_DAYS" | "LAST_90_DAYS" | "LAST_365_DAYS" | "ALL_TIME" | "LAST_14_DAYS" | "LAST_60_DAYS"; } /** * Represents a whole or partial calendar date, such as a birthday. The time of * day and time zone are either specified elsewhere or are insignificant. The * date is relative to the Gregorian Calendar. This can represent one of the * following: * A full date, with non-zero year, month, and day values. * A * month and day, with a zero year (for example, an anniversary). * A year on * its own, with a zero month and a zero day. * A year and month, with a zero * day (for example, a credit card expiration date). Related types: * * google.type.TimeOfDay * google.type.DateTime * google.protobuf.Timestamp */ export interface Date { /** * Day of a month. Must be from 1 to 31 and valid for the year and month, or * 0 to specify a year by itself or a year and month where the day isn't * significant. */ day?: number; /** * Month of a year. Must be from 1 to 12, or 0 to specify a year without a * month and day. */ month?: number; /** * Year of the date. Must be from 1 to 9999, or 0 to specify a date without a * year. */ year?: number; } /** * Represents a single filter rule. */ export interface FilterPair { /** * The type of value to filter by. Defined by a * [Filter](/bid-manager/reference/rest/v2/filters-metrics#filters) value. */ type?: string; /** * The identifying value to filter by, such as a relevant resource ID. */ value?: string; } export interface ListQueriesResponse { /** * A token to retrieve the next page of results. Pass this value in the * page_token field in the subsequent call to `queries.list` method to * retrieve the next page of results. */ nextPageToken?: string; /** * The list of queries. This field will be absent if empty. */ queries?: Query[]; } export interface ListReportsResponse { /** * A token to retrieve the next page of results. Pass this value in the * page_token field in the subsequent call to `queries.reports.list` method to * retrieve the next page of results. */ nextPageToken?: string; /** * The list of reports. This field will be absent if empty. */ reports?: Report[]; } /** * Report parameter options. */ export interface Options { /** * Whether to include data for audience lists specifically targeted by * filtered line items or insertion orders. Requires the use of * `FILTER_INSERTION_ORDER` or `FILTER_LINE_ITEM` filters. */ includeOnlyTargetedUserLists?: boolean; } /** * Parameters of a generated report. */ export interface Parameters { /** * Filters to limit the scope of reported data. */ filters?: FilterPair[]; /** * Dimensions by which to segment and group the data. Defined by * [Filter](/bid-manager/reference/rest/v2/filters-metrics#filters) values. */ groupBys?: string[]; /** * Metrics to define the data populating the report. Defined by * [Metric](/bid-manager/reference/rest/v2/filters-metrics#metrics) values. */ metrics?: string[]; /** * Additional report parameter options. */ options?: Options; /** * The type of the report. The type of the report determines the dimesions, * filters, and metrics that can be used. */ type?: | "REPORT_TYPE_UNSPECIFIED" | "STANDARD" | "INVENTORY_AVAILABILITY" | "AUDIENCE_COMPOSITION" | "FLOODLIGHT" | "YOUTUBE" | "GRP" | "YOUTUBE_PROGRAMMATIC_GUARANTEED" | "REACH" | "UNIQUE_REACH_AUDIENCE" | "FULL_PATH" | "PATH_ATTRIBUTION"; } /** * Additional options for DoubleClickBidManager#queriesList. */ export interface QueriesListOptions { /** * Field to sort the list by. Accepts the following values: * `queryId` * (default) * `metadata.title` The default sorting order is ascending. To * specify descending order for a field, add the suffix `desc` to the field * name. For example, `queryId desc`. */ orderBy?: string; /** * Maximum number of results per page. Must be between `1` and `100`. * Defaults to `100` if unspecified. */ pageSize?: number; /** * A token identifying which page of results the server should return. * Typically, this is the value of nextPageToken, returned from the previous * call to the `queries.list` method. If unspecified, the first page of * results is returned. */ pageToken?: string; } /** * Additional options for DoubleClickBidManager#queriesReportsList. */ export interface QueriesReportsListOptions { /** * Field to sort the list by. Accepts the following values: * `key.reportId` * (default) The default sorting order is ascending. To specify descending * order for a field, add the suffix `desc` to the field name. For example, * `key.reportId desc`. */ orderBy?: string; /** * Maximum number of results per page. Must be between `1` and `100`. * Defaults to `100` if unspecified. */ pageSize?: number; /** * A token identifying which page of results the server should return. * Typically, this is the value of nextPageToken returned from the previous * call to the `queries.reports.list` method. If unspecified, the first page * of results is returned. */ pageToken?: string; } /** * Additional options for DoubleClickBidManager#queriesRun. */ export interface QueriesRunOptions { /** * Whether the query should be run synchronously. When `true`, the request * won't return until the resulting report has finished running. This * parameter is `false` by default. Setting this parameter to `true` is **not * recommended**. */ synchronous?: boolean; } /** * A single query used to generate a report. */ export interface Query { /** * The metadata of the query. */ metadata?: QueryMetadata; /** * The parameters of the report generated by the query. */ params?: Parameters; /** * Output only. The unique ID of the query. */ readonly queryId?: bigint; /** * When and how often the query is scheduled to run. If the frequency field * is set to `ONE_TIME`, the query will only run when queries.run is called. */ schedule?: QuerySchedule; } /** * The metadata of the query. */ export interface QueryMetadata { /** * The date range the report generated by the query will report on. This date * range will be defined by the time zone as used by the advertiser. */ dataRange?: DataRange; /** * The format of the report generated by the query. */ format?: | "FORMAT_UNSPECIFIED" | "CSV" | "XLSX"; /** * Whether an email notification is sent to the query creator when a report * generated by the query is ready. This value is `false` by default. */ sendNotification?: boolean; /** * List of additional email addresses with which to share the query. If * send_notification is `true`, these email addresses will receive a * notification when a report generated by the query is ready. If these email * addresses are connected to Display & Video 360 users, the query will be * available to them in the Display & Video 360 interface. */ shareEmailAddress?: string[]; /** * The display name of the query. This value will be used in the file name of * reports generated by the query. */ title?: string; } /** * Settings on when and how frequently to run a query. */ export interface QuerySchedule { /** * The date on which to end the scheduled runs. This field is required if * frequency is not set to `ONE_TIME`. Otherwise, it will be ignored. */ endDate?: Date; /** * How frequently to run the query. If set to `ONE_TIME`, the query will only * be run when queries.run is called. */ frequency?: | "FREQUENCY_UNSPECIFIED" | "ONE_TIME" | "DAILY" | "WEEKLY" | "SEMI_MONTHLY" | "MONTHLY" | "QUARTERLY" | "YEARLY"; /** * The canonical code for the timezone the query schedule is based on. * Scheduled runs are usually conducted in the morning of a given day. * Defaults to `America/New_York`. */ nextRunTimezoneCode?: string; /** * The date on which to begin the scheduled runs. This field is required if * frequency is not set to `ONE_TIME`. Otherwise, it will be ignored. */ startDate?: Date; } /** * A single report generated by its parent report. */ export interface Report { /** * The key information identifying the report. */ key?: ReportKey; /** * The metadata of the report. */ metadata?: ReportMetadata; /** * The parameters of the report. */ params?: Parameters; } /** * Identifying information of a report. */ export interface ReportKey { /** * Output only. The unique ID of the query that generated the report. */ readonly queryId?: bigint; /** * Output only. The unique ID of the report. */ readonly reportId?: bigint; } /** * The metadata of a report. */ export interface ReportMetadata { /** * Output only. The location of the generated report file in Google Cloud * Storage. This field will be absent if status.state is not `DONE`. */ readonly googleCloudStoragePath?: string; /** * The end date of the report data date range. */ reportDataEndDate?: Date; /** * The start date of the report data date range. */ reportDataStartDate?: Date; /** * The status of the report. */ status?: ReportStatus; } /** * The status of a report. */ export interface ReportStatus { /** * Output only. The timestamp of when report generation finished successfully * or in failure. This field will not be set unless state is `DONE` or * `FAILED`. */ readonly finishTime?: Date; /** * The format of the generated report file. */ format?: | "FORMAT_UNSPECIFIED" | "CSV" | "XLSX"; /** * Output only. The state of the report generation. */ readonly state?: | "STATE_UNSPECIFIED" | "QUEUED" | "RUNNING" | "DONE" | "FAILED"; } /** * Details specifying how to run a query. */ export interface RunQueryRequest { /** * The date range used by the query to generate the report. If unspecified, * the query's original data_range is used. */ dataRange?: DataRange; }