-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlocation-bid-adjustments.js
280 lines (219 loc) · 9.84 KB
/
location-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
// Version: 2.3.2
// Latest Source: https://github.com/Czarto/Adwords-Scripts/blob/master/device-bid-adjustments.js
//
// This Google Ads Script will incrementally change location bid adjustments
// based on conversion Value Per Click (VPC) using the Campaign's average VPC
// as a baseline.
//
/***********
MIT License
Copyright (c) 2016-2022 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 LABEL_PROCESSING = "_processing_location";
var LABEL_IGNORE = '';
var BID_INCREMENT = 0.05; // Value by which to adjust bids
var MIN_CONVERSIONS = 25; // Minimum conversions needed to adjust bids.
var HIGH_COST = 500; // or Adjust bids anyway if cost is above HIGH_COST
var MAX_BID_ADJUSTMENT = 2.00; // Do not increase adjustments above this value
var MIN_BID_ADJUSTMENT = 0.10; // Do not decrease adjustments below this value
// TODO: Instead of "high cost" use a "high CPA". If Conversions < Min Conversions, but CPA is high, then adjust. Otherwise leave alone.
function main() {
initLabels(); // Create Labels
Logger.log('Set Location Bids: 30 days');
setLocationBids("LAST_30_DAYS");
Logger.log('Set Location Bids: 90 days');
setLocationBids(LAST_90_DAYS(), TODAY());
Logger.log('Set Location Bids: Past Year');
setLocationBids(LAST_YEAR(), TODAY());
//Logger.log('Set Location Bids: All Time');
//setLocationBids("ALL_TIME");
cleanup(); // Remove Labels
}
//
// Set the Processing label
// This keeps track of which bid adjustments have already been processed
// in the case where multiple time-lookback windows are being used
//
function initLabels() {
checkLabelExists();
cleanup();
Logger.log('Initializing labels...');
var itemsToLabel = [AdsApp.campaigns(), AdsApp.shoppingCampaigns()];
for (i = 0; i < itemsToLabel.length; i++) {
var iterator = itemsToLabel[i].withCondition("Status = ENABLED").get();
while (iterator.hasNext()) {
iterator.next().applyLabel(LABEL_PROCESSING);
}
}
}
//
// Create the processing label if it does not exist
//
function checkLabelExists() {
var labelIterator = AdsApp.labels().withCondition("Name = '" + LABEL_PROCESSING + "'" ).get();
if( !labelIterator.hasNext()) {
AdsApp.createLabel(LABEL_PROCESSING, "AdWords Scripts label used to process bids")
}
}
//
// Remove Processing label
//
function cleanup() {
Logger.log('Cleaning up...');
var cleanupList = [AdsApp.campaigns(), AdsApp.shoppingCampaigns()];
for (i = 0; i < cleanupList.length; i++) {
var iterator = cleanupList[i].withCondition("LabelNames CONTAINS_ANY ['" + LABEL_PROCESSING + "']").get();
while (iterator.hasNext()) {
iterator.next().removeLabel(LABEL_PROCESSING);
}
}
}
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) {
// TODO: Just do one loop, with the campaign performance report.
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignId = campaign.getId();
// Get click and revenue data for the entire campaign
var report = AdsApp.report(
"SELECT CampaignId, CampaignName, Clicks, ConversionValue " +
"FROM CAMPAIGN_PERFORMANCE_REPORT " +
"WHERE CampaignId = " + campaignId + " " +
" AND CampaignStatus = 'ENABLED' " +
"DURING " + dateRangeToString(dateRange, dateRangeEnd));
var row = report.rows().next();
var campaignId = row['CampaignId'];
var campaignName = row['CampaignName'];
var campaignClicks = row['Clicks'];
var campaignRevenue = row['ConversionValue'].replace(',','');
var campaignRevenuePerClick = (campaignClicks == 0 ? 0 : campaignRevenue/campaignClicks);
// Get click and revenue data for each geo location
var report = AdsApp.report(
"SELECT Id, Clicks, Conversions, ConversionValue, Cost, BidModifier " +
"FROM CAMPAIGN_LOCATION_TARGET_REPORT " +
"WHERE Id > 0 AND CampaignId = " + campaignId + " " +
"DURING " + dateRangeToString(dateRange, dateRangeEnd));
var reportRows = report.rows();
while(reportRows.hasNext()) {
var row = reportRows.next();
var locationId = [[campaignId, row["Id"]]];
var locationClicks = row['Clicks'];
var locationConverions = row['Conversions'];
var locationRevenue = row['ConversionValue'].replace(',','');
var locationCost = row['Cost'].replace(',','');
var locationBidModifier = (parseFloat(row['BidModifier']) / 100.0) + 1;
var locationRevenuePerClick = (locationClicks == 0 ? 0 : locationRevenue/locationClicks);
if (locationConverions >= MIN_CONVERSIONS || locationCost >= HIGH_COST ) {
var newBidModifier = (locationRevenuePerClick / campaignRevenuePerClick)
var isIncreaseNeeded = (newBidModifier > locationBidModifier && locationBidModifier < MAX_BID_ADJUSTMENT);
var isDecreaseNeeded = (newBidModifier < locationBidModifier && locationBidModifier > MIN_BID_ADJUSTMENT);
if( isIncreaseNeeded || isDecreaseNeeded ) {
var locationIterator = campaign.targeting().targetedLocations().withIds(locationId).get()
if( locationIterator.hasNext()) {
var location = locationIterator.next();
if( isIncreaseNeeded ) {
newBidModifier = Math.min(locationBidModifier + BID_INCREMENT, MAX_BID_ADJUSTMENT)
location.setBidModifier(newBidModifier);
} else if( isDecreaseNeeded ) {
newBidModifier = Math.max(locationBidModifier - BID_INCREMENT, MIN_BID_ADJUSTMENT);
location.setBidModifier(newBidModifier);
}
}
}
}
}
}
}
//
// Returns the CampaignIterator object
//
function getCampaignSelector(dateRange, dateRangeEnd, isShopping) {
var campaignSelector = isShopping ? AdWordsApp.shoppingCampaigns() : AdWordsApp.campaigns();
campaignSelector = campaignSelector
.forDateRange(dateRange, dateRangeEnd)
.withCondition("Status = ENABLED")
.withCondition("LabelNames CONTAINS_ANY ['" + LABEL_PROCESSING + "']");
if (LABEL_IGNORE.length > 0) {
campaignSelector = campaignSelector
.withCondition("LabelNames CONTAINS_NONE ['" + LABEL_IGNORE + "']");
}
return campaignSelector;
}
//
// Date range helper function
// Returns today's date
//
function TODAY() {
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; // 0-11
var yyyy = today.getFullYear();
return { year: yyyy, month: mm, day: dd };
}
//
// Date range helper functions
// Returns date 90 days ago
//
function LAST_90_DAYS() {
var date = new Date();
date.setDate(date.getDate() - 90);
var dd = date.getDate();
var mm = date.getMonth()+1; // 0-11
var yyyy = date.getFullYear();
return {year: yyyy, month: mm, day: dd};
}
//
// Date range helper functions
// Returns date 1 year ago
//
function LAST_YEAR() {
var today = TODAY();
today.year = today.year - 1;
return today;
}
//
// Date range helper function - Reports
// Returns a date range that will work in the DURING clause of the reporting query langugae
//
function dateRangeToString(dateRange, dateRangeEnd) {
if( dateRange == "LAST_7_DAYS" || dateRange == "LAST_14_DAYS" || dateRange == "LAST_30_DAYS" ) {
return dateRange;
} else if (dateRange == "ALL_TIME" ) {
return "20000101," + TODAY().year.toString() + ("0" + TODAY().month).slice(-2) + ("0" + TODAY().day).slice(-2);
} else {
return dateRange.year.toString() + ("0" + dateRange.month).slice(-2) + ("0" + dateRange.day).slice(-2) + ","
+ dateRangeEnd.year.toString() + ("0" + dateRangeEnd.month).slice(-2) + ("0" + dateRangeEnd.day).slice(-2);
}
}