R/fb_ads_ga_format.R

Defines functions fb_ads_ga_format

Documented in fb_ads_ga_format

#' fb_ads_ga_format
#'
#' Format your FB ads data for upload to GA
#' @param fb_ads_df The data frame of advertisting data from Facebook
#' @param utm_medium The medium of the campaign, this should match the values you set in your UTM codes in the URLs in the Facebook ads
#' @param utm_source The source of the campaign, this should match the values you set in your UTM codes in the URLs in the Facebook ads
#' @param utm_term The relevant keyword of the campaign, this should match the values you set in your UTM codes in the URLs in the Facebook ads (optional)
#' @param utm_content The ad content of the campaign, this should match the values you set in your UTM codes in the URLs in the Facebook ads (optional)
#' @export

fb_ads_ga_format <- function(fb_ads_df,utm_medium,utm_source,utm_term=NULL,utm_content=NULL) {

require(tidyverse)
require(purrr)

df <- fb_ads_df %>%
  mutate(medium = utm_medium,
         source = utm_source) %>%
  rename(date = date_start,
         campaign = campaign_name,
         adCost = spend,
         adClicks = clicks) %>%
  select(date,medium,source,adClicks,adCost,impressions,campaign) %>%
  mutate(date = str_replace_all(date,'-',''))

if (is_empty(utm_term)==FALSE) {

  df <- df %>%
    mutate(keyword = utm_term)

}

if (is_empty(utm_content)==FALSE) {

  df <- df %>%
    mutate(adContent = utm_content)
}

fbcols <- names(df)
fbcols2 <- paste('ga:',fbcols,sep='')

colnames(df) <- fbcols2

return(df)

}
neugelb/neugelbtools documentation built on July 7, 2020, 1:17 a.m.