R/count.R

#' count allows you to query a Keen.io event collection by counting occurances, as you could in the browser

count <- function(event_collection, timeframe, filters = NULL, grouping = NULL, interval = NULL){
  requireNamespace("httr")

  read_key <- keenr::read_key()
  project_id <- keenr::project_id()

  body <- list(
    event_collection = event_collection,
    timeframe = timeframe
  )

  if (!is.null(filters)){
    body <- append(body, filters)
  }

  if(!is.null(grouping)){
    body <- append(body, grouping)
  }

  if(!is.null(interval)){
    body <- append(body, interval)
  }


  response <- httr::POST(
    url = "https://api.keen.io",
    path = paste0("3.0/projects/", project_id, "/queries/count"),
    httr::add_headers(
      "content-type" = "application/json",
      Authorization = read_key
    ),
    body = body,
    encode = "json",
    httr::verbose()

  )

  return(httr::content(response, "parsed")$result)
}
plduffy/keenr documentation built on May 17, 2019, 8:19 p.m.