This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHourly_Bid_Optimization.js
195 lines (166 loc) · 7.42 KB
/
Hourly_Bid_Optimization.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
/**
*
* Hourly Bid Optimization
*
* This script will apply ad schedules and set the bid modifier for the schedule
* at each hour according to a multiplier timetable in a Google sheet.
*
* Version: 1.1
* Author: Michael Taggart - @mikeytag
* envoymediagroup.com
*
* Based on original from Daniel Gilbert - @danielgilbert44
* brainlabsdigital.com
*
* NOTE: FOR ALL AFFECTED CAMPAIGNS, THIS SCRIPT WILL DELETE ALL EXISTING AD SCHEDULES
*
*
* The MIT License (MIT)
* Copyright (c) 2015 Envoy Media Group
* 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.
**/
function main() {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//Options
//The google sheet to use
//The default value is the example sheet linked to in the article
var spreadsheet_url = "https://docs.google.com/spreadsheets/d/1Q1p4N8Tm_OMsbtk9JEu0LrFdPFLxdkQe4W-6nx8Eh48/edit?usp=sharing";
var tab_name = 'Sheet1';
//Optional parameters for filtering campaign names.
//Leave blank to use filters. The matching is case insensitive.
var includeCampaignNameContains = ["Lorem" ,"Ipsum"]; //Select which campaigns to include. Leave blank to include all campaigns.
var excludeCampaignNameContains = ["Foo", "Bar"]; //Select which campaigns to exclude. Leave blank to not exclude any campaigns.
//When you want to stop running the ad scheduling for good, set
//the lastRun variable to true to remove all ad schedules.
var lastRun = false;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~//
var timeZone = AdWordsApp.currentAccount().getTimeZone();
var date = new Date();
var dayOfWeek = parseInt(Utilities.formatDate(date, timeZone, "uu")) - 1;
var hour = parseInt(Utilities.formatDate(date, timeZone, "HH"), 10);
//Initialise for use later.
var weekDays = ["MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"];
var dayOfWeekString = weekDays[dayOfWeek];
Logger.log('DAY OF WEEK: '+dayOfWeekString);
Logger.log('HOUR OF DAY: '+hour);
//Retrieving up hourly data
var scheduleRange = "B2:H25";
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheet_url);
var sheet = spreadsheet.getSheetByName(tab_name);
var data = sheet.getRange(scheduleRange).getValues();
var hot_mode_cell = "K2";
var hot_mode_boost = sheet.getRange(hot_mode_cell).getValue();
//convert to coefficient format
Logger.log('HOT MODE BOOST: '+hot_mode_boost);
//This hour's bid multiplier.
var bid_modifier = data[hour][dayOfWeek];
Logger.log('BID MODIFIER: '+bid_modifier);
//convert to coefficient format
var bid_multiplier = 1 + bid_modifier;
Logger.log('BID MULTIPLIER: '+bid_multiplier);
var lastHourCell = "K1";
bid_multiplier = bid_multiplier + hot_mode_boost;
//allowed range is -90% => 300%
if (bid_multiplier < 0.1) {
bid_multiplier = 0.1;
} else if (bid_multiplier > 4) {
bid_multiplier = 4;
}
Logger.log('FINAL BID MULTIPLIER (AFTER APPLYING HOT MODE BOOST): '+bid_multiplier);
sheet.getRange(lastHourCell).setValue(bid_multiplier);
var adScheduleCodes = [];
//Dummy name to exclude
if(excludeCampaignNameContains === ""){
excludeCampaignNameContains += "#@%" + date + "~};";
}
var campaignIds = [];
//Pull a list of all relevant campaign IDs in the account.
var campaignSelector = AdWordsApp.campaigns();
campaignSelector.withCondition('Status = ENABLED');
if (excludeCampaignNameContains.length > 0) {
for (var i in excludeCampaignNameContains) {
//Logger.log(excludeCampaignNameContains[i]);
campaignSelector.withCondition('Name DOES_NOT_CONTAIN_IGNORE_CASE "' + excludeCampaignNameContains[i] + '"');
}
}
var campaignIterator = campaignSelector.get();
var campaignCount = campaignIterator.totalNumEntities();
var campaigns_by_campaign_id = new Array;
var ad_schedules_by_campaign_id = new Array;
while(campaignIterator.hasNext()){
var campaign = campaignIterator.next();
var campaign_id = campaign.getId();
var go = false;
if (includeCampaignNameContains.length > 0) {
for (var i in includeCampaignNameContains) {
var lower_name = campaign.getName().toLowerCase();
if (lower_name.indexOf(includeCampaignNameContains[i].toLowerCase()) != -1) {
go = true;
break;
}
}
} else {
go = true;
}
if (go) {
var ad_schedules = campaign.targeting().adSchedules().get();
campaigns_by_campaign_id[campaign_id] = campaign;
ad_schedules_by_campaign_id[campaign_id] = ad_schedules;
}
}
//Return if there are no campaigns.
if(campaigns_by_campaign_id.length === 0){
Logger.log("NOTICE: There are no campaigns matching your criteria.");
return;
}
var new_schedule = {
dayOfWeek: dayOfWeekString,
startHour: 0,
startMinute: 0,
endHour: 24,
endMinute: 0,
bidModifier: bid_multiplier
};
Logger.log('NEW SCHEDULE: ');
Logger.log(new_schedule);
campaigns_by_campaign_id.forEach(function (campaign,campaign_id,array) {
var adSchedules = ad_schedules_by_campaign_id[campaign_id];
var day_found_flag = false;
while(adSchedules.hasNext()){
var adSchedule = adSchedules.next();
//Logger.log(adSchedule);
//Logger.log(adSchedule.getStartHour());
if (adSchedule.getStartHour() != 0.0 || adSchedule.getStartMinute() != 0.0 || adSchedule.getEndHour() != 24.0 || adSchedule.getEndMinute() != 0.0 || lastRun == true) {
//Logger.log('Removed an ad schedule for campaign ' + campaign_id);
adSchedule.remove();
} else {
if (adSchedule.getDayOfWeek() == dayOfWeekString) {
day_found_flag = true;
//update just the bidModifier
adSchedule.setBidModifier(bid_multiplier);
}
}
}
if (day_found_flag == false && lastRun == false) {
campaign.addAdSchedule(new_schedule);
}
});
}