0

我可以使用 R 成功地从 Localytics 查询数据,例如以下示例:

r <- POST(url = "https://api.localytics.com/v1/query,
           body=list(app_id=<APP_ID>,
                     metrics=c("occurrences","users"),
                     dimensions=c('a:URI'),
                     conditions=list(day = c("between", "2020-02-11", "2020-03-12"),
                                     event_name = "Content Viewed",
                                     "a:Item URI" = "testing")
           ),
           encode="json",
           authenticate(Key,Secret),
           accept("application/json"),
           content_type("application/json"))
stop_for_status(r)

但我想做的是创建一个函数,这样我就可以快速完成这项工作,而不必复制/粘贴数据。

我遇到的问题是行"a:Item URI" = "testing",我通过 Item URI 过滤所有搜索,它们都相等"testing",但有时,我不想包含 filter 语句,所以我只是完全删除了该行。

当我编写我的函数时,我尝试了以下内容:

get_localytics <- function(appID, metrics, dimensions, from = Sys.Date()-30, 
                           to = Sys.Date(), eventName = "Content Viewed",
                           Key, Secret, filterDim = NULL, filterCriteria = NULL){

  r <- httr::POST(url = "https://api.localytics.com/v1/query",
                  body = list(app_id = appID,
                              metrics = metrics,
                              dimensions = dimensions,
                              conditions = list(day = c("between", as.character(from), as.character(to)),
                                                event_name = eventName,
                                                filterDim = filterCriteria)
                              ),
                  encode="json",
                  authenticate(Key, Secret),
                  accept("application/json"),
                  content_type("application/json"))
  stop_for_status(r)
  result <- paste(rawToChar(r$content),collapse = "")  
  document <- fromJSON(result)                                      
  df <- document$results
  return(df)
}

但是我尝试添加filterDim并且filterCriteria只产生错误Unprocessable Entity。(请记住,我可以过滤很多变量,而不仅仅是"a:Item URI"我需要能够操纵它。

我如何包含一个语句,如果我需要过滤,我可以合并该行,但如果我不需要过滤,则不包含该行?

4

1 回答 1

1

conditions只是一个列表,因此您可以有条件地向其中添加元素。在这里,我们只是使用一个if语句来测试值是否通过,如果是,则将它们添加进去。

get_localytics <- function(appID, metrics, dimensions, from = Sys.Date()-30, 
                           to = Sys.Date(), eventName = "Content Viewed",
                           Key, Secret, filterDim = NULL, filterCriteria = NULL){

  conditions <- list(day = c("between", as.character(from), as.character(to)),
                     event_name = eventName)
  if (!is.null(filterDim)  & !is.null(filterCriteria)) {
    conditions[[filterDim]] <- filterCriteria)
  }

  r <- httr::POST(url = "https://api.localytics.com/v1/query",
                  body = list(app_id = appID,
                              metrics = metrics,
                              dimensions = dimensions,
                              conditions = conditions),
                  encode="json",
                  authenticate(Key, Secret),
                  accept("application/json"),
                  content_type("application/json"))
  stop_for_status(r)
  result <- paste(rawToChar(r$content),collapse = "")  
  document <- fromJSON(result)                                      
  df <- document$results
  return(df)
}
于 2020-03-12T20:13:10.677 回答