User Activity

Posit Connect records information about visits to hosted content. Shiny applications and other content types track different details about those visits. This page contains recipes for both kinds of activity data.

In addition to the recipes provided here, an R Markdown dashboard using the instrumentation APIs can be found in the sol-eng/connect-usage GitHub repository.

User Activity: Shiny Applications

Connect records information about each visit to a Shiny application and the length of that visit.

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

Note

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

Workflow

  1. Obtain the Posit Connect server URL and API Key from environment variables.
  2. Call the GET /v1/instrumentation/shiny/usage endpoint to get the first page.
  3. Parse the response using httr::content.
  4. Print the current page.
  5. The paging.next response field indicates the URL for the next page. Load, parse, and print each additional page until paging.next is NULL.

Here is an example of the workflow:

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

Connect records information about each visit for non-Shiny application content.

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

Note

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

Workflow

  1. Obtain the server URL and API Key from environment variables.
  2. Call the GET /v1/instrumentation/content/visits endpoint to get the first page.
  3. Parse the response using httr::content.
  4. Print the current page.
  5. The paging.next response field indicates the URL for the next page. Load, parse, and print each additional page until paging.next is NULL.

Here is an example of the workflow:

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)
}