-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAdWordsScriptMMCAndAccountTemplate
152 lines (122 loc) · 6.69 KB
/
AdWordsScriptMMCAndAccountTemplate
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
/**
* This AdWords Script Template is prepared to be executed at Account level and at MCC level as well
* Version 1.1
* Changelog v1.1 - Added parallel/sequential execution option
*
* You can choose how to execute it modifying mccLevelScript variable:
*
* mccLevelScript = true; -> MCC Level execution
* mccLevelScript = false; -> Account level execution
*
* In the case of MCC level execution, you can filter Accounts by label with mccLabelText variable:
*
* mccLabelText = 'no label'; -> NO filtering
* mccLabelText = 'Other text'; -> Filtering by MCC Label that has 'Other text' name
*
* NEW v1.1 You can choose if enable or not parallel execution changing parallelExecution variable as following:
*
* parallelExecution = true; -> Parallel execution ENABLED
* parallelExecution = false; -> Parallel execution DISABLED // This is the by default option
*
* Last version available in: https://github.com/AlbertoEstevesC/AdWords-Scripts/blob/master/AdWordsScriptMMCAndAccountTemplate
*
* SPANISH explanation: https://www.albertoestevescorreia.com/adwords-script-plantilla-scripts-ejecutables-nivel-mcc-cuenta/
*
* @Author: Alberto Esteves Correia
* https://www.albertoestevescorreia.com
**/
// ---------------- GLOBAL VARIABLES ---------------------------
var mccLevelScript = true; // "true" value for MCC level script execution // "false" value for account level script execution
var mccLabelText = 'no label'; // Filter AdWords accounts which have this MCC Account label // 'no label' = NO filtering
var parallelExecution = false; // "true" to enable parallel AdWords Scripts execution // "false" to disable it. This is the by default option
/////////////////////////////////////////////////////////////////////////////
// //
// INSERT YOUR GLOBAL VARIABLES HERE //
// (These variables may be used in any part of the script) //
// //
/////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------------------------------------------------
function accountCodeExecution (){
/////////////////////////////////////////////////////////////////////////////
// //
// INSERT YOUR LOCAL SCRIPT CODE HERE //
// (This code will be executed FOR EACH selected AdWords account) //
// //
/////////////////////////////////////////////////////////////////////////////
return true;
} // End of accountCodeExecution function
/////////////////////////////////////////////////////////////////////////////
// //
// INSERT YOUR AUXILIARY FUNCTIONS HERE //
// (This code will be executed FOR EACH function call) //
// //
/////////////////////////////////////////////////////////////////////////////
// ----------------------------------------------------------------
function getFilteredMccAccounts (){
var accountIterator;
var accountIds = [];
var accountId;
if (mccLabelText != "no label") { // Filter accounts by MCC label
accountIterator = MccApp.accounts().withCondition("LabelNames CONTAINS '" + mccLabelText +"'").orderBy('Name').get();
} else { // Execute script for all MCC accounts
accountIterator = MccApp.accounts().orderBy('Name').get();
}
while (accountIterator.hasNext()) {
accountId = accountIterator.next().getCustomerId();
accountIds.push(accountId);
}
return accountIds;
} // End of getFilteredMccAccounts function
function executeInSequence (sequentialIds, executeSequentiallyFunc) {
//Logger.log('Executing in sequence : ' + sequentialIds);
sequentialIds.forEach(function (accountId) {
//Logger.log("\nACCOUNT ID: " + accountId + "\n");
var account = MccApp.accounts().withIds([accountId]).get().next();
MccApp.select(account);
var res = executeSequentiallyFunc();
});
} // End of executeInSequence function
// ----------------------------------------------------------------
function main () {
try {
/////////////////////////////////////////////////////////////////////////////
// //
// INSERT YOUR GLOBAL SCRIPT INITIAL CODE HERE //
// (This code will be executed once BEFORE selecting any AdWords account) //
// //
/////////////////////////////////////////////////////////////////////////////
if (mccLevelScript){ // MCC Level execution
var accountIds = getFilteredMccAccounts();
var sequentialIds;
if (parallelExecution) {
var parallelIds = accountIds.slice(0, 50);
sequentialIds = accountIds.slice(50);
// execute accross accounts
MccApp.accounts()
.withIds(parallelIds)
.executeInParallel('accountCodeExecution');
} else { // sequential execution only
sequentialIds = accountIds.slice(0);
}
if (accountIds.length > 0) {
executeInSequence(sequentialIds, accountCodeExecution);
}
} else { // Account Level execution
var aux = accountCodeExecution();
}
/////////////////////////////////////////////////////////////////////////////
// //
// INSERT YOUR GLOBAL SCRIPT FINAL CODE HERE //
// (This code will be executed AFTER processing ALL AdWords accounts) //
// //
/////////////////////////////////////////////////////////////////////////////
} catch (exception) {
// Logger.getLog();
/////////////////////////////////////////////////////////////////////////////
// //
// INSERT YOUR ERROR CODE HERE //
// (This code will be executed ONLY in case of SCRIPT EXECUTION ERROR) //
// //
/////////////////////////////////////////////////////////////////////////////
}
} // End of Script