User Activity

RStudio Connect records different types of user activity for different types of content.

  1. Content - records information about each visit.

    This includes all content types other than Shiny applications.

    The GET /instrumentation/content/visits API gives details about visits to content.

    Sample code is in this recipe.

  2. Shiny applications - records information about each visit and the length of that visit.

    The GET /instrumentation/shiny/usage API provides details about Shiny application sessions.

    Sample code is in this recipe.

An R Markdown dashboard using instrumentation APIs can be found in the sol-eng/usage GitHub repository.

User Activity: Shiny Applications

This recipe uses the GET /instrumentation/shiny/usage Shiny usage API to page through Shiny application usage. Information is returned in pages following the keyset pagination model.

The keyset pagination recipe explains how to perform multiple, paged requests.

  1. Obtain the server URL and API Key from environment variables.
  2. Call the "Get shiny app usage" endpoint to get the first page. See the API Documentation for more information.
  3. Parse the response using httr::content.
  4. Print the current page.
  5. Repeat steps 1 through 3 until there are no more results. Note also that the paging.next property in the response is the URL of the next page.
library(httr)

# The CONNECT_SERVER URL must have a trailing slash.
connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

# Request a page of up to 25 usage records.
resp <- GET(
  paste0(connectServer, "__api__/v1/instrumentation/shiny/usage?limit=25"),
  add_headers(Authorization = paste("Key", connectAPIKey))
)
payload <- content(resp)
# print the current page results
print(payload$results)

# Continue to page through additional records
# while we have a "next" reference
while(!is.null(payload$paging[["next"]])) {
  resp <- GET(
    payload$paging[["next"]],
    add_headers(Authorization = paste("Key", connectAPIKey))
  )
  payload <- content(resp)
  # print the results on this page
  print(payload$results)
}

User Activity: Content

This recipe uses the GET /instrumentation/content/visits content visit API to page through information about content visits. Information is returned in pages following the keyset pagination model.

The keyset pagination recipe explains how to perform multiple, paged requests.

  1. Obtain the server URL and API Key from environment variables.
  2. Call the "Get content visits" endpoint to get the first page. See the API Documentation for more information.
  3. Parse the response using httr::content.
  4. Print the current page.
  5. Repeat steps 1 through 3 until there are no more results. Note also that the paging.next property in the response is the URL of the next page.
library(httr)

# The CONNECT_SERVER URL must have a trailing slash.
connectServer <- Sys.getenv("CONNECT_SERVER")
connectAPIKey <- Sys.getenv("CONNECT_API_KEY")

# Request a page of up to 25 visitation records.
resp <- GET(
  paste0(connectServer, "__api__/v1/instrumentation/content/visits?limit=25")
  add_headers(Authorization = paste("Key", connectAPIKey))
)
payload <- content(resp)
# print the current page results
print(payload$results)

# Continue to page through additional records
# while we have a "next" reference
while(!is.null(payload$paging[["next"]])) {
  resp <- GET(
    payload$paging[["next"]],
    add_headers(Authorization = paste("Key", connectAPIKey))
  )
  payload <- content(resp)
  # print the results on this page
  print(payload$results)
}