-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoogleAdsScript 1.01
180 lines (128 loc) · 5.11 KB
/
GoogleAdsScript 1.01
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
// CPC Watchdog 1.01
// Blaise & Bruno 01-2021
var SPREADSHEET_URL = "COPY_PASTE_YOUR_G_SHEET_URL" ;
var CAMPAIGNLABEL= ""; // monitor campaigns only with these labels
var CPC_THRESHOLD = 2.00; // CPC limit
function main() {
var ss = SpreadsheetApp.openByUrl(SPREADSHEET_URL);
var sheet = ss.getActiveSheet();
var header = ["Date",
"Campaign Name",
"AdGroup Name",
"Keyword ",
"AverageCpc",
"Clicks"
];
// campaigns to check
var campaignSelector = AdWordsApp.campaigns()
.withCondition("Status = ENABLED");
if (CAMPAIGNLABEL!="") {
campaignSelector=campaignSelector
.withCondition("LabelNames CONTAINS_ANY ['"+CAMPAIGNLABEL+"']");
}
var campaignIds=[];
var campaignIterator = campaignSelector.get();
while (campaignIterator.hasNext()) {
var campaign = campaignIterator.next();
var campaignId = campaign.getId();
campaignIds.push(campaignId) ;
}
Logger.log(" # of campaign to check: "+campaignIds.length);
var single = [];
if (campaignIds.length > 0) {
single = singleCPCRetrieve(campaignIds); // Get all clicks with CPCs >threshold for TODAY
}
Logger.log("Total # of Single: "+single.length);
if (single.length > 0) {
var previous = ss.getSheetByName("LastFetch");
previous.activate();
var lastPrevious = previous.getLastRow();
// Clear sheet content
var maxPrevious = previous.getMaxRows().toString();
previous.getRange('A2:F'+ maxPrevious).clearContent();
// Populate "Last Fetch" with values from Today
var previousRange = previous.getRange(2, 1, single.length, header.length);
previousRange.setValues(single);
extractSingleCPCs(previous, sheet);
removeDuplicates(ss);
sheet.sort(5, false); // sort by CPC desc
updatePrevious(ss);
}
}
function removeDuplicates(ss) {
var sheet = ss.getSheetByName("CPC Watchdog");
sheet.activate();
var data = sheet.getDataRange().getValues();
var newData = [];
for (var i in data) {
var row = data[i];
var duplicate = false;
for (var j in newData) {
if (row.join() == newData[j].join()) {
duplicate = true;
}
}
if (!duplicate) {
newData.push(row);
}
}
var length=sheet.getMaxRows().toString();
sheet.getRange('A1:F'+ length).clearContent();
sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
}
function singleCPCRetrieve(campaignIds) {
var cpcThreshold = CPC_THRESHOLD * 1000000;
var singleRow = [];
var awq="SELECT Date, CampaignName, AdGroupName, Criteria, Id, AverageCpc, Clicks FROM KEYWORDS_PERFORMANCE_REPORT WHERE CampaignId IN ["+campaignIds.join(",")+"] AND AverageCpc > "+cpcThreshold+" DURING TODAY" ;
var rows=AdWordsApp.report(awq, {
includeZeroImpressions: true,
returnMoneyInMicros: false,
}).rows() ;
Logger.log("SQL checkCPC: " +awq);
while (rows.hasNext()) {
var row = rows.next();
var day = row['Date'].toString().trim();
var campaignName = row['CampaignName'].toString().trim();
var adGroupName = row['AdGroupName'].toString().trim();
var keyword = row['Criteria'].toString().trim();
var AverageCPC=row['AverageCpc'].toString().replace(".", ",") ; // Format cell to French comma separator
var clicks=row['Clicks'].toString().trim() ;
singleRow.push([day,campaignName, adGroupName, keyword, AverageCPC, clicks]) ;
}
Logger.log("Single row: "+ singleRow);
return singleRow;
}
function extractSingleCPCs(sheetPrevious, sheetCurrent) {
var lastRow = sheetPrevious.getLastRow();
var count = sheetCurrent.getLastRow() + 1; // +1
for (var i=1; i < lastRow ; i++) {
var newData = [];
for (var j=0; j<8 ; j++) {
newData[j] = sheetPrevious.getRange(i, j+1).getValue();
}
var newRow = [];
if (newData[7] > CPC_THRESHOLD) { // If new Single CPC value
newData[4] = newData[7] ; // Transfert it to main sheet
Logger.log("SINGLE CPC from " + newData[5] + " click(s) | " + newData[3] + " | " + newData[4]);
newData[5] = "1"; // As single click
newData[6] = ""; newData[7] = ""; // Clear remaining columns
newRow.push(newData);
sheetCurrent.getRange(count, 1, 1, 8).setValues(newRow);
count++ ;
}
}
}
function updatePrevious(ss) {
var sourceSheet = ss.getSheetByName("LastFetch");
sourceSheet.activate();
var previousSheet = ss.getSheetByName("Previous");
previousSheet.activate();
var lengthSource = sourceSheet.getMaxRows().toString();
var source_range = sourceSheet.getRange('A1:F'+ lengthSource)
var lengthPrevious=previousSheet.getMaxRows().toString();
previousSheet.getRange('A1:F' + lengthSource).clearContent();
var target_range = previousSheet.getRange("A1:F"+lengthSource );
var values = source_range.getValues();
target_range.setValues(values);
previousSheet.sort(5, false); // sort by CPC desc
}