如何在Localytics的R httr查询中包含/排除filter语句
我可以使用R成功地从Localytics查询数据,例如以下示例:如何在Localytics的R httr查询中包含/排除filter语句,r,httr,localytics,R,Httr,Localytics,我可以使用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'),
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)
但是我尝试添加filterDim和filterCriteria只会产生错误不可处理的实体。请记住,我可以根据很多变量进行筛选,而不仅仅是:Item URI,因此我还需要能够对其进行操作
如何包含语句,如果需要筛选,可以合并该行,但如果不需要筛选,则不包括该行?条件只是一个列表,因此可以有条件地向其中添加元素。在这里,我们只使用一个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)
}
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)
}