-
Notifications
You must be signed in to change notification settings - Fork 5
/
README.Rmd
121 lines (73 loc) · 4.32 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
---
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%"
)
#source("R/get_targeting.R")
```
<!-- badges: start -->
[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)
[![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/) as well as 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 some helper functions to work with Meta ad library data in general. Finally, it has also been enhanced with some data to interact with the Google Transparency Report.
## 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. Just put in the right Page ID.
```{r}
last30 <- get_targeting("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("121264564551002", timeframe = "LAST_7_DAYS")
head(last7, 5)
```
## 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 Ad Library 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 = "AR18177962546424709121",
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 = "AR18177962546424709121",
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()
```