-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad-copy-length-analysis.js
More file actions
205 lines (172 loc) · 5.16 KB
/
ad-copy-length-analysis.js
File metadata and controls
205 lines (172 loc) · 5.16 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
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
// ID: 1394f371467113cb76547199d4caf78f
/**
* Ad Copy Length Analysis
*
* Download an ad performance report for the account and break up the
* information and aggregate by component lengths. This will create a sheet for
* Headline1, Headline2, Headline3, Description1, Description2, Path1, Path2
* and Headline/Description/Path, which concatenates the respective components.
*
* Google Ads Script
* @author Brainlabs
*/
function main() {
const spreadsheetUrl = "YOUR_SPREADSHEET_URL_HERE";
const dateRange = "LAST_30_DAYS";
const attributes = [
"AdType",
"Headline",
"Description1",
"Description2",
"HeadlinePart1",
"HeadlinePart2",
"ExpandedTextAdHeadlinePart3",
"Description",
"ExpandedTextAdDescription2",
"ExpandedDynamicSearchCreativeDescription2",
"Path1",
"Path2"
];
const metrics = [
"Clicks",
"Impressions",
"Cost",
"Conversions"
];
const calculatedMetrics = {
CPC: function (d) {
return d.Cost / d.Clicks;
},
CTR: function (d) {
return d.Clicks / d.Impressions;
},
CPA: function (d) {
return d.Cost / d.Conversions;
}
};
const validAdTypes = [
"EXPANDED_TEXT_AD",
"TEXT_AD",
"EXPANDED_DYNAMIC_SEARCH_AD",
"DYNAMIC_SEARCH_AD",
];
const query = _buildQuery(attributes.concat(metrics), validAdTypes, dateRange);
const report = AdsApp.report(query, {
includeZeroImpressions: false
});
const ads = [];
const rows = report.rows();
while (rows.hasNext()) {
var row = rows.next();
ads.push(_normalised(row));
}
/**
* The variables affecting performance:
*/
const variables = [
"Headline",
"Headline1",
"Headline2",
"Headline3",
"Description",
"Description1",
"Description2",
"Path",
"Path1",
"Path2"
];
var spreadsheet = SpreadsheetApp.openByUrl(spreadsheetUrl);
const groupSummer = GroupSummer(ads, metrics);
const formatter = Formatter("# chars", ["# ads"].concat(metrics), calculatedMetrics);
const numColumns = 2 + metrics.length + Object.keys(calculatedMetrics).length;
variables.forEach(function (varname) {
const table = formatter(groupSummer(varname));
const sheet = spreadsheet.getSheetByName(varname) || spreadsheet.insertSheet(varname);
const range = sheet.getRange(1, 1, table.length, numColumns);
range.setValues(table);
const maxRows = sheet.getMaxRows();
const maxCols = sheet.getMaxColumns();
// Prune the sheet.
if (maxRows > table.length) {
sheet.deleteRows(table.length + 1, maxRows - table.length);
}
if (maxCols > numColumns) {
sheet.deleteColumns(numColumns + 1, maxCols - numColumns);
}
});
}
/**
* @param {string[]} fields Fields to include in the report
* @param {string[]} validAdTypes Ad types to filter by
* @param {string} dateRange Date range for metrics
*/
function _buildQuery(fields, validAdTypes, dateRange) {
return "SELECT " + fields.join(", ")
+ " FROM AD_PERFORMANCE_REPORT"
+ " WHERE AdType IN [" + validAdTypes.join(", ") + "]"
+ " AND Impressions > 0"
+ " DURING " + dateRange;
}
function Formatter(keyName, metrics, calculations) {
var calculatedMetrics = Object.keys(calculations);
var header = [keyName].concat(metrics, calculatedMetrics);
/**
* Format the aggregated data from GroupSummer into a table as a 2d array.
*/
return function (aggregate) {
// Get the number of characters in increasing order.
var keys = Object.keys(aggregate);
keys.sort(function (i, j) {
return i - j;
});
var rows = keys.map(function (key) {
var data = aggregate[key];
var performanceData = metrics.map(function (m) {
return data[m];
});
var calculatedData = calculatedMetrics.map(function (c) {
return calculations[c](data);
});
return [key].concat(performanceData, calculatedData);
});
return [header].concat(rows);
};
}
function GroupSummer(ads, metrics) {
/**
* Get the sum of all the metrics, grouped by the length of the number
* of characters in `field`.
*/
return function (field) {
function reducer(result, ad) {
const numCharacters = ad[field].length;
const aggregate = result[numCharacters] || {
"# ads": 0
};
aggregate["# ads"] += 1;
metrics.forEach(function (m) {
aggregate[m] = parseFloat(ad[m]) + (aggregate[m] || 0);
});
result[numCharacters] = aggregate;
return result;
}
const result = ads.reduce(reducer, {});
return result;
};
}
/**
* Convert a row from an AD_PERFORMANCE_REPORT into a normalised structure
* for an ad.
*/
function _normalised(row) {
const ad = Object.create(row);
ad.Headline1 = row.HeadlinePart1 || row.Headline;
ad.Headline2 = row.HeadlinePart2 || "";
ad.Headline3 = row.ExpandedTextAdHeadlinePart3 || "";
ad.Description1 = row.Description || row.Description1 || "";
ad.Description2 = row.Description2 || row.ExpandedTextAdDescription2 || row.ExpandedDynamicSearchCreativeDescription2 || "";
ad.Headline = ad.Headline1 + ad.Headline2 + ad.Headline3;
ad.Description = ad.Description1 + ad.Description2;
ad.Path = ad.Path1 + ad.Path2;
return ad;
}