-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
262 lines (169 loc) · 10.1 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
---
output: github_document
---
# metatargetr <img src="man/figures/metatargetr_logo.png" width="160px" align="right"/>
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
<!-- badges: start -->
[![Lifecycle: maturing](https://img.shields.io/badge/lifecycle-maturing-lightblue.svg)](https://lifecycle.r-lib.org/articles/stages.html#maturing)
[![CRAN status](https://www.r-pkg.org/badges/version/metatargetr)](https://CRAN.R-project.org/package=metatargetr)
<!-- badges: end -->
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}
<div style="text-align: right;">
<a href="https://www.buymeacoffee.com/favstats" target="_blank">
<img src="https://img.buymeacoffee.com/button-api/?text=Buy%20me%20a%20coffee&emoji=&slug=favstats&button_colour=FFDD00&font_colour=000000&font_family=Arial&outline_colour=000000&coffee_colour=ffffff" alt="Buy Me a Coffee" style="height: 40px; width: auto;">
</a>
</div>
```
---
## 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 [`retrieve_targeting_metadata`](#retrieve-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)
```
## `retrieve_targeting_metadata()`
The `retrieve_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 <- retrieve_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!
---