fb-graph
npm install fb-graph --save
Usage
import { FacebookAds, flattenActions, parseUserInputsToParams } from 'fb-graph'
const accessToken = 'XXXXXXXXX'
const fb = new FacebookAds({ accessToken })
const { params, actions } = parseUserInputsToParams({
reportFields:[
'date',
'clicks',
'unique_clicks',
'spend',
'adset_name',
'campaign_name',
'campaign_id',
'adset_id',
'view_content::action',
'purchase::action'
],
attributionWindow:[],
startDate:'2020-11-01',
endDate:'2020-12-01',
attributionType:'conversion'
})
const report = fb.insights({
accountId:'act_12345677898765',
...params
}).limit(100)
for await(let row of report.generator()){
console.log(row)
console.log( flattenActions(row, {
'offsite_conversion.custom.1148622891903620':'my_pruchace_event'
}, actions) )
}
Watch for progress during long Async Reports
const report = fb.insights({
accountId:'act_12345677898765',
...params
}).on('progress',function({event, ...metaData}){
console.log(event)
console.log(metaData)
})
const report = fb.insights({
accountId:'act_12345677898765',
...params
})
report.on('data',function(row){
console.log(row)
}).on('complete',function(metaData){
console.log('DONE')
})
const reportRows = await fb.insights({
accountId:'act_12345677898765',
...params
}).get()
reportRows.forEach(function(row){
console.log(row)
})