-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_audiences.R
69 lines (49 loc) · 1.71 KB
/
get_audiences.R
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
source("utils.R")
# ?get_targeting
# get_targeting("41459763029", timeframe = "LAST_90_DAYS")
# debugonce(get_targeting)
library(httr)
library(tidyverse)
tstamp <- Sys.time()
# source("utils.R")
last90days <- read_csv("data/FacebookAdLibraryReport_2022-11-25_US_last_90_days_advertisers.csv") %>%
janitor::clean_names() %>%
arrange(desc(amount_spent_usd)) %>%
mutate(spend_upper = amount_spent_usd %>% as.numeric()) %>%
arrange(-spend_upper) %>%
mutate_all(as.character)
georgia_wtm <- readr::read_csv("data/wtm-advertisers-us-2022-11-28T14_22_01.338Z.csv") %>%
select(page_name = name,
page_id = advertisers_platforms.advertiser_platform_ref) %>%
mutate(page_id = as.character(page_id))
# options(scipen = 999999)
# georgia_wtm
internal_page_ids <- georgia_wtm %>%
mutate_all(as.character) %>%
bind_rows(last90days) %>%
distinct(page_id, .keep_all = T)
# get_targeting(internal_page_ids$page_id[1], timeframe = "LAST_30_DAYS")
### save seperately
internal_page_ids %>% #count(cntry, sort =T) %>%
# filter(!(page_id %in% already_there)) %>%
# filter(cntry == "GB") %>%
# slice(1:10) %>%
split(1:nrow(.)) %>%
walk(~{
try({
print(paste0(.x$page_name,": ", round(which(internal_page_ids$page_id == .x$page_id)/nrow(internal_page_ids)*100, 2)))
yo <- get_targeting(.x$page_id, timeframe = "LAST_30_DAYS") %>%
mutate(tstamp = tstamp)
if(nrow(yo)!=0){
path <- paste0("midterms/",.x$page_id, ".rds")
# if(file.exists(path)){
# ol <- read_rds(path)
#
# saveRDS(yo %>% bind_rows(ol), file = path)
# } else {
saveRDS(yo, file = path)
# }
}
print(nrow(yo))
})
})