如何在 Localytics 的 R httr 查询中包含/排除过滤语句
How to include / exclude filter statement in R httr query for 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'),
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)
但我想做的是创建一个函数,这样我就可以快速完成此操作,而不必 copy/paste 数据。
我 运行 遇到的问题是第 "a:Item URI" = "testing"
行,我在其中按项目 URI 过滤所有搜索,它们都等于 "testing"
,但有时,我不' 想要包含过滤语句,所以我完全删除了该行。
当我编写我的函数时,我尝试了如下操作:
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"
所以我也需要能够操纵它。
如何包含语句,如果我需要过滤,我可以合并该行,但如果我不需要过滤,则不包含该行?
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)
}
我可以使用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)
但我想做的是创建一个函数,这样我就可以快速完成此操作,而不必 copy/paste 数据。
我 运行 遇到的问题是第 "a:Item URI" = "testing"
行,我在其中按项目 URI 过滤所有搜索,它们都等于 "testing"
,但有时,我不' 想要包含过滤语句,所以我完全删除了该行。
当我编写我的函数时,我尝试了如下操作:
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"
所以我也需要能够操纵它。
如何包含语句,如果我需要过滤,我可以合并该行,但如果我不需要过滤,则不包含该行?
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)
}