Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
drop pre v2.6 support and default to v2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
daroczig committed Jul 27, 2016
1 parent 02d05b6 commit d7f0acc
Show file tree
Hide file tree
Showing 34 changed files with 92 additions and 346 deletions.
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ S3method(print,FB_Ad_Account)
export(fb_api_most_recent_version)
export(fb_api_version)
export(fb_insights)
export(fb_reportstats_ad)
export(fb_stats_ad)
export(fbad_add_audience)
export(fbad_create_ad)
export(fbad_create_adset)
Expand Down
68 changes: 15 additions & 53 deletions R/fb_ad.R
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
#' Create ad
#' @inheritParams fbad_request
#' @param name Ad group name
#' @param campaign_id Ad Set id (v2.4)
#' @param adset_id Ad Set id (v2.5)
#' @param adset_id Ad Set id
#' @param creative_id creative ID
#' @param adgroup_status initial status of the Ad group (v2.4)
#' @param status initial status of the Ad group (v2.5)
#' @param status initial status of the Ad group
#' @param ... further parameters passed to the Facebook API
#' @return ad id
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup/v2.5#Creating}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup#Creating}
fbad_create_ad <- function(fbacc,
name,
## v2.4
campaign_id,
## v2.5
adset_id,
creative_id,
adgroup_status = c('ACTIVE', 'PAUSED'),
Expand All @@ -27,24 +22,13 @@ fbad_create_ad <- function(fbacc,

## initial status of the ad to be created
status <- match.arg(status)
if (!missing(adgroup_status)) {
warning('"adgroup_status" argument is deprecated, use "status" instead from v2.5')
status <- match.arg(adgroup_status)
}

## build params list
params <- list(
name = name,
creative = toJSON(list(creative_id = unbox(creative_id))))

## different campaign names in v2.4 VS v2.5
if (fb_api_version() < '2.5') {
params$campaign_id <- campaign_id
params$adgroup_status <- status
} else {
params$adset_id <- adset_id
params$status <- status
}
name = name,
creative = toJSON(list(creative_id = unbox(creative_id))),
adset_id = adset_id,
status = status)

## add further params if provided
if (length(list(...)) > 0) {
Expand All @@ -53,8 +37,7 @@ fbad_create_ad <- function(fbacc,

## get results
res <- fbad_request(fbacc,
path = paste0('act_', fbacc$account_id,
ifelse(fb_api_version() < '2.5', '/adgroups', '/ads')),
path = paste0('act_', fbacc$account_id, '/ads'),
method = "POST",
params = params)

Expand All @@ -71,7 +54,7 @@ fbad_create_ad <- function(fbacc,
#' @return data.frame
#' @note Will do a batched request to the Facebook API if multiple ids are provided.
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup/v2.5#Reading}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup#Reading}
#' @examples \dontrun{
#' ## get and Ad ID from yesterday
#' adid <- fb_insights(date_preset = 'yesterday', level = 'ad')[[1]]$ad_id[1]
Expand Down Expand Up @@ -130,7 +113,7 @@ fbad_read_ad <- function(fbacc, id, fields = 'id') {
#' @param ... parameters passed to Facebook API
#' @return invisible TRUE
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup/v2.5#Updating}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup#Updating}
fbad_update_ad <- function(fbacc, id, ...) {

fbacc <- fbad_check_fbacc()
Expand Down Expand Up @@ -160,7 +143,7 @@ fbad_update_ad <- function(fbacc, id, ...) {
#' @return data.frame
#' @note Will do a batched request to the Facebook API if multiple ids are provided.
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup/v2.5#read-adaccount}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup#read-adaccount}
fbad_list_ad <- function(fbacc, id, statuses, fields = 'id') {

fbacc <- fbad_check_fbacc()
Expand All @@ -176,25 +159,7 @@ fbad_list_ad <- function(fbacc, id, statuses, fields = 'id') {

## filter for status
if (!missing(statuses)) {

if (fb_api_version() < '2.5') {

params$adgroup_status <- toJSON(statuses)

## update filter name for Ad Sets and Campaigns
if (fn == 'fbad_list_adset') {
names(params)[3] <- 'campaign_status'
}
if (fn == 'fbad_list_campaign') {
names(params)[3] <- 'campaign_group_status'
}

} else {

params$effective_status <- toJSON(statuses)

}

params$effective_status <- toJSON(statuses)
}

## default ID for current Ad Account
Expand All @@ -204,12 +169,9 @@ fbad_list_ad <- function(fbacc, id, statuses, fields = 'id') {

## API endpoint
endpoint <- switch(fn,
'fbad_list_ad' = ifelse(fb_api_version() < '2.5',
'adgroups', 'ads'),
'fbad_list_adset' = ifelse(fb_api_version() < '2.5',
'adcampaigns', 'adsets'),
'fbad_list_campaign' = ifelse(fb_api_version() < '2.5',
'adcampaign_groups', 'campaigns'))
'fbad_list_ad' = 'ads',
'fbad_list_adset' = 'adsets',
'fbad_list_campaign' = 'campaigns')

## paged query for one id
if (length(id) == 1) {
Expand Down
42 changes: 9 additions & 33 deletions R/fb_adset.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
#' @param optimization_goal optimization goal
#' @param billing_event the billing event
#' @param bid_amount integer
#' @param promoted_object see at \url{https://developers.facebook.com/docs/marketing-api/reference/ad-campaign/promoted-object/v2.4}
#' @param campaign_group_id parent Ad Campaign id (v2.4)
#' @param campaign_id parent Ad Campaign id (v2.5)
#' @param campaign_status Ad Set status (v2.4)
#' @param status Ad Set status (v2.5)
#' @param promoted_object see at \url{https://developers.facebook.com/docs/marketing-api/reference/ad-campaign/promoted-object}
#' @param campaign_id parent Ad Campaign id
#' @param status Ad Set status
#' @param daily_budget using account currency
#' @param lifetime_budget using account currency
#' @param end_time UTC UNIX timestamp
Expand All @@ -29,10 +27,6 @@ fbad_create_adset <- function(fbacc,
daily_budget, lifetime_budget,
end_time, start_time,
targeting,
## v2.4 arguments
campaign_group_id,
campaign_status = c('ACTIVE', 'PAUSED', 'ARCHIVED', 'DELETED'),
## other
...) {

fbacc <- fbad_check_fbacc()
Expand All @@ -52,10 +46,7 @@ fbad_create_adset <- function(fbacc,
}

## we need a campaign_group_id
if (fb_api_version() < '2.5' && missing(campaign_group_id)) {
stop('A campaign ad ID is required.')
}
if (fb_api_version() >= '2.5' && missing(campaign_id)) {
if (missing(campaign_id)) {
stop('A campaign ad ID is required.')
}

Expand All @@ -69,20 +60,9 @@ fbad_create_adset <- function(fbacc,
name = name,
optimization_goal = optimization_goal,
billing_event = billing_event,
bid_amount = bid_amount)

## version specific params
if (fb_api_version() < '2.5') {

params$campaign_group_id <- campaign_group_id
params$campaign_status <- match.arg(campaign_status)

} else {

params$campaign_id <- campaign_id
params$configured_status <- match.arg(status)

}
bid_amount = bid_amount,
campaign_id = campaign_id,
configured_status = match.arg(status))

## end_time for lifetime budget
if (!missing(lifetime_budget) && missing(end_time)) {
Expand All @@ -108,9 +88,7 @@ fbad_create_adset <- function(fbacc,
}

## promoted object based on parent campaign
campaign <- fbad_read_campaign(fbacc, ifelse(fb_api_version() < '2.5',
campaign_group_id,
campaign_id),
campaign <- fbad_read_campaign(fbacc, campaign_id,
fields = 'objective')
if (campaign$objective %in% c('WEBSITE_CONVERSIONS', 'PAGE_LIKES', 'OFFER_CLAIMS', 'MOBILE_APP_INSTALLS', 'CANVAS_APP_INSTALLS', 'MOBILE_APP_ENGAGEMENT', 'CANVAS_APP_ENGAGEMENT') && missing(promoted_object)) {
stop(paste('A promoted object is needed when having the objective of', campaign$objective, 'in the parent ad campaign.'))
Expand All @@ -126,9 +104,7 @@ fbad_create_adset <- function(fbacc,

## get results
res <- fbad_request(fbacc,
path = paste0('act_', fbacc$account_id,
ifelse(fb_api_version() < '2.5',
'/adcampaigns', '/adsets')),
path = paste0('act_', fbacc$account_id, '/adsets'),
method = "POST",
params = params)

Expand Down
23 changes: 6 additions & 17 deletions R/fb_audience.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#' Create a new FB custom audience
#' @references \url{https://developers.facebook.com/docs/marketing-api/custom-audience-targeting/v2.4#create}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/custom-audience#Creating}
#' @inheritParams fbad_request
#' @param name string
#' @param description optional string
Expand All @@ -23,11 +23,6 @@ fbad_create_audience <- function(fbacc, name, description, opt_out_link) {
params$opt_out_link <- opt_out_link
}

## this is a static param required by v2.4
if (fbacc$api_version >= '2.4') {
params$subtype <- 'CUSTOM'
}

## get results
res <- fbad_request(fbacc,
path = paste0('act_', fbacc$account_id, '/customaudiences'),
Expand All @@ -41,7 +36,7 @@ fbad_create_audience <- function(fbacc, name, description, opt_out_link) {


#' Read metadata on a FB custom audience
#' @references \url{https://developers.facebook.com/docs/marketing-api/custom-audience-targeting/v2.4#read}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/custom-audience#Reading}
#' @inheritParams fbad_request
#' @param audience_id numeric
#' @param fields character vector of fields to be returned
Expand Down Expand Up @@ -69,7 +64,7 @@ fbad_read_audience <- function(fbacc, audience_id, fields = c('id', 'account_id'


#' Delete a FB custom audience
#' @references \url{https://developers.facebook.com/docs/marketing-api/custom-audience-targeting/v2.4#delete}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/custom-audience#Deleting}
#' @inheritParams fbad_request
#' @param audience_id numeric
#' @return custom audience ID
Expand All @@ -92,7 +87,6 @@ fbad_delete_audience <- function(fbacc, audience_id) {


#' Share a FB custom audience with other accounts
#' @references \url{https://developers.facebook.com/docs/marketing-api/custom-audience-targeting/v2.3#sharing}
#' @inheritParams fbad_request
#' @param audience_id audience ID
#' @param adaccounts numeric vector of FB account IDs
Expand All @@ -115,7 +109,7 @@ fbad_share_audience <- function(fbacc, audience_id, adaccounts) {


#' Add people to a custom FB audience
#' @references \url{https://developers.facebook.com/docs/marketing-api/custom-audience-targeting/v2.4#create}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/custom-audience/users/#Creating}
#' @inheritParams fbad_request
#' @param audience_id string
#' @param schema only two schema are supported out of the four: you can add/remove persons to/from a custom audience by e-mail addresses or phone numbers
Expand Down Expand Up @@ -169,12 +163,12 @@ fbad_add_audience <- function(fbacc, audience_id,
#' Add people from a custom FB audience
#' @inheritParams fbad_add_audience
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/custom-audience/users#Deleting}
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/custom-audience/users/#Deleting}
fbad_remove_audience <- fbad_add_audience


#' Create a new FB lookalike audience similar to an already existing custom audience
#' @references \url{https://developers.facebook.com/docs/marketing-api/lookalike-audience-targeting/v2.4#create}
#' @references \url{https://developers.facebook.com/docs/marketing-api/lookalike-audience-targeting#create}
#' @inheritParams fbad_request
#' @param name string
#' @param origin_audience_id numeric ID of origin custom audience
Expand All @@ -201,11 +195,6 @@ fbad_create_lookalike_audience <- function(fbacc, name, origin_audience_id, rati
country = country
), auto_unbox = TRUE))

## this is a static param required by v2.4
if (fbacc$api_version >= '2.4') {
params$subtype <- 'LOOKALIKE'
}

## get results
res <- fbad_request(fbacc,
path = paste0('act_', fbacc$account_id, '/customaudiences'),
Expand Down
62 changes: 19 additions & 43 deletions R/fb_campaign.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#' Created Ad Campaign
#' @inheritParams fbad_request
#' @param buying_type Facebook optimization algorithm to delivery, pricing, and limits
#' @param campaign_status initial status of the Ad Campaign (v2.5)
#' @param campaign_group_status initial status of the Ad Campaign (v2.4)
#' @param campaign_status initial status of the Ad Campaign
#' @param execution_options special execution settings passed to the API
#' @param name Ad Campaign name
#' @param objective the campaign's objective
Expand All @@ -11,10 +10,16 @@
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group#Creating}
fbad_create_campaign <- function(fbacc, buying_type = c('AUCTION', 'FIXED_CPM', 'RESERVED'),
campaign_group_status = c('ACTIVE', 'PAUSED'),
campaign_status = c('ACTIVE', 'PAUSED'),
execution_options = NULL, name,
objective,
objective = c(
'BRAND_AWARENESS', 'CANVAS_APP_ENGAGEMENT',
'CANVAS_APP_INSTALLS', 'CONVERSIONS',
'EVENT_RESPONSES', 'EXTERNAL', 'LEAD_GENERATION',
'LINK_CLICKS', 'LOCAL_AWARENESS',
'MOBILE_APP_ENGAGEMENT', 'MOBILE_APP_INSTALLS',
'OFFER_CLAIMS', 'PAGE_LIKES', 'POST_ENGAGEMENT',
'PRODUCT_CATALOG_SALES', 'VIDEO_VIEWS'),
spend_cap = NULL) {

fbacc <- fbad_check_fbacc()
Expand All @@ -25,51 +30,22 @@ fbad_create_campaign <- function(fbacc, buying_type = c('AUCTION', 'FIXED_CPM',

## build params list
params <- list(
buying_type = buying_type,
objective = objective,
name = name,
execution_options = execution_options,
spend_cap = spend_cap)

## version specific params
if (fb_api_version() < '2.5') {

params$objective <- match.arg(
objective,
c(
'NONE', 'CANVAS_APP_ENGAGEMENT', 'CANVAS_APP_INSTALLS',
'EVENT_RESPONSES', 'LOCAL_AWARENESS', 'MOBILE_APP_ENGAGEMENT',
'MOBILE_APP_INSTALLS', 'OFFER_CLAIMS', 'PAGE_LIKES',
'POST_ENGAGEMENT', 'VIDEO_VIEWS', 'WEBSITE_CLICKS',
'WEBSITE_CONVERSIONS'))
params$campaign_group_status <- match.arg(campaign_group_status)

} else {

## objectives were changed in v2.5
## * WEBSITE_CLICKS -> LINK_CLICKS
## * WEBSITE_CONVERSIONS -> CONVERSIONS
params$objective <- match.arg(
objective,
c('BRAND_AWARENESS', 'CANVAS_APP_ENGAGEMENT', 'CANVAS_APP_INSTALLS',
'CONVERSIONS', 'EVENT_RESPONSES', 'EXTERNAL', 'LEAD_GENERATION',
'LINK_CLICKS', 'LOCAL_AWARENESS', 'MOBILE_APP_ENGAGEMENT',
'MOBILE_APP_INSTALLS', 'OFFER_CLAIMS', 'PAGE_LIKES',
'POST_ENGAGEMENT', 'PRODUCT_CATALOG_SALES', 'VIDEO_VIEWS'))
params$campaign_status <- match.arg(campaign_status)

}
buying_type = buying_type,
objective = objective,
name = name,
execution_options = execution_options,
spend_cap = spend_cap,
objective = match.arg(objective),
campaign_status = match.arg(campaign_status))

## drop NULL args
params <- as.list(unlist(params, recursive = FALSE))

## get results
res <- fbad_request(fbacc,
path = paste0('act_', fbacc$account_id,
ifelse(fb_api_version() < '2.5',
'/adcampaign_groups', '/campaigns')),
method = "POST",
params = params)
path = paste0('act_', fbacc$account_id, '/campaigns'),
method = "POST",
params = params)

## return Ad Campaign ID on success
fromJSON(res)$id
Expand Down

0 comments on commit d7f0acc

Please sign in to comment.