forked from google/bulkdozer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CampaignManagerDAO.js
278 lines (244 loc) · 8.13 KB
/
CampaignManagerDAO.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***************************************************************************
*
* Copyright 2020 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Note that these code samples being shared are not official Google
* products and are not formally supported.
*
***************************************************************************/
/**
* Object responsible for all interactions with the Campaign Manager API
*
* params:
* profileId: The profile id to be used to access the Campaign Manager API
*/
var CampaignManagerDAO = function(profileId) {
// PRIVATE FIELDS
// 8 seconds default sleep, 4 retries, waiting twice as long for each retry.
// This means that the total wait time in the worst case scenario is 248
// seconds, or just over 4 minutes, ensuring the retries will be exhausted
// within the 5 minutes runtime
const CACHE_EXPIRATION = 21600;
var cache = getCache();
var listCache = {};
var userProperties = PropertiesService.getUserProperties();
var jobId = userProperties.getProperty('jobId');
function getCacheKey(entity, id) {
return entity + '|' + id + '|' + jobId;
}
// PRIVATE METHODS
function getListCacheKey(entity, listName, options) {
var result = entity;
if(options) {
result += JSON.stringify(options);
}
return result;
}
/**
* Fetches items from Campaign Manager based on the provided parameters and it
* handles pagination by fetching all pages. This uses the list method of the
* API
*
* params:
* entity: The name of the Campaign Manager API entity
* listName: Name of the list returned by the API
* options: Additional options to be passed to the list API call
*
* returns: Array with all items that match the specified search
*/
function fetchAll(entity, listName, options) {
var response = _retry(function() {
return DoubleClickCampaigns[entity].list(profileId, options)
}, DEFAULT_RETRIES, DEFAULT_SLEEP);
var result = [];
while (response && response[listName] && response[listName].length > 0) {
result = result.concat(response[listName]);
if (response.nextPageToken) {
// Change due to bug: 276008048
// as long as we provide filters in later API list request along with the pageToken, It works fine in v4.
requestOptions = {'pageToken': response.nextPageToken, ...options};
response = _retry(function() {
return DoubleClickCampaigns[entity].list(profileId, requestOptions);
}, DEFAULT_RETRIES, DEFAULT_SLEEP);
} else {
response = null;
}
}
return result;
}
// PUBLIC METHODS
/**
* Sets the cache object to be used by this instance. This allows for
* controlling which type of cache to use, e.g. in memory or cache service.
*
* params:
* newCache: Object to be used for caching
*/
this.setCache = function(newCache) {
cache = newCache;
}
/**
* Fetches a list of items from CM
*
* params:
* entity: The name of the Campagign Manager API entity
* listName: The name of the list returned by the API
* options: Any additional option that should be passed to the list call
*
* returns: List of items returned from the API
*/
this.list = function(entity, listName, options) {
var result = [];
var cacheKey = getListCacheKey(entity, listName, options);
if (listCache[cacheKey]) {
result = listCache[cacheKey];
} else {
console.log('Invoking API to list ' + entity);
// Check for ids present in the search options
// to create batches if length > 500
if(!options['ids']) {
result = fetchAll(entity, listName, options);
} else {
let batches = createBatches(options['ids']);
// Make API calls in batches to avoid the 500 ids limit error
for(let i = 0; i < batches.length; i++) {
options['ids'] = batches[i];
result = result.concat(fetchAll(entity, listName, options));
}
}
listCache[cacheKey] = result;
for (var i = 0; i < result.length; i++) {
var item = result[i];
if (JSON.stringify(item).length < 100000 && item['id']) {
cache.put(getCacheKey(entity, item['id']), item, CACHE_EXPIRATION);
}
}
}
return result;
}
/**
* Creates batches of 500 ids for the search options query params
*
* params:
* ids: the id list in the search options obj
*/
function createBatches(ids) {
let batches = [];
let tempArray;
let limit = 500;
for (let i = 0, j = ids.length; i < j; i += limit) {
tempArray = ids.slice(i, (i + limit));
batches.push(tempArray);
}
return batches;
}
/**
* Fetches a specific item from Campaign Manager. This method uses cache
*
* params:
* entity: the entity name on the Campaign Manager API
* id: the id of the item to fetch
*/
this.get = function(entity, id) {
if (!id) {
return null;
}
if (cache.get(getCacheKey(entity, id))) {
return JSON.parse(cache.get(getCacheKey(entity, id)));
} else {
console.log('Invoking API to fetch ' + entity);
var result = _retry(function() {
return DoubleClickCampaigns[entity].get(profileId, id);
}, DEFAULT_RETRIES, DEFAULT_SLEEP);
if (result) {
cache.put(getCacheKey(entity, id), result);
}
return result;
}
}
/**
* Inserts or updates item in Campaign Manager
*
* params:
* entity: the name of the Campaign Manager entity
* obj: Object to insert or update
*/
this.update = function(entity, obj) {
console.log('Updating entity ' + entity);
console.log('entity id: ' + obj.id);
if(obj.id) {
return _retry(function() {
return DoubleClickCampaigns[entity].update(obj, profileId);
}, DEFAULT_RETRIES, DEFAULT_SLEEP);
} else {
return _retry(function() {
return DoubleClickCampaigns[entity].insert(obj, profileId);
}, DEFAULT_RETRIES, DEFAULT_SLEEP);
}
}
/**
* Associates a creative to a campaign
*
* params:
* campaignId: The ID of the campaign to associate the creative with
* creativeId: The ID of the creative to associate
*/
this.associateCreativeToCampaign = function(campaignId, creativeId) {
var resource = {
'creativeId': creativeId
}
_retry(function() {
DoubleClickCampaigns.CampaignCreativeAssociations.insert(resource, profileId, campaignId);
}, DEFAULT_RETRIES, DEFAULT_SLEEP);
}
/**
* Fetches a list of Campaign Manager Sizes that match the width and height
* specified.
*
* params:
* width: width of the Size to search
* height: height of the Size to search
*
* returns: List of sizes that match width and height specified.
*/
this.getSize = function(width, height) {
return _retry(function() {
return DoubleClickCampaigns.Sizes.list(profileId, {
'height': height,
'width': width
}).sizes;
}, DEFAULT_RETRIES, DEFAULT_SLEEP);
}
/**
* Fetches items of a given entity based on a list of ids. Since CM has a
* limitation of 500 ids per "list" call, this method splits the ids in chunks
* of up to 500 items and merges the result.
*
* params:
* entity: Name of the CM entity to fetch
* listName: Name of the list field returned by the api
* ids: Array of integers representing the ids to fetch
*
* returns: Array of entities returned by CM
*/
this.chunkFetch = function(entity, listName, ids) {
var result = [];
for(var i = 0; i < ids.length; i += 500) {
var chunk = ids.slice(i, i + 500);
result = result.concat(this.list(entity, listName, {'ids': chunk}));
}
return result;
}
}