-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtargeting-bid-adjustments.js
377 lines (274 loc) · 12.4 KB
/
targeting-bid-adjustments.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Version: Kunta
// DEPRECATED
// Please use "device-bid-adjustments" and "location-bid-adjustments" scripts instead
/***********
MIT License
Copyright (c) 2016-2019 Alex Czartoryski
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**********/
var BID_INCREMENT = 0.05;
var DEBUG = false;
var TAG_IGNORE = '';
var LOCATION_IGNORE_COUNTRY = true; // Ignore location bid adjustments for Countries
var LOCATION_IGNORE_STATE = false; // Ignore location bid adjustments for States or Provinces
var MIN_CONVERSIONS = 10; // Set this to 1 to increase bids more aggressively
var MIN_CONVERSIONS = 5; // Set this to 1 to decrease bids more aggressively
var HIGH_COST = 40; // How much is too much
var STOPLIMIT_POSITION = 1.3; // Do not increase bids at this position or better
var MAX_BID_ADJUSTMENT = 1.50; // Do not increase adjustments above +50%
function main() {
setLocationBids(LAST_YEAR(), TODAY());
setAdScheduleBids(LAST_YEAR(), TODAY());
setLocationBids("LAST_30_DAYS");
setAdScheduleBids("LAST_30_DAYS");
setLocationBids("LAST_14_DAYS");
setLocationBids("LAST_7_DAYS");
}
function setLocationBids(dateRange, dateRangeEnd) {
// Adjust for normal campaigns
var campaignIterator = getCampaignSelector(dateRange, dateRangeEnd).get();
Logger.log(' ')
Logger.log('### ADJUST LOCATION TARGETING BIDS ###');
Logger.log('Non-Shopping Campaigns');
Logger.log('Total Campaigns found : ' + campaignIterator.totalNumEntities());
setLocationBidsForCampaigns(campaignIterator, dateRange, dateRangeEnd);
// Adjust for Shopping campaigns
var campaignIterator = getCampaignSelector(dateRange, dateRangeEnd, true).get();
Logger.log(' ')
Logger.log('Shopping Campaigns');
Logger.log('Total Campaigns found : ' + campaignIterator.totalNumEntities());
setLocationBidsForCampaigns(campaignIterator, dateRange, dateRangeEnd);
}
//
// Sets the location bids for all the campaigns within the CampaignIterator.
//
function setLocationBidsForCampaigns(campaignIterator, dateRange, dateRangeEnd) {
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignConvRate = campaign.getStatsFor(dateRange, dateRangeEnd).getConversionRate();
Logger.log('-- CAMPAIGN: ' + campaign.getName());
var iterator = campaign.targeting().targetedLocations().get();
Logger.log('----- Locations found : ' + iterator.totalNumEntities());
while (iterator.hasNext()) {
var targetedLocation = iterator.next();
Logger.log('----- ' + targetedLocation.getTargetType() + ':' + getName(targetedLocation));
if (!(LOCATION_IGNORE_COUNTRY && targetedLocation.getTargetType() == "Country") &&
!(LOCATION_IGNORE_STATE && (targetedLocation.getTargetType() == "State" || targetedLocation.getTargetType() == "Province"))) {
var stats = targetedLocation.getStatsFor(dateRange, dateRangeEnd);
var conversions = stats.getConversions();
var cost = stats.getCost();
var currentBidModifier = targetedLocation.getBidModifier();
// At least 1 conversion
if (conversions > 0) {
Logger.log(' ^ Convervions > 0');
if (isBidIncreaseNeeded(stats, currentBidModifier, campaignConvRate)) {
increaseBid(targetedLocation);
} else if (isBidDecreaseNeeded(stats, currentBidModifier, campaignConvRate)) {
decreaseBid(targetedLocation);
}
}
// Zero Conversions, Hight Cost. Drop bids.
if (conversions == 0 && cost > HIGH_COST) {
Logger.log(' High Cost');
decreaseBid(targetedLocation);
}
} else {
var message = '----- ^ Ignoring ';
if (LOCATION_IGNORE_COUNTRY && targetedLocation.getTargetType() == "Country") {
message = message + 'Countries';
} else if (LOCATION_IGNORE_STATE && (targetedLocation.getTargetType() == "State" || targetedLocation.getTargetType() == "Province")) {
message = message + 'States and Provinces';
}
Logger.log(message);
}
}
}
}
function setAdScheduleBids(dateRange, dateRangeEnd) {
var campaignIterator = getCampaignSelector(dateRange, dateRangeEnd).get();
Logger.log(' ')
Logger.log('### ADJUST AD SCHEDULE TARGETING BIDS ###');
Logger.log('Total Campaigns found : ' + campaignIterator.totalNumEntities());
setAdScheduleBidsForCampaigns(campaignIterator, dateRange, dateRangeEnd);
// Adjust for Shopping campaigns
var campaignIterator = getCampaignSelector(dateRange, dateRangeEnd, true).get();
Logger.log(' ')
Logger.log('Shopping Campaigns');
Logger.log('Total Campaigns found : ' + campaignIterator.totalNumEntities());
setAdScheduleBidsForCampaigns(campaignIterator, dateRange, dateRangeEnd);
}
/*
** Set schedule bid adjustments for all campaigns within the campaign iterator
*/
function setAdScheduleBidsForCampaigns(campaignIterator, dateRange, dateRangeEnd) {
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignConvRate = campaign.getStatsFor(dateRange, dateRangeEnd).getConversionRate();
Logger.log('-- CAMPAIGN: ' + campaign.getName());
var iterator = campaign.targeting().adSchedules().get();
Logger.log('----- Schedules found : ' + iterator.totalNumEntities());
while (iterator.hasNext()) {
var adSchedule = iterator.next();
Logger.log('----- ' + getName(adSchedule));
var stats = adSchedule.getStatsFor(dateRange, dateRangeEnd);
var conversions = stats.getConversions();
var cost = stats.getCost();
var currentBidModifier = adSchedule.getBidModifier();
if (conversions > 0) {
Logger.log(' ^ Convervions > 0');
if (isBidIncreaseNeeded(stats, currentBidModifier, campaignConvRate)) {
increaseBid(adSchedule)
} else if (isBidDecreaseNeeded(stats, currentBidModifier, campaignConvRate)) {
decreaseBid(adSchedule);
}
}
// Zero Conversions, Hight Cost. Drop bids.
if (conversions == 0 && cost > HIGH_COST) {
Logger.log(' High Cost');
decreaseBid(adSchedule);
}
}
}
}
//
// Returns true if a bid increase is needed, false otherwise
//
function isBidIncreaseNeeded(stats, currentBid, baselineConversionRate) {
var conversions = stats.getConversions();
var conversionRate = stats.getConversionRate();
var position = stats.getAveragePosition();
var targetBid = (conversionRate / baselineConversionRate)
if (isBidChangeSignificant(currentBid, targetBid)) {
var isIncreaseNeeded = (targetBid > currentBid
&& (position > STOPLIMIT_POSITION || position == 0)
&& currentBid < MAX_BID_ADJUSTMENT
&& conversions >= MIN_CONVERSIONS);
if (DEBUG) {
Logger.log(' ^ Is increase needed? ' + isIncreaseNeeded
+ ':: targetBid:' + targetBid + ' currentBid:' + currentBid
+ ':: conversionRate:' + conversionRate + ' baseline:' + baselineConversionRate
+ ':: position:' + position + ' stoplimit:' + STOPLIMIT_POSITION
+ ':: currentBid:' + currentBid + ' stoplimit:' + MAX_BID_ADJUSTMENT
+ ':: conversions:' + conversions + ' threshold:' + MIN_CONVERSIONS);
}
return (isIncreaseNeeded);
} else {
return false;
}
}
//
// Returns true if a bid decrease is needed, false otherwise
//
function isBidDecreaseNeeded(stats, currentBid, baselineConversionRate) {
var conversions = stats.getConversions();
var conversionRate = stats.getConversionRate();
var targetBid = (conversionRate / baselineConversionRate)
if (isBidChangeSignificant(currentBid, targetBid)) {
var isDecreaseNeeded = (targetBid < currentBid && conversions >= MIN_CONVERSIONS);
if (DEBUG) {
Logger.log(' ^ Is decrease needed? ' + isDecreaseNeeded
+ ':: targetBid:' + targetBid + ' currentBid:' + currentBid
+ ':: conversionRate:' + conversionRate + ' baseline:' + baselineConversionRate
+ ':: conversions:' + conversions + ' threshold:' + MIN_CONVERSIONS);
}
return (isDecreaseNeeded);
} else {
return false;
}
}
//
// returns true if the difference between the two bids is >= BID_INCREMENT
//
function isBidChangeSignificant(bid1, bid2) {
var isSignificant = (Math.abs(bid1 - bid2) >= BID_INCREMENT);
if (DEBUG) {
Logger.log(' ^ Is bid change significant? BID1:' + bid1 + ' BID2:' + bid2 + ' :: ' + isSignificant);
}
return (isSignificant)
}
//
// Increase bid adjustments by the default amount
//
function increaseBid(target) {
var newBidModifier = target.getBidModifier() + BID_INCREMENT;
target.setBidModifier(newBidModifier);
if (DEBUG) {
Logger.log('*** UPDATE *** ' + target.getEntityType() + ' : ' + getName(target)
+ ', bid modifier: ' + newBidModifier
+ ' increase bids');
}
}
//
// Decrease bid adjustments by the default amount
//
function decreaseBid(target) {
var newBidModifier = target.getBidModifier() - BID_INCREMENT;
newBidModifier = Math.max(newBidModifier, 0.1); // Modifier cannot be less than 0.1 (-90%)
// TODO: Reset bid modifier to 0% (1.0) if the current conversion rate is below avg conversion rate
// var newBidModifier = Math.min(currentBidModifier - BID_INCREMENT, 1);
target.setBidModifier(newBidModifier);
if (DEBUG) {
Logger.log('*** UPDATE *** ' + target.getEntityType() + ' : ' + getName(target)
+ ', bid modifier: ' + newBidModifier
+ ' decrease bids');
}
}
//
// Returns the CampaignIterator object
//
function getCampaignSelector(dateRange, dateRangeEnd, isShopping) {
var campaignSelector = isShopping ? AdWordsApp.shoppingCampaigns() : AdWordsApp.campaigns();
campaignSelector = campaignSelector
.forDateRange(dateRange, dateRangeEnd)
.withCondition("Status = ENABLED");
if (TAG_IGNORE.length > 0) {
campaignSelector = campaignSelector
.withCondition("LabelNames CONTAINS_NONE ['" + TAG_IGNORE + "']");
}
return campaignSelector;
}
/*
** Helper function for log formatting
*/
function getName(object) {
if (object.getEntityType() == 'AdSchedule') {
return formatSchedule(object);
} else {
return object.getName();
}
}
//
// Date formatting for logging
//
function formatSchedule(schedule) {
function zeroPad(number) { return Utilities.formatString('%02d', number); }
return schedule.getDayOfWeek() + ', ' +
schedule.getStartHour() + ':' + zeroPad(schedule.getStartMinute()) +
' to ' + schedule.getEndHour() + ':' + zeroPad(schedule.getEndMinute());
}
function TODAY() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; //January is 0!
var yyyy = today.getFullYear();
return { year: yyyy, month: mm, day: dd };
}
function LAST_YEAR() {
var today = TODAY();
today.year = today.year - 1;
return today;
}