-
Notifications
You must be signed in to change notification settings - Fork 7
/
retrieveData.gs
206 lines (163 loc) · 5.34 KB
/
retrieveData.gs
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
206
// Total page likes is a cumulative number. Get the page likes on the last day of the date range.
function reportPageLikesTotal(report, type) {
var rows = [];
report = report.day;
var lastObject = report[report.length-1];
var row = {};
row[type] = lastObject[lastObject.length-1]['value'];
rows[0] = row;
return rows;
}
function reportMetric(report, type) {
var rows = [];
report = report.day;
console.log(JSON.stringify(report));
//Loop chunks
for (var c = 0; c < report.length; c++) {
var valueRows = report[c];
// Loop report
for (var i = 0; i < valueRows.length; i++) {
var row = {};
row[type] = report[c][i]['value'];
if (type == 'pageNewLikes') {
//Data is reported on day after. Actual date is end_time - 24 hours. Change date format to Month (from YYYY-MM-DD to MM)
row['pageNewLikesMonth'] = new Date(new Date(report[c][i]['end_time']).getTime()-86400000).toISOString().slice(4, 7).replace(/-/g, '');
}
// Assign all data to rows list
rows.push(row);
}
}
return rows;
}
function reportGenderAge(report) {
var rows = [];
report = report.day;
//Define fans per gender (female, male, unknown)
var fans = {};
fans['Female'] = 0;
fans['Male'] = 0;
fans['Unknown'] = 0;
// Define fans per age
fans['13-17'] = 0;
fans['18-24'] = 0;
fans['25-34'] = 0;
fans['35-44'] = 0;
fans['45-54'] = 0;
fans['55-64'] = 0;
fans['65+'] = 0;
// Only report last number of fans per gender/age within date range
// Get gender/age objects
var lastObject = report[report.length-1]
var results = lastObject[lastObject.length-1]['value'];
// Loop all objects
for (var property in results) {
if (results.hasOwnProperty(property)) {
// Assign values to gender
switch (true) {
case (property.indexOf('F') > -1):
fans['Female'] += results[property];
break;
case (property.indexOf('M') > -1):
fans['Male'] += results[property];
break;
case (property.indexOf('U') > -1):
fans['Unknown'] += results[property];
break;
}
// Assign values to age
switch (true) {
case (property.indexOf('13-17') > -1):
fans['13-17'] += results[property];
break;
case (property.indexOf('18-24') > -1):
fans['18-24'] += results[property];
break;
case (property.indexOf('25-34') > -1):
fans['25-34'] += results[property];
break;
case (property.indexOf('35-44') > -1):
fans['35-44'] += results[property];
break;
case (property.indexOf('45-54') > -1):
fans['45-54'] += results[property];
break;
case (property.indexOf('55-64') > -1):
fans['55-64'] += results[property];
break;
case (property.indexOf('65+') > -1):
fans['65+'] += results[property];
break;
}
}
}
for (var property in fans) {
var row = {};
if (fans.hasOwnProperty(property)) {
if (property.indexOf('Female') > -1 || property.indexOf('Male') > -1 || property.indexOf('Unknown') > -1) {
row['pageLikesGender'] = property;
row['pageLikesGenderNumber'] = fans[property];
} else {
row['pageLikesAge'] = property;
row['pageLikesAgeNumber'] = fans[property];
}
}
rows.push(row);
}
return rows;
}
//Report language of page fnas
function reportPageLikesLocale(report, type) {
var rows = [];
report = report.day;
var lastObject = report[report.length-1]
var results = lastObject[lastObject.length-1]['value'];
for (var property in results) {
var row = {};
row[type] = property;
row['pageAudienceLanguageLikes'] = results[property];
rows.push(row);
}
return rows;
}
//Report page likes by source
function reportPageLikesSource(report, type) {
var rows = [];
report = report.day;
var lastObject = report[report.length-1];
for(var i = 0; i < lastObject.length; i++) {
var results = lastObject[i]['value'];
for (var property in results) {
if (results.hasOwnProperty(property)) {
var row = {};
row[type] = property;
row['pageLikesSourceNumber'] = results[property];
rows.push(row);
}
}
}
return rows;
}
// Report posts to rows
function reportPosts(report) {
var rows = [];
// Only loop object when it contains data
if (typeof report.data !== 'undefined' && report.data.length > 0) {
// Loop posts
for( var i = 0; i < report.data.length; i++) {
// Exclude events without message
if (typeof report.data[i]['message'] !== 'undefined') {
// Define empty row object
var row = {};
//Return date object to ISO formatted string
row["postDate"] = new Date(report.data[i]['created_time']).toISOString().slice(0, 10);
row["postMessage"] = report.data[i]['message'] || report.data[i]['story'];
row["postLink"] = report.data[i]['permalink_url'];
row["postImpressions"] = report.data[i].insights.data[0].values[0]['value'];
row["postEngagement"] = report.data[i].insights.data[1].values[0]['value'];
// Assign all post data to rows list
rows.push(row);
}
}
}
return rows;
}