|
| 1 | +/** |
| 2 | + * Copyright Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +// [START apps_script_dcbm_list_queries] |
| 17 | +/** |
| 18 | + * Logs all of the queries available in the account. |
| 19 | + */ |
| 20 | +function listQueries() { |
| 21 | + // Retrieve the list of available queries |
| 22 | + try { |
| 23 | + const queries = DoubleClickBidManager.Queries.list(); |
| 24 | + |
| 25 | + if (queries.queries) { |
| 26 | + // Print out the ID and name of each |
| 27 | + for (let i = 0; i < queries.queries.length; i++) { |
| 28 | + const query = queries.queries[i]; |
| 29 | + console.log('Found query with ID %s and name "%s".', |
| 30 | + query.queryId, query.metadata.title); |
| 31 | + } |
| 32 | + } |
| 33 | + } catch (e) { |
| 34 | + // TODO (Developer) - Handle exception |
| 35 | + console.log('Failed with error: %s', e.error); |
| 36 | + } |
| 37 | +} |
| 38 | +// [END apps_script_dcbm_list_queries] |
| 39 | + |
| 40 | +// [START apps_script_dcbm_create_and_run_query] |
| 41 | +/** |
| 42 | + * Create and run a new DBM Query |
| 43 | + */ |
| 44 | +function createAndRunQuery() { |
| 45 | + let result; |
| 46 | + let execution; |
| 47 | + //We leave the default date range blank for the report run to |
| 48 | + //use the value defined during query creation |
| 49 | + let defaultDateRange = {} |
| 50 | + let partnerId = "1234567" //Replace with your Partner ID |
| 51 | + let query = { |
| 52 | + "metadata": { |
| 53 | + "title": "Apps Script Example Report", |
| 54 | + "dataRange": { |
| 55 | + "range": "YEAR_TO_DATE" |
| 56 | + }, |
| 57 | + "format": "CSV" |
| 58 | + }, |
| 59 | + "params": { |
| 60 | + "type": "STANDARD", |
| 61 | + "groupBys": [ |
| 62 | + "FILTER_PARTNER", |
| 63 | + "FILTER_PARTNER_NAME", |
| 64 | + "FILTER_ADVERTISER", |
| 65 | + "FILTER_ADVERTISER_NAME", |
| 66 | + ], |
| 67 | + "filters": [ |
| 68 | + {"type": "FILTER_PARTNER","value": partnerId} |
| 69 | + ], |
| 70 | + "metrics": [ |
| 71 | + "METRIC_IMPRESSIONS" |
| 72 | + ] |
| 73 | + }, |
| 74 | + "schedule": { |
| 75 | + "frequency": "ONE_TIME" |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + try { |
| 80 | + result = DoubleClickBidManager.Queries.create(query); |
| 81 | + if (result.queryId) { |
| 82 | + console.log('Created query with ID %s and name "%s".', |
| 83 | + result.queryId, result.metadata.title); |
| 84 | + execution = DoubleClickBidManager.Queries.run(defaultDateRange, result.queryId); |
| 85 | + if(execution.key){ |
| 86 | + console.log('Created query report with query ID %s and report ID "%s".', |
| 87 | + execution.key.queryId, execution.key.reportId); |
| 88 | + } |
| 89 | + } |
| 90 | + } catch (e) { |
| 91 | + // TODO (Developer) - Handle exception |
| 92 | + console.log(e) |
| 93 | + console.log('Failed with error: %s', e.error); |
| 94 | + } |
| 95 | +} |
| 96 | +// [END apps_script_dcbm_create_and_run_query] |
| 97 | + |
| 98 | +// [START apps_script_dcbm_fetch_report] |
| 99 | +/** |
| 100 | + * Fetches a report file |
| 101 | + */ |
| 102 | +function fetchReport() { |
| 103 | + const queryId = '1234567'; // Replace with your query ID. |
| 104 | + const orderBy = "key.reportId desc"; |
| 105 | + |
| 106 | + try { |
| 107 | + const report = DoubleClickBidManager.Queries.Reports.list(queryId, {orderBy:orderBy}); |
| 108 | + if(report.reports){ |
| 109 | + const firstReport = report.reports[0]; |
| 110 | + if(firstReport.metadata.status.state == 'DONE'){ |
| 111 | + const reportFile = UrlFetchApp.fetch(firstReport.metadata.googleCloudStoragePath) |
| 112 | + console.log("Printing report content to log...") |
| 113 | + console.log(reportFile.getContentText()) |
| 114 | + } |
| 115 | + else{ |
| 116 | + console.log("Report status is %s, and is not available for download", firstReport.metadata.status.state) |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + } catch (e) { |
| 121 | + // TODO (Developer) - Handle exception |
| 122 | + console.log(e) |
| 123 | + console.log('Failed with error: %s', e.error); |
| 124 | + } |
| 125 | +} |
| 126 | +// [END apps_script_dcbm_fetch_report] |
0 commit comments