R/yadirGetReport.R

Defines functions yadirGetReport

Documented in yadirGetReport

yadirGetReport <- function(ReportType        = "CUSTOM_REPORT", 
                           DateRangeType     = "LAST_30_DAYS", 
                           DateFrom          = NULL, 
                           DateTo            = NULL, 
                           FieldNames        = c("CampaignName","Impressions","Clicks","Cost"), 
                           FilterList        = NULL,
                           Goals             = NULL,
                           AttributionModels = NULL,
                           IncludeVAT        = "YES",
                           IncludeDiscount   = "NO",
                           Login             = NULL,
                           AgencyAccount     = NULL,
                           FetchBy           = NULL,
                           Token             = NULL,
                           TokenPath         = getwd()) {
  st <- Sys.time()
  
  # FetchBy
  if (!is.null(FetchBy)) {
    if ((DateRangeType != "CUSTOM_DATE") && (is.null(DateFrom)) && (is.null(DateTo)))
        stop('You should use "FetchBy" parameter only with DateRangeType "CUSTOM_DATE" and existing DateFrom and DateTo.')
    #dates df
    dates <- date_ranges(DateFrom, DateTo, FetchBy)
    n <- nrow(dates)
    packageStartupMessage("Batch processing mode is enabled.")
    packageStartupMessage(paste0("Fetching data by ", tolower(FetchBy), ": from ", as.Date(DateFrom), " to ", as.Date(DateTo), "."))
    packageStartupMessage()
  } else {
    #dates df
    dates <- as.data.frame(list(DateFrom, DateTo), col.names = c("start","end"), stringsAsFactors = F)
    n <- 1
  }

  # result
  result <- data.frame()
  for (i in 1:n) {
    df <- yadirGetReportFun(ReportType, 
                           DateRangeType, 
                           dates[i, "start"], 
                           dates[i, "end"], 
                           FieldNames, 
                           FilterList,
                           Goals,
                           AttributionModels,
                           IncludeVAT,
                           IncludeDiscount,
                           Login,
                           AgencyAccount,
                           Token,
                           TokenPath
    )
    result <- rbind(result, df)
  }
  
  # logs
  if (!is.null(FetchBy)) {
    packageStartupMessage('Function with batch processing mode has executed successfully.')
    packageStartupMessage("Total number of rows is: ", nrow(result))
    packageStartupMessage("Total executing time is: ", round(difftime(Sys.time(), st , units = "secs"), 0), " sec.")
  }
  
  return(result)
}
grkhr/ryandexdirect documentation built on Dec. 10, 2019, 2:28 p.m.