Skip to content
This repository was archived by the owner on Mar 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 11 additions & 31 deletions R/fb_ad.R
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
#' Create ad
#' Create Ad
#' @inheritParams fbad_request
#' @param name Ad group name
#' @param adset_id Ad Set id
#' @param creative_id creative ID
#' @param status initial status of the Ad group
#' @param ... further parameters passed to the Facebook API
#' @param ... further parameters passed to the API, see below references
#' @return ad id
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/adgroup#Creating}
fbad_create_ad <- function(fbacc,
name,
adset_id,
creative_id,
status = c('ACTIVE', 'PAUSED'),...) {
fbad_create_ad <- function(fbacc, ...) {

fbacc <- fbad_check_fbacc()
stopifnot(!missing(name), !missing(adset_id), !missing(creative_id))

## initial status of the ad to be created
status <- match.arg(status)
## lookup caller fn name and the API endpoint based on that
endpoint <- switch(this_function_name(),
'fbad_create_ad' = 'ads',
'fbad_create_adset' = 'adsets',
'fbad_create_campaign' = 'campaigns')

## build params list
params <- list(
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) {
params <- c(params, list(...))
}

## get results
res <- fbad_request(fbacc,
path = paste0('act_', fbacc$account_id, '/ads'),
res <- fbad_request(fbad_check_fbacc(),
path = paste0('act_', fbacc$account_id, '/', endpoint),
method = "POST",
params = params)
params = list(...))

## return campaign ID on success
fromJSONish(res)$id
Expand Down
118 changes: 2 additions & 116 deletions R/fb_adset.R
Original file line number Diff line number Diff line change
@@ -1,123 +1,9 @@
#' Create Ad Set
#' @inheritParams fbad_request
#' @param name name of the Ad Set
#' @param optimization_goal optimization goal
#' @param billing_event the billing event
#' @param is_autobid logical. If \code{TRUE}, autobid is set and you do not need to specify \code{bid_amount}
#' @param bid_amount integer
#' @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
#' @param start_time UTC UNIX timestamp
#' @param targeting list
#' @param ... further arguments passed to the API endpoint
#' @inheritParams fbad_create_ad
#' @return Ad Set id
#' @export
#' @references \url{https://developers.facebook.com/docs/marketing-api/reference/ad-campaign#Creating}
fbad_create_adset <- function(fbacc,
name,
optimization_goal = c('NONE', 'APP_INSTALLS', 'CLICKS', 'ENGAGED_USERS', 'EXTERNAL', 'EVENT_RESPONSES', 'IMPRESSIONS', 'LINK_CLICKS', 'OFFER_CLAIMS', 'OFFSITE_CONVERSIONS', 'PAGE_ENGAGEMENT', 'PAGE_LIKES', 'POST_ENGAGEMENT', 'REACH', 'SOCIAL_IMPRESSIONS', 'VIDEO_VIEWS'),
billing_event = c('APP_INSTALLS', 'CLICKS', 'IMPRESSIONS', 'LINK_CLICKS', 'OFFER_CLAIMS', 'PAGE_LIKES', 'POST_ENGAGEMENT', 'VIDEO_VIEWS'),
is_autobid = FALSE, bid_amount,
promoted_object,
campaign_id,
status = c('ACTIVE', 'PAUSED', 'ARCHIVED', 'DELETED'),
daily_budget, lifetime_budget,
end_time, start_time,
targeting,
...) {

fbacc <- fbad_check_fbacc()

optimization_goal <- match.arg(optimization_goal)
billing_event <- match.arg(billing_event)

## update args for the first or selected value
status <- match.arg(status)

## match call for future reference
mc <- match.call()

## we need a name
if (missing(name)) {
stop('Ad Set name is required.')
}

## we need a campaign_group_id
if (missing(campaign_id)) {
stop('A campaign ad ID is required.')
}

## verify that we have targeting info
if (missing(targeting)) {
stop('A targeting spec is required.')
}

## build base params list
params <- list(
name = name,
optimization_goal = optimization_goal,
billing_event = billing_event,
campaign_id = campaign_id,
configured_status = match.arg(status))
## option for auto bidding
if (is_autobid) {
params$is_autobid <- TRUE
} else {
params$bid_amount <- bid_amount
}

## end_time for lifetime budget
if (!missing(lifetime_budget) && missing(end_time)) {
stop('End time of the ad set is required when using a lifetime budget.')
}

## we need a budget
if (missing(daily_budget) && missing(lifetime_budget)) {
stop('Either a lifetime_budget or a daily_budget must be set.')
}
if (!missing(daily_budget) && !missing(lifetime_budget)) {
stop('Only one of lifetime_budget or daily_budget should be set.')
}
if ((!missing(daily_budget) && !is.numeric(daily_budget)) ||
!missing(lifetime_budget) && !is.numeric(lifetime_budget)) {
stop('Numeric value needed for the budget.')
}
if (missing(daily_budget)) {
params$lifetime_budget <- lifetime_budget
params$end_time <- end_time
} else {
params$daily_budget <- daily_budget
}

## promoted object based on parent campaign
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.'))
}

## start time if provided
if (!missing(start_time)) {
params$start_time <- start_time
}

## transform lists to JSON
params$targeting <- toJSON(targeting, auto_unbox = TRUE)

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

## return campaign ID on success
fromJSONish(res)$id

}
fbad_create_adset <- fbad_create_ad


#' Read Ad Set details
Expand Down
53 changes: 3 additions & 50 deletions R/fb_campaign.R
Original file line number Diff line number Diff line change
@@ -1,56 +1,9 @@
#' 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
#' @param execution_options special execution settings passed to the API
#' @param name Ad Campaign name
#' @param objective the campaign's objective
#' @param spend_cap spend cap of the campaign
#' Create Ad Campaign
#' @inheritParams fbad_create_ad
#' @return Ad Campaign id
#' @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_status = c('ACTIVE', 'PAUSED'),
execution_options = NULL, name,
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()
if (missing(name))
stop('A campaign name is required.')

buying_type <- match.arg(buying_type)

## build params list
params <- list(
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, '/campaigns'),
method = "POST",
params = params)

## return Ad Campaign ID on success
fromJSONish(res)$id

}
fbad_create_campaign <- fbad_create_ad


#' Read Ad Campaign details
Expand Down
17 changes: 4 additions & 13 deletions man/fbad_create_ad.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 2 additions & 37 deletions man/fbad_create_adset.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 4 additions & 21 deletions man/fbad_create_campaign.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.