Title: | 'Facebook Marketing API' Social Watcher |
---|---|
Description: | Facilitates querying data from the ‘Facebook Marketing API', particularly for social science research <https://developers.facebook.com/docs/marketing-apis/>. Data from the 'Facebook Marketing API' has been used for a variety of social science applications, such as for poverty estimation (Marty and Duhaut (2024) <doi:10.1038/s41598-023-49564-6>), disease surveillance (Araujo et al. (2017) <doi:10.48550/arXiv.1705.04045>), and measuring migration (Alexander, Polimis, and Zagheni (2020) <doi:10.1007/s11113-020-09599-3>). The package facilitates querying the number of Facebook daily/monthly active users for multiple location types (e.g., from around a specific coordinate to an administrative region) and for a number of attribute types (e.g., interests, behaviors, education level, etc). The package supports making complex queries within one API call and making multiple API calls across different locations and/or parameters. |
Authors: | Robert Marty [aut, cre] |
Maintainer: | Robert Marty <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.1.1 |
Built: | 2024-11-02 04:12:15 UTC |
Source: | https://github.com/worldbank/rsocialwatcher |
This function returns dataframes of Facebook parameters and their associated IDs for different categories of information. Categories include behaviors, interests, locales, job titles, education major, and location (e.g., country, city, zip code, etc). The returned dataframe contains ids that can be used in the query_fb_marketing_api function.
get_fb_parameter_ids( type, version, token, q = NULL, country_code = NULL, region_id = NULL, key = NULL, limit = NULL, add_location_coords = FALSE )
get_fb_parameter_ids( type, version, token, q = NULL, country_code = NULL, region_id = NULL, key = NULL, limit = NULL, add_location_coords = FALSE )
type |
Type of data. Either: "behaviors", "demographics", "interests", "income", "industries", "life_events", "family_statuses", "work_positions", "work_employers", "education_statuses", "relationship_statuses", "education_majors", "locales", "country", "country_group", "region", "large_geo_area", "medium_geo_area", "small_geo_area", "city", "subcity", "neighborhood", "zip", "geo_market", "electoral_district", "zip" |
version |
'Facebook Marketing' API version; for example, "v19.0" |
token |
'Facebook Marketing' API token |
q |
Query string to limit search. For example, when searching job titles, setting |
country_code |
When searching locations, limit the search to a specific country; for example, only search for cities within a specific country. |
region_id |
When searching locations, limit the search to a specific region; for example, only search for cities within a specific region. |
key |
When searching locations, limit the search to a specific location key; for example, only search for neighborhood within a specific city. |
limit |
Number of parameter IDs to search for. |
add_location_coords |
When querying location IDs (eg, when |
For additional information, see: https://developers.facebook.com/docs/marketing-api/audiences/reference/targeting-search/
Dataframe with parameter IDs and descriptions.
## Not run: #### Define version and token VERSION = "enter-version" TOKEN = "enter-token" #### Query parameter IDs get_fb_parameter_ids(type = "interests", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "behaviors", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "education_majors", q = "Computer", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "education_schools", q = "Washington", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "education_statuses", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "family_statuses", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "income", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "industries", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "work_positions", q = "Data", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "work_employers", q = "World Bank", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "relationship_statuses", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "life_events", version = VERSION, token = TOKEN) #### Location IDs get_fb_parameter_ids(type = "country", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "region", country_code = "US", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "medium_geo_area", country_code = "US", q = "Henrico", version = VERSION, token = TOKEN) ## End(Not run)
## Not run: #### Define version and token VERSION = "enter-version" TOKEN = "enter-token" #### Query parameter IDs get_fb_parameter_ids(type = "interests", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "behaviors", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "education_majors", q = "Computer", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "education_schools", q = "Washington", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "education_statuses", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "family_statuses", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "income", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "industries", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "work_positions", q = "Data", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "work_employers", q = "World Bank", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "relationship_statuses", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "life_events", version = VERSION, token = TOKEN) #### Location IDs get_fb_parameter_ids(type = "country", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "region", country_code = "US", version = VERSION, token = TOKEN) get_fb_parameter_ids(type = "medium_geo_area", country_code = "US", q = "Henrico", version = VERSION, token = TOKEN) ## End(Not run)
When targeting around a specific location, returns a suggested radius to reach enough people
get_fb_suggested_radius(location, distance_unit = "kilometer", version, token)
get_fb_suggested_radius(location, distance_unit = "kilometer", version, token)
location |
Vector of latitude and longitude ( |
distance_unit |
Either |
version |
'Facebook Marketing' API version; for example, "v19.0" |
token |
'Facebook Marketing' API token |
For more information, see the Facebook documentation here
Dataframe with suggested radius and distance unit
## Not run: get_fb_suggested_radius(location = c(38.89831, -77.03658), version = "v14.0", token = "TOKEN-HERE") ## End(Not run)
## Not run: get_fb_suggested_radius(location = c(38.89831, -77.03658), version = "v14.0", token = "TOKEN-HERE") ## End(Not run)
Get Coordinates/Geometries for Valid Location Keys
get_location_coords( location_unit_type, location_keys, version, token, large_query_chunk_size = 10, large_query_pause = 0, limit = NULL, verbose = TRUE )
get_location_coords( location_unit_type, location_keys, version, token, large_query_chunk_size = 10, large_query_pause = 0, limit = NULL, verbose = TRUE )
location_unit_type |
Either |
location_keys |
Key associated with location. Use the |
version |
API version. e.g., "v19.0" |
token |
Facebook API token |
large_query_chunk_size |
The function will first try to query all locations using one API call. If too many locations are requested, the function will query in chunks. By default, the function will query 10 locations at a time. (Default: 10). |
large_query_pause |
The function will first try to query all locations using one API call. If too many locations are requested, the function will query in chunks. After each query, the |
limit |
Number of parameter IDs to search for. |
verbose |
If the function needs to make multiple queries to obtain location information for all location keys, print progress. (Default: |
Spatial features dataframe
## Not run: #### Define version, creation act, and token VERSION = "enter-version" TOKEN = "enter-token" #### Grab locations loc_sf <- get_location_coords(location_type = "countries", location_keys = c("US", "MX", "CA"), version = VERSION, token = TOKEN) ## End(Not run)
## Not run: #### Define version, creation act, and token VERSION = "enter-version" TOKEN = "enter-token" #### Grab locations loc_sf <- get_location_coords(location_type = "countries", location_keys = c("US", "MX", "CA"), version = VERSION, token = TOKEN) ## End(Not run)
query_fb_marketing_api()
Groups input into a map_param
object. When a map_param
object is entered as a parameter in query_fb_marketing_api()
, query_fb_marketing_api()
makes a separate API query for each item within map_param
. A map_param
object is structured as a list, where the map_param
class triggers the query_fb_marketing_api
function to make a separate API query for each item in the list.Map Parameters within query_fb_marketing_api()
Groups input into a map_param
object. When a map_param
object is entered as a parameter in query_fb_marketing_api()
, query_fb_marketing_api()
makes a separate API query for each item within map_param
. A map_param
object is structured as a list, where the map_param
class triggers the query_fb_marketing_api
function to make a separate API query for each item in the list.
map_param(...)
map_param(...)
... |
Vector or list |
Object of class map_param
to be used as input to the query_fb_marketing_api()
function to make multiple API queries.
## Not run: # Make 3 queries: # 1. Number of males and females MAU/DAU # 2. Number of male MAU/DAU # 3. Number of female MAU/DAU query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", gender = map_param(c(1,2), 1, 2), version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## End(Not run)
## Not run: # Make 3 queries: # 1. Number of males and females MAU/DAU # 2. Number of male MAU/DAU # 3. Number of female MAU/DAU query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", gender = map_param(c(1,2), 1, 2), version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## End(Not run)
map_param
object. When a map_param
object is entered as a parameter in query_fb_marketing_api()
, query_fb_marketing_api()
makes a separate API query for each item within map_param
(ie, for each item in the original vector entered into map_param
). A map_param
object is structured as a list, where the map_param
class triggers the query_fb_marketing_api
function to make a separate API query for each item in the list.Map Parameters over Vector
Converts a vector into a map_param
object. When a map_param
object is entered as a parameter in query_fb_marketing_api()
, query_fb_marketing_api()
makes a separate API query for each item within map_param
(ie, for each item in the original vector entered into map_param
). A map_param
object is structured as a list, where the map_param
class triggers the query_fb_marketing_api
function to make a separate API query for each item in the list.
map_param_vec(...)
map_param_vec(...)
... |
Vector |
Object of class map_param
to be used as input to the query_fb_marketing_api()
function to make multiple API queries.
## Not run: # Make 2 queries: # 1. Number of male MAU/DAU # 2. Number of female MAU/DAU query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", gender = map_param_vec(1:2), version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## End(Not run)
## Not run: # Make 2 queries: # 1. Number of male MAU/DAU # 2. Number of female MAU/DAU query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", gender = map_param_vec(1:2), version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## End(Not run)
Query 'Facebook Marketing' API
query_fb_marketing_api( location_unit_type, lat_lon = NULL, radius = NULL, radius_unit = NULL, location_keys = NULL, location_types = c("home", "recent"), locales = NULL, interests = NULL, behaviors = NULL, college_years = NULL, education_majors = NULL, education_schools = NULL, education_statuses = NULL, family_statuses = NULL, income = NULL, industries = NULL, life_events = NULL, relationship_statuses = NULL, work_positions = NULL, work_employers = NULL, excl_interests = NULL, excl_behaviors = NULL, excl_college_years = NULL, excl_education_majors = NULL, excl_education_schools = NULL, excl_education_statuses = NULL, excl_family_statuses = NULL, excl_income = NULL, excl_industries = NULL, excl_life_events = NULL, excl_relationship_statuses = NULL, excl_work_positions = NULL, excl_work_employers = NULL, user_os = NULL, wireless_carrier = NULL, gender = c(1, 2), age_min = 18, age_max = 65, flex_target = NULL, version, creation_act, token, sleep_time = 0.1, show_result = FALSE, verbose = TRUE, add_query = FALSE, add_query_hide_credentials = TRUE )
query_fb_marketing_api( location_unit_type, lat_lon = NULL, radius = NULL, radius_unit = NULL, location_keys = NULL, location_types = c("home", "recent"), locales = NULL, interests = NULL, behaviors = NULL, college_years = NULL, education_majors = NULL, education_schools = NULL, education_statuses = NULL, family_statuses = NULL, income = NULL, industries = NULL, life_events = NULL, relationship_statuses = NULL, work_positions = NULL, work_employers = NULL, excl_interests = NULL, excl_behaviors = NULL, excl_college_years = NULL, excl_education_majors = NULL, excl_education_schools = NULL, excl_education_statuses = NULL, excl_family_statuses = NULL, excl_income = NULL, excl_industries = NULL, excl_life_events = NULL, excl_relationship_statuses = NULL, excl_work_positions = NULL, excl_work_employers = NULL, user_os = NULL, wireless_carrier = NULL, gender = c(1, 2), age_min = 18, age_max = 65, flex_target = NULL, version, creation_act, token, sleep_time = 0.1, show_result = FALSE, verbose = TRUE, add_query = FALSE, add_query_hide_credentials = TRUE )
location_unit_type |
Either ——————————If location_unit_type is "coordinates" |
lat_lon |
Coordinates, c(lat, lon). For example, |
radius |
Radius around coordinate |
radius_unit |
Unit for radius; either ——————————If location_unit_type is not "coordinates" |
location_keys |
Key associated with location. Use the ——————————Other location parameters |
location_types |
Either: (1) |
locales |
Locales ID. For more information on locales, see the Advanced Targeting Documentation ——————————Parameters:
|
interests |
Interest IDs. For example, |
behaviors |
Behavior IDs. For example, |
college_years |
College graduation years. For example, |
education_majors |
Education major IDs. For example, |
education_schools |
School IDs. For example, |
education_statuses |
Education status IDs. For example, |
family_statuses |
Family status IDs. For example, |
income |
Income IDs. For example, |
industries |
Industries IDs. For example, |
life_events |
Life event IDs. For example, |
relationship_statuses |
Relationship status IDs. For example, |
work_positions |
Work position IDs. For example, |
work_employers |
Work employer IDs. For example, ——————————Exlcude Facebook users from query by select parameters |
excl_interests |
Interest IDs to exclude. |
excl_behaviors |
Behavior IDs to exclude. |
excl_college_years |
Colleage year IDs to exclude. |
excl_education_majors |
Education major IDs to exclude. |
excl_education_schools |
Education school IDs to exclude. |
excl_education_statuses |
Education status IDs to exclude. |
excl_family_statuses |
Family status IDs to exclude. |
excl_income |
Income IDs to exclude. |
excl_industries |
Industry IDs to exclude. |
excl_life_events |
Life event IDs to exclude. |
excl_relationship_statuses |
Relationship status IDs to exclude. |
excl_work_positions |
Work position IDs to exclude. |
excl_work_employers |
Work employer IDs to exclude. ——————————Non-Flexible parameters:
|
user_os |
User operating systems. For example, |
wireless_carrier |
Wireless carrier. If set to |
gender |
Genders to target; 1 targets males and 2 targets females. Default is both. See |
age_min |
Minimum age. Default is 18. See |
age_max |
Maximum age. Default is 65. See ——————————Flex targeting parameters |
flex_target |
Flexible targeting allows for more complicated parameter specifications. For example, specifying OR conditions across parameter types (eg, behaviors, interests, etc). For information on how to use flexible targeting, see the documentation here. ——————————Parameters for credentials |
version |
API version. e.g., "v19.0" |
creation_act |
Facebook creation act |
token |
Facebook API token ——————————Scraping parameters |
sleep_time |
How much time (in seconds) to pause between each query (default: |
show_result |
After each query, whether to print the number of monthly active users (default: |
verbose |
Display messages that indicate the function is pausing before making additional queries. Pausing can result from API key rate limits or no internet (default: ——————————Return query text |
add_query |
If |
add_query_hide_credentials |
If |
Dataframe that includes (1) daily and monthly active users and (2) parameter values
get_fb_parameter_ids()
To get IDs and descriptions for behaviors, demographics, interests, and locales. For additional details on how to use the package, see the documentation here.
## Not run: #### Define version, creation act, and token VERSION = "enter-version" CREATION_ACT = "creation_act" TOKEN = "enter-token" #### Query data ## All Facebook users in US query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## All Facebook users in US with interest in concernts concert_id <- get_fb_parameter_ids(type = "interests", version = VERSION, token = TOKEN) %>% filter(name == "Concerts (music event)") %>% pull(id) query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", interests = concert_id, version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## End(Not run)
## Not run: #### Define version, creation act, and token VERSION = "enter-version" CREATION_ACT = "creation_act" TOKEN = "enter-token" #### Query data ## All Facebook users in US query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## All Facebook users in US with interest in concernts concert_id <- get_fb_parameter_ids(type = "interests", version = VERSION, token = TOKEN) %>% filter(name == "Concerts (music event)") %>% pull(id) query_fb_marketing_api( location_unit_type = "countries", location_keys = "US", interests = concert_id, version = VERSION, creation_act = CREATION_ACT, token = TOKEN) ## End(Not run)