How do I access (new july 2022) targeting information from the Facebook Ad Library API (R solution preferred)?

Viewed 71

As this announcement mentions (https://www.facebook.com/business/news/transparency-social-issue-electoral-political-ads) new targeting information (or a summary) has been made available in the Facebook Ad Library.

I am used to use the 'Radlibrary' package in R, but I can't seem to find any fields in 'Radlibrary' which allows me to get this information? Does anyone know either how to access this information from the Radlibrary package in R (preferred, since this is what I know and usually works with) or how to access this from the API in another way?

I use it to look at how politicians choose to target their ads, why it would be a too big of a task to manually look it up at the facebook.com/ads/library

EDIT The targeting I refer to is found browsering the ad library like the screenshots below

enter image description here

enter image description here

1 Answers

Thanks for highlighting this data being published which I did not know had been announced. I just registered for an API token to play around with it.

It seems to me that looking for ads from a particular politician or organisation is a question of downloading large amounts of data and then manipulating it in R. For example, to recreate the curl query on the API docs page:

curl -G \
-d "search_terms='california'" \
-d "ad_type=POLITICAL_AND_ISSUE_ADS" \
-d "ad_reached_countries=['US']" \
-d "access_token=<ACCESS_TOKEN>" \
"https://graph.facebook.com/<API_VERSION>/ads_archive"

We can simply do:

# enter token interactively so it doesn't get added to R history
token <- readline() 

query <- adlib_build_query(
    search_terms = "california",
    ad_reached_countries = 'US', 
    ad_type = "POLITICAL_AND_ISSUE_ADS"
)

response <- adlib_get(params = query, token = token)

results_df <- Radlibrary::as_tibble(response, censor_access_token = TRUE)

This seems to return what one would expect:

names(results_df)
#  [1] "id"                            "ad_creation_time"              "ad_creative_bodies"            "ad_creative_link_captions"     "ad_creative_link_titles"       "ad_delivery_start_time"
#  [7] "ad_snapshot_url"               "bylines"                       "currency"                      "languages"                     "page_id"                       "page_name"
# [13] "publisher_platforms"           "estimated_audience_size_lower" "estimated_audience_size_upper" "impressions_lower"             "impressions_upper"             "spend_lower"
# [19] "spend_upper"                   "ad_creative_link_descriptions" "ad_delivery_stop_time"

library(dplyr)
results_df |>
    group_by(page_name) |>
    summarise(n = n()) |>
    arrange(desc(n))

# # A tibble: 237 x 2
#    page_name                    n
#    <chr>                    <int>
#  1 Senator Brian Dahle        169
#  2 Katie Porter               122
#  3 PragerU                     63
#  4 Results for California      28
#  5 Big News Buzz               20
#  6 California Water Service    20
#  7 Cancer Care is Different    17
#  8 Robert Reich                14
#  9 Yes On 28                   14
# 10 Protect Tribal Gaming       13
# # ... with 227 more rows

Now - assuming that you are interested specifically in the ads by Senator Brian Dahle, it does not appear that you can send a query for all ads he has placed (i.e. using the page_name parameter in the query). But you can request for all political ads in their area (setting the limit parameter to a high number) with a particular search_term or search_page_id, and then filter the data to the relevant person.

Related