-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuto_Placement_Exclude_By_Domain.js
More file actions
137 lines (117 loc) · 4.56 KB
/
Auto_Placement_Exclude_By_Domain.js
File metadata and controls
137 lines (117 loc) · 4.56 KB
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
var TLDs = ['.cn' , '.hk','.in','.pk', '.tr','.ru'];
//var TLDs = ['.info','.br'];
var timePeriod = "LAST_30_DAYS";
var reportRows = [];
var exflag = "exclude"
// you need clone a sheet first click the following SPREADSHEET_URL
// click make a copy to clone the template
// paste your SPREADSHEET_URL to replace the following SPREADSHEET_URL
var SPREADSHEET_URL = 'https://docs.google.com/spreadsheets/d/1Exyhk0MopoLCqftIiJuTwQK1z2x9IECXyRwK1BsOSmw/edit#gid=0'
//Logger.log('Using spreadsheet - %s.', SPREADSHEET_URL);
var spreadsheet = validateAndGetSpreadsheet(SPREADSHEET_URL);
var sheet = spreadsheet.getSheets()[0];
// -------------------------------------------------------
function removePlacementByDomain (domain,leg) {
var placementSelector = AdWordsApp.display().placements()
.withCondition("PlacementUrl CONTAINS '" + domain + "'")
.withCondition("CampaignStatus != REMOVED")
.forDateRange(timePeriod)
.withLimit(100);
var placementIterator = placementSelector.get();
while (placementIterator.hasNext()) {
var placement = placementIterator.next();
var placementUrl = placement.getUrl();
//Logger.log(placementUrl);
var s = 0-leg
var p = placementUrl.slice(s);
if (p == domain){
var campaign = placement.getCampaign();
//var adgroup = placement.getAdGroup();
var excludeOperation = campaign.display().newPlacementBuilder().withUrl(placementUrl).exclude();
reportRows.push([placement.getCampaign().getName(),placement.getAdGroup().getName(),placementUrl,exflag]);
Logger.log(" exclude : " + placementUrl)
if (!excludeOperation.isSuccessful()) {
Logger.log("Could not exclude : " + placementUrl);
exflag = "Could not exclude"
reportRows.push([campaign,adgroup,placementUrl,exflag]);
}
}
}
}
function trm(domain){
return domain.trim();
}
function run () {
Logger.log('Using spreadsheet - %s.', SPREADSHEET_URL);
spreadsheet.setSpreadsheetTimeZone(AdWordsApp.currentAccount().getTimeZone());
spreadsheet.getRangeByName('account_id').setValue(
AdWordsApp.currentAccount().getCustomerId());
sheet.getRange(1, 2, 1, 1).setValue('Date');
sheet.getRange(1, 3, 1, 1).setValue(new Date());
//sheet.getRange(7, 1, sheet.getMaxRows() - 7, sheet.getMaxColumns()).clear();
Logger.log('Running on .' + AdWordsApp.currentAccount().getCustomerId());
var lastRow = sheet.getLastRow();
for (var i = 0; i < TLDs.length; i++){
var dom = trm(TLDs[i])
removePlacementByDomain(dom,dom.length)
}
Logger.log('all:'+reportRows.length)
if (reportRows.length > 0) {
sheet.getRange(lastRow+1, 2, 1, 1).setValue(new Date());
sheet.getRange(lastRow+2, 2, reportRows.length, 4).setValues(reportRows);
}
var email = spreadsheet.getRangeByName('email').getValue();
if (email) {
var body = [];
body.push('The Account Placement exclude history :\n');
body.push('Account ID:' +AdWordsApp.currentAccount().getCustomerId());
body.push('Full report at ' + SPREADSHEET_URL + '\n\n');
}
MailApp.sendEmail(email, '' +
reportRows.length + ' The Placement Auto Exclude ' +
AdWordsApp.currentAccount().getName(), body.join('\n'));
}
function executeInSequence (sequentialIds, executeSequentiallyFunc) {
Logger.log('Executing in sequence : ' + sequentialIds);
sequentialIds.forEach(function (accountId) {
var account = MccApp.accounts().withIds([accountId]).get().next();
MccApp.select(account);
executeSequentiallyFunc();
});
}
/*
function main () {
try {
var accountIterator = MccApp.accounts().orderBy('Name').get();
Logger.log('Total number of accounts under MCC : ' + accountIterator.totalNumEntities());
var accountIds = [];
while (accountIterator.hasNext()) {
var account = accountIterator.next();
accountIds.push(account.getCustomerId());
}
var parallelIds = accountIds.slice(0, 50);
var sequentialIds = accountIds.slice(50);
// execute accross accounts
MccApp.accounts()
.withIds(parallelIds)
.executeInParallel('run');
if (sequentialIds.length > 0) {
executeInSequence(sequentialIds, run);
}
} catch (exception) {
// not an Mcc
Logger.log('Running on non-MCC account.');
run();
}
}*/
function main(){
Logger.log('Running on non-MCC account.');
run();
}
function validateAndGetSpreadsheet(spreadsheeturl) {
if (spreadsheeturl == 'YOUR_SPREADSHEET_URL') {
throw new Error('Please specify a valid Spreadsheet URL. You can find' +
' a link to a template in the associated guide for this script.');
}
return SpreadsheetApp.openByUrl(spreadsheeturl);
}