Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dantonnoriega/b3045837832bbe10d10aa0d00a055f8b to your computer and use it in GitHub Desktop.
Save dantonnoriega/b3045837832bbe10d10aa0d00a055f8b to your computer and use it in GitHub Desktop.
an example of pulling total and unique visits for a specific Rstudio Connect application Raw
# The CONNECT_SERVER URL must have a trailing slash.
connectServer <- "https://some-rsc-endpont.com"
connectAPIKey <- Sys.getenv("RSC_API_KEY")
# the content GUID of interest
content_guid_3pc <- "0a0b1c2d-1234-4321-a1c2-5678aaa9d9bb" # I made this one up
resp <- httr::GET(
paste0(connectServer, "__api__/v1/instrumentation/shiny/usage?limit=100"),
httr::add_headers(Authorization = paste("Key", connectAPIKey)),
query = list(
content_guid = content_guid_3pc)
)
payload <- httr::content(resp, encoding = 'UTF-8')
# get initial load of results
results <- payload$results
# Continue to page through additional records
# while we have a "next" reference
while(!is.null(payload$paging[["next"]])) {
resp <- httr::GET(
payload$paging[["next"]],
httr::add_headers(Authorization = paste("Key", connectAPIKey))
)
payload <- httr::content(resp, encoding = 'UTF-8')
results <- append(results, payload$results)
}
# as.data.table has better handling of NULL list elements
# - results in ERROR: as.data.frame(list(value = 3, endpoint = NULL))
# - works but drops NULL: data.table::as.data.table(list(value = 3, endpoint = NULL))
l = lapply(results, data.table::as.data.table)
d = data.table::rbindlist(l, T, T)
# unique users by ym, content -----------
d[, ym := format(lubridate::ymd_hms(started), '%Y%m')]
rdx = d[, .(n_user_visits = .N), by = .(ym, content_guid, user_guid)]
unq = rdx[, .(n_uniq_users = .N, n_user_vists = sum(n_user_visits)), by = .(ym, content_guid)]
unq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment