forked from panoplyio/facebook-insight-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
286 lines (235 loc) · 8.46 KB
/
index.js
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// doc: this module is a facebook-insights read stream built over node readable stream
// it provide stream api to read insights data from facebook accounts,
// currently supporting only pages-insight and app-insights.
module.exports = FacebookInsightStream;
var util = require( "util" );
var sugar = require( "sugar" );
var stream = require( "stream" );
var extend = require( "extend" );
var request = require( "request" );
var Promise = require( "bluebird" );
request = Promise.promisifyAll( request )
var BASEURL = "https://graph.facebook.com/v2.5";
//edge url for each node type
var EDGEMAP = {
page: "insights",
app: "app_insights",
}
util.inherits( FacebookInsightStream, stream.Readable )
function FacebookInsightStream( options ) {
stream.Readable.call( this, { objectMode: true } );
options.edge = EDGEMAP[ options.node ];
this.options = options;
}
// _read will be called once for each collected item
FacebookInsightStream.prototype._read = function ( ) {
if ( ! this.items ) {
return this._init( this._read.bind( this ) )
}
if ( ! this.items.length ) {
return this.push( null )
}
var metrics = this.options.metrics.clone();
var item = this.items.shift();
var events = this.events.clone();
this._collect( metrics, item, {}, events )
.then( this.push.bind( this ) )
}
FacebookInsightStream.prototype._init = function ( callback ) {
var options = this.options;
// building url pattern for all the request
var until = Date.now();
var since = new Date();
since = since.setDate( since.getDate() - options.pastdays )
// fb ask for timestamp in seconds
until = Math.round( until / 1000 );
since = Math.round( since / 1000 );
var path = [
BASEURL,
"{id}",
options.edge,
"{metric}",
].join( "/" )
var query = [
"access_token=" + options.token,
"period=" + options.period,
"since=" + since,
"until=" + until,
].join( "&" );
var hasEvents = options.events && options.events.length;
var breakdowns = options.breakdowns;
if ( hasEvents ) {
query += "&event_name={ev}"
}
if ( options.aggregate ) {
query += "&aggregateBy={agg}";
}
if ( breakdowns && breakdowns.length ) {
for ( var i = 0; i < breakdowns.length; i += 1 ) {
query += "&breakdowns[{index}]={breakdown}".assign( {
index: i,
breakdown: breakdowns[ i ]
});
}
}
// this url is urlPattern shared by all the requests
// each request using thie pattern should replace the
// {id} and {metric} place holders with real values
this.url = [ path, query ].join( "?" )
// options.itemlist can be either array of items or
// promise that resolved with array of items
Promise.resolve( options.itemList )
.bind( this )
.map( this._initItem, { concurrency: 3 } )
.then( function ( items ) {
this.items = items;
this.events = options.events || [];
this.total = items.length;
this.loaded = 0;
callback();
})
.catch( this.emit.bind( this, "error" ) )
}
FacebookInsightStream.prototype._initItem = function ( item ) {
var options = this.options;
var model = {
base: BASEURL,
id: item,
token: options.token
};
var url = strReplace( "{base}/{id}?access_token={token}", model )
var title = "FACEBOOK " + options.node.toUpperCase();
console.log( new Date().toISOString(), title, url )
return request.getAsync( url )
.get( 1 )
.then( JSON.parse )
.then( errorHandler )
.then( function ( data ) {
return {
id: item,
name: data.name
}
})
}
// _collect will be called once for each metric, the insight api request
// single api call for each metric, wich result in a list of values ( value per day)
// so in attempt to create one table with all the metrics,
// we are buffering each result in a key value map, with key for
// each day in the collected time range, and appending each value
// of the current metric to the appropriate key in the buffer.
// finally we generating single row for each day.
FacebookInsightStream.prototype._collect = function ( metrics, item, buffer, events ) {
var options = this.options;
var hasEvents = events && events.length;
// done with the current item
if ( ! metrics.length && ! hasEvents ) {
var data = Object.keys( buffer ).map( function ( key ) {
var row = buffer[ key ];
// if the key is constructed with numerous attributes,
// take the datetime information
row.date = key.split( "__" )[ 0 ];
row[ options.node + "Id" ] = item.id;
row[ options.node + "Name" ] = item.name;
return row;
})
this.emit( "progress", {
total: this.total,
loaded: ++this.loaded,
message: "{{remaining}} " + options.node + "s remaining"
})
return data;
}
// for the audience API, we just use one metric ['app_event']
// with a few events
var _metric = metrics.shift() || options.metrics[ 0 ];
var model = { id: item.id, metric: _metric }
var _ev;
var _agg;
if ( hasEvents ) {
// extend the query model with event name
// and aggregation type
_ev = events.shift();
_agg = aggregationType( _ev );
extend( model, { ev: _ev, agg: _agg } );
}
var url = strReplace( this.url, model );
var title = "FACEBOOK " + options.node.toUpperCase();
console.log( new Date().toISOString(), title, url );
return request.getAsync( url )
.get( 1 )
.then( JSON.parse )
.then( errorHandler )
.get( "data" )
.bind( this )
.then( function ( data ) {
// in case that there is no data for a given metric
// we will skip to the next metric
if ( ! data.length ) {
var error = new Error( "No data found for the metric " + _metric );
error.skip = true;
throw error;
}
// in app insight the returned data is list of values
// in page insight its object that include the list of values
return data[ 0 ].values || data
})
.each( function ( val ) {
var key = val.end_time || val.time;
// when using breakdowns we get numerous results for
// the same date therefore we need to identify unique
// keys for the buffer by the date and different breakdowns
// we're using the '__' to later seperate the date
Object.keys( val.breakdowns || {} ).forEach( function ( b ){
key += "__{breakdown}".assign( {
breakdown: val.breakdowns[ b ]
});
});
buffer[ key ] || ( buffer[ key ] = {} )
// either a metric or an event
var column = _ev ? _ev : _metric;
buffer[ key ][ column ] = val.value;
// set breakdowns data if given
var breakdowns = options.breakdowns;
if ( !breakdowns || !val.breakdowns ) {
return;
}
for ( var i = 0; i < breakdowns.length; i += 1 ) {
// options breakdown
var b = breakdowns[ i ];
if ( val.breakdowns[ b ] ) {
buffer[ key ][ b ] = val.breakdowns[ b ];
}
}
})
.then( function () {
return this._collect( metrics, item, buffer, events );
})
.catch( function ( error ) {
if ( error.skip ) {
return this._collect( metrics, item, buffer )
} else {
this.emit( "error", error )
}
})
}
function errorHandler ( body ) {
if ( body.error ) {
throw new Error( body.error.message )
} else {
return body
}
}
function strReplace ( string, model ) {
Object.keys( model ).each( function ( name ) {
string = string.replace( "{" + name + "}", model[ name ] );
})
return string;
}
function aggregationType ( ev ) {
var events = [ "fb_ad_network_imp", "fb_ad_network_click" ];
var shouldUseCount = ev && events.indexOf( ev ) > -1;
if ( shouldUseCount ) {
return "COUNT"
}
return "SUM";
}