R/af_get_data.R

Defines functions af_get_data

Documented in af_get_data

af_get_data <- function(
  date_from  = Sys.Date() - 8,
  date_to    = Sys.Date() - 1,
  dimensions = c("app_id", "pid", "af_channel", "c", "af_c_id", "geo"),
  metrics    = c("impressions", "clicks", "installs", "sessions", "loyal_users", "cost", "revenue", "uninstalls"),
  filters    = NULL,
  currency   = NULL,
  timezone   = "Europe/Moscow",
  app_id     = getOption("apps_flyer_app_id"),
  api_token  = getOption("apps_flyer_api_key")
) {

  # docs https://support.appsflyer.com/hc/en-us/articles/207034346-Master-API-user-acquisition-metrics-via-API

  # send api call
  retry(
    {
      answer <- GET(
        url = str_glue("https://hq1.appsflyer.com/api/master-agg-data/v4/app/{app_id}"),
        httr::add_headers(Authorization = paste("Bearer", api_token)),
        query = list(
          from      = date_from,
          to        = date_to,
          groupings = str_c(dimensions, collapse = ","),
          kpis      = str_c(metrics, collapse = ","),
          filters   = filters,
          currency  = currency
        )
      )

      # check limit error
      if ( status_code(answer) != 200 ) {

        msg <- ifelse( str_detect(string = content(answer, "text", encoding = "UTF-8"), "^Limit reached.*"), "Limit reached", content(answer, "text", encoding = "UTF-8") )
        lg$error(content(answer, "text", encoding = "UTF-8"))
        stop(msg)

      }

    },
  when      = "Limit reached",
  interval  = 60,
  max_tries = 5

  )

  data <- content(answer, "parsed", "text/csv", encoding = "UTF-8")

  return(data)
}

Try the rappsflyer package in your browser

Any scripts or data that you put into this service are public.

rappsflyer documentation built on May 27, 2026, 9:06 a.m.