dfp-lib
Version:
This project hosts the Node.JS client library for the SOAP-based DFP API at Google.
49 lines (48 loc) • 2.84 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
const request = require("request");
const src_1 = require("../../../src");
const config_1 = require("../auth/config");
(function () {
return __awaiter(this, void 0, void 0, function* () {
const user = new src_1.dfp.DfpUser(config_1.default);
user.logAll();
const reportService = new src_1.dfp.ReportService(user);
const networkService = new src_1.dfp.NetworkService(user);
const rootAdUnitId = (yield networkService.getCurrentNetwork()).effectiveRootAdUnitId;
const statementBuilder = new src_1.dfp.StatementBuilder()
.where('PARENT_AD_UNIT_ID = :parentAdUnitId')
.withBindVariableValue('parentAdUnitId', rootAdUnitId);
const reportQuery = new src_1.dfp.ReportQuery()
.addDimensions('AD_UNIT_ID', 'AD_UNIT_NAME')
.addColumns('AD_SERVER_IMPRESSIONS', 'AD_SERVER_CLICKS', 'DYNAMIC_ALLOCATION_INVENTORY_LEVEL_IMPRESSIONS', 'DYNAMIC_ALLOCATION_INVENTORY_LEVEL_CLICKS', 'TOTAL_INVENTORY_LEVEL_IMPRESSIONS', 'TOTAL_INVENTORY_LEVEL_CPM_AND_CPC_REVENUE')
.setStatement(statementBuilder.toStatement())
.setAdUnitView('HIERARCHICAL')
.setDateRangeType('YESTERDAY');
let reportJob = new src_1.dfp.ReportJob()
.setReportQuery(reportQuery);
console.log("Running report...");
reportJob = yield reportService.runReportJob(reportJob);
const reportDownloader = new src_1.dfp.ReportDownloader(reportService, reportJob.id);
console.log("Waiting for report to be ready...");
yield reportDownloader.waitForReportReady();
console.log("Report is ready!");
const reportDownloadOptions = new src_1.dfp.ReportDownloadOptions();
reportDownloadOptions.exportFormat = 'CSV_DUMP';
reportDownloadOptions.useGzipCompression = false;
const downloadUrl = yield reportDownloader.getDownloadUrlWithOptions(reportDownloadOptions);
console.log("Downloading report... ");
request.get(downloadUrl, function (error, response, body) {
console.log("Report downloaded:\n");
console.log(body);
console.log("\nDONE");
});
});
})().catch(err => console.error(err));