Skip to content

Commit 34d0e2d

Browse files
authored
feat: Create Apps Script examples for new DV360 and DBM services (#555)
* feat: Create Apps Script examples for new DV360 and DBM services * fix: Update region tags in doubleclickbidmanager.gs
1 parent 9066cac commit 34d0e2d

File tree

4 files changed

+314
-0
lines changed

4 files changed

+314
-0
lines changed

advanced/displayvideo.gs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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_dv360_list_partners]
17+
/**
18+
* Logs all of the partners available in the account.
19+
*/
20+
function listPartners() {
21+
// Retrieve the list of available partners
22+
try {
23+
const partners = DisplayVideo.Partners.list();
24+
25+
if (partners.partners) {
26+
// Print out the ID and name of each
27+
for (let i = 0; i < partners.partners.length; i++) {
28+
const partner = partners.partners[i];
29+
console.log('Found partner with ID %s and name "%s".',
30+
partner.partnerId, partner.displayName);
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_dv360_list_partners]
39+
40+
// [START apps_script_dv360_list_active_campaigns]
41+
/**
42+
* Logs names and ID's of all active campaigns.
43+
* Note the use of paging tokens to retrieve the whole list.
44+
*/
45+
function listActiveCampaigns() {
46+
const advertiserId = '1234567'; // Replace with your advertiser ID.
47+
let result;
48+
let pageToken;
49+
try {
50+
do {
51+
result = DisplayVideo.Advertisers.Campaigns.list(advertiserId, {
52+
'filter': 'entityStatus="ENTITY_STATUS_ACTIVE"',
53+
'pageToken': pageToken
54+
});
55+
if (result.campaigns) {
56+
for (let i = 0; i < result.campaigns.length; i++) {
57+
const campaign = result.campaigns[i];
58+
console.log('Found campaign with ID %s and name "%s".',
59+
campaign.campaignId, campaign.displayName);
60+
}
61+
}
62+
pageToken = result.nextPageToken;
63+
} while (pageToken);
64+
} catch (e) {
65+
// TODO (Developer) - Handle exception
66+
console.log('Failed with error: %s', e.error);
67+
}
68+
}
69+
// [END apps_script_dv360_list_active_campaigns]
70+
71+
// [START apps_script_dv360_update_line_item_name]
72+
/**
73+
* Updates the display name of a line item
74+
*/
75+
function updateLineItemName() {
76+
const advertiserId = '1234567'; // Replace with your advertiser ID.
77+
const lineItemId = '123456789'; //Replace with your line item ID.
78+
const updateMask = "displayName";
79+
80+
const lineItemDef = {displayName: 'New Line Item Name (updated from Apps Script!)'};
81+
82+
try {
83+
const lineItem = DisplayVideo.Advertisers.LineItems
84+
.patch(lineItemDef, advertiserId, lineItemId, {updateMask:updateMask});
85+
86+
87+
} catch (e) {
88+
// TODO (Developer) - Handle exception
89+
console.log('Failed with error: %s', e.error);
90+
}
91+
}
92+
// [END apps_script_dv360_update_line_item_name]

advanced/doubleclickbidmanager.gs

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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]

advanced/test_displayvideo.gs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
17+
/**
18+
* Tests listPartners function of displayvideo.gs
19+
*/
20+
function itShouldListPartners() {
21+
console.log('> itShouldListPartners');
22+
listPartners();
23+
}
24+
25+
/**
26+
* Tests listActiveCampaigns function of displayvideo.gs
27+
*/
28+
function itShouldListActiveCampaigns() {
29+
console.log('> itShouldListActiveCampaigns');
30+
listActiveCampaigns();
31+
}
32+
33+
/**
34+
* Tests updateLineItemName function of displayvideo.gs
35+
*/
36+
function itShouldUpdateLineItemName() {
37+
console.log('> itShouldUpdateLineItemName');
38+
updateLineItemName();
39+
}
40+
41+
/**
42+
* Run all tests
43+
*/
44+
function RUN_ALL_TESTS() {
45+
itShouldListPartners();
46+
itShouldListActiveCampaigns();
47+
itShouldUpdateLineItemName();
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
17+
/**
18+
* Tests listQueries function of doubleclickbidmanager.gs
19+
*/
20+
function itShouldListQueries() {
21+
console.log('> itShouldListQueries');
22+
listQueries();
23+
}
24+
25+
/**
26+
* Tests createAndRunQuery function of doubleclickbidmanager.gs
27+
*/
28+
function itShouldCreateAndRunQuery() {
29+
console.log('> itShouldCreateAndRunQuery');
30+
createAndRunQuery();
31+
}
32+
33+
/**
34+
* Tests fetchReport function of doubleclickbidmanager.gs
35+
*/
36+
function itShouldFetchReport() {
37+
console.log('> itShouldFetchReport');
38+
fetchReport();
39+
}
40+
41+
/**
42+
* Run all tests
43+
*/
44+
function RUN_ALL_TESTS() {
45+
itShouldListQueries();
46+
itShouldCreateAndRunQuery();
47+
itShouldFetchReport();
48+
}

0 commit comments

Comments
 (0)