https://github.com/favstats/metatargetr
Retrieve and Parse Meta Ad Targeting Data
https://github.com/favstats/metatargetr
Last synced: 10 months ago
JSON representation
Retrieve and Parse Meta Ad Targeting Data
- Host: GitHub
- URL: https://github.com/favstats/metatargetr
- Owner: favstats
- License: other
- Created: 2022-10-19T14:41:46.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-08T13:21:10.000Z (almost 2 years ago)
- Last Synced: 2024-07-08T16:39:23.437Z (almost 2 years ago)
- Language: R
- Homepage:
- Size: 212 KB
- Stars: 30
- Watchers: 4
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
README
---
output: github_document
---
# metatargetr 
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
[](https://lifecycle.r-lib.org/articles/stages.html#maturing)
[](https://CRAN.R-project.org/package=metatargetr)
The goal of `metatargetr` is to parse targeting information from the [Meta Ad Targeting dataset](https://developers.facebook.com/docs/fort-ads-targeting-dataset/) and retrieve data from the [Audience tab](https://www.facebook.com/ads/library/?active_status=all&ad_type=political_and_issue_ads&country=NL&view_all_page_id=175740570505&sort_data[direction]=desc&sort_data[mode]=relevancy_monthly_grouped&search_type=page&media_type=all) in the Meta Ad Library. It also includes helper functions for Meta ad library data and integrates data from the Google Transparency Report.
đĄ *Support Open-Source Development*
If `metatargetr` has been helpful to you, consider [supporting the project](https://www.buymeacoffee.com/favstats)! Every contribution keeps the maintenance work going and helps me develop new features đ
```{=html}
```
---
## Table of Contents
- đ [Installation](#installation)
- đĻ [Load in Package](#load-in-package)
- đ¯ [Get Targeting Criteria](#get-targeting-criteria)
- âŗ [Last 30 Days](#get-targeting-criteria-last-30-days)
- đī¸ [Last 7 Days](#get-targeting-criteria-last-7-days)
- đ°ī¸ [Retrieve Historical Targeting Data](#retrieve-historical-targeting-data-from-database)
- đī¸ [Retrieve Historical Report Data](#retrieve-historical-report-data-from-the-database)
- âšī¸ [Get Page Info](#get-page-info)
- đ [Retrieve Targeting Metadata](#retrieve-targeting-metadata)
- đŧī¸ [Get Images and Videos](#get-images-and-videos)
- đ [Google Transparency Report](#google-transparency-report)
- đ° [Retrieve Aggregated Spending Data](#retrieve-aggregated-spending-data)
- đ [Retrieve Time-Based Spending Data](#retrieve-time-based-spending-data)
- âī¸ [Citing metatargetr](#citing-metatargetr)
---
## Installation
You can install the development version of metatargetr like so:
``` r
remotes::install_github("favstats/metatargetr")
```
## Load in Package
```{r example}
library(metatargetr)
```
## Get Targeting Criteria (Last 30 Days)
The following code retrieves the targeting criteria used by the main page of the VVD (Dutch party) in the last 30 days of available data.
Just put in the right *Page ID*. These can be found in the [Meta Ad Library](https://www.facebook.com/ads/library/) or the [Meta Ad Library Report](https://www.facebook.com/ads/library/report/). You can also [retrieve historical report data](#retrieve-historical-report-data-from-the-database) from the maintained database.
```{r}
last30 <- get_targeting(id = "121264564551002",
timeframe = "LAST_30_DAYS")
head(last30, 5)
```
## Get Targeting Criteria (Last 7 Days)
The following code retrieves the targeting criteria used by the main page of the VVD (Dutch party) in the last 7 days. Just put in the right Page ID.
```{r}
last7 <- get_targeting(id = "121264564551002",
timeframe = "LAST_7_DAYS")
head(last7, 5)
```
## Retrieve *Historical* Targeting Data from Database
Unfortunately, using `get_targeting` **you can only get the targeting criteria in the last 7, 30, and 90 days windows**. However, I have set up scrapers that retrieve the daily targeting data for every single page in the world that runs advertisements in order to archive this data. You can use the function below to retrieve it.
> Be aware: sometimes the scrapers do not work so it is possible that some pages are missing. You can use [`get_targeting_metadata`](#get-targeting-metadata) function to check which data for which country and day is present.
```{r}
# # set some parameters
the_cntry <- "DE"
tf <- 30
ds <- "2024-10-25"
# # Call the function
latest_data <- get_targeting_db(the_cntry, tf, ds)
# # Inspect the data
head(latest_data)
```
## Retrieve *Historical* Report Data from the Database
Using `get_report_db`, you can retrieve archived advertising reports for specific pages, countries, and timeframes. Reports are stored in a repository and can be downloaded and read directly into R.
> Note: While we strive to keep the archive complete, occasional scraper failures may lead to missing data for certain days.
```{r}
# # set some parameters
the_cntry <- "DE"
tf <- 30
ds <- "2024-10-25"
# # Call the function
latest_data <- get_report_db(the_cntry, tf, ds)
# # Inspect the data
head(latest_data)
```
## Get Page Info
You can also retrieve some page info of the page that you are interested in.
```{r}
page_info <- get_page_insights("121264564551002", include_info = "page_info")
str(page_info)
```
## `get_targeting_metadata()`
The `get_targeting_metadata` function is designed to retrieve metadata about targeting data releases from a GitHub repository to see which data is present (or not). It extracts and organizes information such as file names, sizes, timestamps, and tags for a specified country and timeframe. **This metadata provides an overview of the available targeting data without downloading the actual files.**
- `country_code` (*Character*):
The ISO country code (e.g., `"DE"` for Germany, `"US"` for the United States).
- `timeframe` (*Character*):
The timeframe for the targeting data. Acceptable values are:
- `"7"`: Last 7 days.
- `"30"`: Last 30 days.
- `"90"`: Last 90 days.
- `base_url` (*Character*, default: `"https://github.com/favstats/meta_ad_targeting/releases/expanded_assets/"`):
The base URL for the GitHub repository hosting the targeting data.
```{r}
# Retrieve metadata for Germany for the last 30 days
metadata <- get_targeting_metadata("DE", "30")
print(metadata)
```
## Get Images and Videos
The following code downloads the images and videos of a Meta ad. It also retrieves additional info not present in the Meta Ad Library API (e.g. `page_like_count` `cta_type` i.e. call to action button). Just put in the right Ad Archive ID.
It automatically handles duplicate images and videos (of which there are many) by hashing the images and videos and making sure they are not saved twice.
This piece of code was created in collaboration with [Philipp Mendoza](https://www.uva.nl/en/profile/m/e/p.m.mendoza/p.m.mendoza.html).
```{r}
get_ad_snapshots("561403598962843", download = T, hashing = T, mediadir = "data/media")
```
## Google Transparency Report
`ggl_get_spending` is a function in R that queries the Google Transparency Report to retrieve information about advertising spending for a specified advertiser. It supports a range of countries and can provide either aggregated data or time-based spending data.
To use `ggl_get_spending`, you need the advertiser's unique identifier, the desired date range, and the country code. The function also has an option to retrieve time-based spending data.
### Retrieve Aggregated Spending Data
Retrieve aggregated spending data for a specific advertiser in the Netherlands. It returns details like currency, number of ads, ad type breakdown, advertiser details, and other metrics.
```{r}
ggl_get_spending(advertiser_id = "AR18091944865565769729",
start_date = "2023-10-24",
end_date = "2023-11-22",
cntry = "NL")
```
### Retrieve Time-Based Spending Data
Retrieve time-based spending data for the same advertiser and country. If `get_times` is set to `TRUE`, it returns a tibble with date-wise spending data.
```{r}
# Retrieve time-based spending data for the same advertiser and country
timeseries_dat <- ggl_get_spending(advertiser_id = "AR18091944865565769729",
start_date = "2023-10-24",
end_date = "2023-11-22",
cntry = "NL",
get_times = T)
# Plotting the time-series data
timeseries_dat %>%
ggplot2::ggplot(ggplot2::aes(date, spend)) +
ggplot2::geom_col() +
ggplot2::theme_minimal()
```
---
## Citing `metatargetr`
If you use the `metatargetr` package or data from its database in your research, publications, or other outputs, please ensure you provide proper attribution. This helps recognize the effort and resources required to maintain and provide access to these data.
### Citation Format
> Votta, Fabio, & Mendoza, Philipp. (2024). `metatargetr`: A package for parsing and analyzing ad library and targeting data. GitHub. Available at: https://github.com/favstats/metatargetr
### BibTeX Entry
```bibtex
@misc{votta2024metatargetr,
author = {Votta, Fabio and Mendoza, Philipp},
title = {metatargetr: A package for parsing and analyzing ad library and targeting data},
year = {2024},
publisher = {GitHub},
url = {https://github.com/favstats/metatargetr}
}
```
### Additional Notes
If you use data from the `metatargetr` database, please include the following acknowledgement in your work:
> Data were retrieved from the `metatargetr` database, maintained by Fabio Votta. The database archives targeting data from the Meta Ad Library and Google Transparency Report. For more information, visit https://github.com/favstats/metatargetr.
By including these citations and acknowledgements, you help support the continued development of `metatargetr` and its associated resources. Thank you for your collaboration!
---