Skip to content

Instantly share code, notes, and snippets.

@favstats
Created May 11, 2022 11:17
Show Gist options
  • Save favstats/16f58fa16e7536b80fb26456584668df to your computer and use it in GitHub Desktop.
Save favstats/16f58fa16e7536b80fb26456584668df to your computer and use it in GitHub Desktop.
library(tidyverse)
library(httr)
token <- Sys.getenv("fb_marketing_token")
#link to fb api
my_lin k<- "https://graph.facebook.com"
#define fields you are interested in
search_fields=c("ad_creation_time",
"ad_delivery_start_time",
"ad_delivery_stop_time",
"ad_creative_link_caption",
"ad_creative_link_description",
"ad_creative_link_title",
"currency",
"ad_creative_body",
"page_id",
"page_name",
"bylines",
"spend",
"ad_snapshot_url",
"demographic_distribution",
"languages",
"funding_entity",
"estimated_audience_size",
"publisher_platforms",
"impressions",
"delivery_by_region") %>%
stringr::str_c(., collapse=", ")
get_page_id <- function(p_id) {
#get the data from the first 'page' of data the api provides
page_one_response <- GET(my_link,
path = "/ads_archive",
query = list(access_token = token,
limit=100,
ad_active_status="ALL",
search_terms="''",
# search_page_ids = p_id,
ad_delivery_date_min = "2020-04-01",
ad_delivery_date_max = "2020-08-05",
fields=search_fields,
ad_reached_countries="US"))
page_one_content<- content(page_one_response)
x <- tibble(data=page_one_content$data)
df_imp <- x %>%
unnest_wider(data)
#get the link refering to the next page
next_link <- page_one_content$paging$`next`
page <- 1
#iterate over all pages until there is no further page
while(length(next_link)>0) {
# while(T) {
next_response <- GET(next_link)
next_content<- content(next_response)
heads_up <- httr::headers(next_response)
usage_dat <- jsonlite::fromJSON(heads_up$`x-business-use-case-usage`) %>%
.[[1]]
print(paste0(p_id, ": ", page, " (", usage_dat$object_count_pct, ")"))
y <- tibble(data=next_content$data)
df_next <- y %>%
unnest_wider(data)
df_imp <- bind_rows(df_imp, df_next)
next_link <- next_content$paging$`next`
page <- page + 1
saveRDS(df_imp, file = glue::glue("data/page_ids/{p_id}.rds"))
}
}
# page_ids_to_get <- readRDS("data/page_ids_to_get.rds")
# page_ids_to_get <- readRDS("data/pages_id.rds")
page_ids_to_get <- readRDS("data/page_ids_to_get.rds") %>%
bind_rows(readRDS("data/pages_id.rds")) %>%
distinct(page_id)
# page_ids_to_get$page_id %>%
# # setdiff(dir("data/page_ids/") %>%
# # str_remove_all(".rds")) %>%
# walk(get_page_id)
# debugonce(get_page_id)
get_page_id("all")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment