Closed vishal14995 closed 6 years ago
In short: yes, you can. Although I'd not use a for
loop for this and growing a data.frame
, but instead eg rbindlist(lapply(accounts_ids, ...))
would be much more efficient. As you provide no errors or any other info on what's not working, I'm closing this, please reopen with a minimal example.
Hi,
I want to pull fb_inights from multiple Facebook Ad Accounts and put them together in a single dataframe.
I tried setting up a loop for this but it does not seem to work. Here is the code:
Created a dataframe accountID, containing the list of account IDs.
finaldataframe = data.frame(matrix(vector(), 0, 8, //Empty Dataframe dimnames=list(c(), c("impressions", "clicks", "spend","account_name","campaign_name","date_start","date_stop","account_id"))), stringsAsFactors=F)
for (i in 1:nrow(accountID)) {
auth<-fbad_init(accountid = paste0(accountID[i,1]), token = tkn, version = '3.1')
library(rjson)
insights<-fb_insights(auth, date_preset="yesterday", job_type="async", level="ad", fields = toJSON(c("impressions","clicks","spend","campaign_name","account_name","adset_name")))
insights<-as.data.frame(insights)
finaldataframe<-rbind.data.frame(finaldataframe,insights)
}
Can we initialize multiple Facebook Ad accounts with fbad_init or is there any other way to do this?
Thanks