次の例のように、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"
フィルター処理している行にありますが、フィルターステートメントを含めたくない場合があるため、その行を完全に削除します。
関数を書いたとき、次のようなことを試しました。
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"
それを操作できるようにする必要があるということです。
フィルタリングする必要がある場合はその行を組み込むことができますが、フィルタリングする必要がない場合はその行は含まれません。