UNPKG

@dmgt/google-ad-manager-api

Version:
97 lines 3.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runAndDownloadReport = runAndDownloadReport; exports.ensureCorrectOrderOfReportQueryParameters = ensureCorrectOrderOfReportQueryParameters; exports.runReport = runReport; exports.waitForReportToFinish = waitForReportToFinish; exports.streamReportResult = streamReportResult; const tslib_1 = require("tslib"); const node_https_1 = tslib_1.__importDefault(require("node:https")); const promises_1 = require("node:timers/promises"); const Object_1 = require("../lang/Object"); /** * @returns An incoming stream of a GZIP'd result * @example * ``` * runAndDownloadReport(api, { * exportFormat: 'CSV_DUMP', * query: { * dimensions: [ * 'DATE', * 'COUNTRY_NAME', * 'DEVICE_CATEGORY_NAME', * 'AD_UNIT_ID', * 'AD_UNIT_NAME', * ], * columns: [ * 'AD_UNIT_CODE', * 'TOTAL_LINE_ITEM_LEVEL_IMPRESSIONS', * 'TOTAL_ACTIVE_VIEW_MEASURABLE_IMPRESSIONS', * 'AD_SERVER_ACTIVE_VIEW_VIEWABLE_IMPRESSIONS', * 'TOTAL_LINE_ITEM_LEVEL_ALL_REVENUE', * ], * startDate: { year: '2024', month: '4', day: '30' }, * endDate: { year: '2024', month: '4', day: '30' }, * dateRangeType: 'TODAY', * adUnitView: 'HIERARCHICAL', * }, * }).then(response => { * response.pipe( * createWriteStream('result.csv.gz', { * autoClose: true, * })) * ) * }) * ``` */ async function runAndDownloadReport(api, { exportFormat, query, statusCheckInterval = 2_000, }) { const client = await api.createReportServiceClient(); const jobId = await runReport(client, query); await waitForReportToFinish(client, jobId, statusCheckInterval); return streamReportResult(client, jobId, exportFormat); } /** * GAM's report api will fail unless the order is: * 1. dimensions * 2. [adUnitView] * ... */ function ensureCorrectOrderOfReportQueryParameters(query) { return (0, Object_1.prioritiseKeys)(query, ['dimensions', 'adUnitView']); } async function runReport(client, query) { const [reportJob] = await client.runReportJobAsync({ reportJob: { reportQuery: ensureCorrectOrderOfReportQueryParameters(query), }, }); if (!reportJob.rval?.id) throw new Error('The job never ran.'); return reportJob.rval.id; } async function waitForReportToFinish(client, jobId, statusCheckInterval) { let status = 'IN_PROGRESS'; while (status === 'IN_PROGRESS') { const [result, rawResponse] = await client.getReportJobStatusAsync({ reportJobId: jobId, }); if (!result.rval) throw new Error('No status received'); status = result.rval; if (status === 'FAILED') throw new Error(`Report failed.\n\n${rawResponse}`); await (0, promises_1.setTimeout)(statusCheckInterval); } } async function streamReportResult(client, jobId, exportFormat) { const [urlResult] = await client.getReportDownloadURLAsync({ reportJobId: jobId, exportFormat, }); if (!urlResult.rval) throw new Error('GAM did not provide a download url'); return new Promise((resolve) => { node_https_1.default.get(urlResult.rval, resolve); }); } //# sourceMappingURL=runReport.js.map