Viewing Content Usage Information

Problem

You need to view how people are interacting with your content, including who is viewing your content, how frequently they are viewing it, when they view it, and other information.

Solution

Create a data frame of content usage metrics.

from posit import connect
import polars as pl

client = connect.Client()

usage = client.metrics.usage.find()
usage_df = pl.DataFrame(usage, infer_schema_length=len(usage))
>>> usage_df
shape: (35_087, 9)
┌─────────────────────────────────┬─────────────────────────────────┬─────────────┬──────────────┬───┬──────────────────────┬──────────────────────┬──────────────┬──────┐
│ content_guid                    ┆ user_guid                       ┆ variant_key ┆ rendering_id ┆ … ┆ started              ┆ ended                ┆ data_version ┆ path │
------------          ┆   ┆ ------------
strstrstrstr          ┆   ┆ strstr                  ┆ i64          ┆ str
╞═════════════════════════════════╪═════════════════════════════════╪═════════════╪══════════════╪═══╪══════════════════════╪══════════════════════╪══════════════╪══════╡
2243770d-ace0-4782-87f9-fe2aec… ┆ 434f97ab-4b97-4443-8490-ed1052… ┆ dZnFUBMR    ┆ 3029259      ┆ … ┆ 2023-10-27T16:19:40Z2023-10-27T16:19:40Z3            ┆ null │
6795b1a7-9885-4dfd-ad8a-bc0f2c… ┆ 7c86da77-ec11-4fe4-9fa1-246dd1… ┆ IothZLX9    ┆ 3003691      ┆ … ┆ 2023-10-27T16:21:14Z2023-10-27T16:21:14Z3            ┆ null │
2243770d-ace0-4782-87f9-fe2aec… ┆ 434f97ab-4b97-4443-8490-ed1052… ┆ dZnFUBMR    ┆ 3029265      ┆ … ┆ 2023-10-27T16:23:52Z2023-10-27T16:23:52Z3            ┆ null │
│ f8aa3b0a-f8d8-4683-a6cb-d9e6fb… ┆ 434f97ab-4b97-4443-8490-ed1052… ┆ null        ┆ null         ┆ … ┆ 2023-10-27T16:24:20Z2023-10-27T16:24:20Z3            ┆ null │
80f996ae-9f62-4ad2-9a4b-844160… ┆ 434f97ab-4b97-4443-8490-ed1052… ┆ null        ┆ null         ┆ … ┆ 2023-10-27T16:25:08Z2023-10-27T16:25:08Z3            ┆ null │
│ …                               ┆ …                               ┆ …           ┆ …            ┆ … ┆ …                    ┆ …                    ┆ …            ┆ …    │
│ c0bf0052-3565-473b-973a-d4c3d1… ┆ d339bdd2-42e0-4165-9a90-08622c… ┆ null        ┆ null         ┆ … ┆ 2024-06-24T18:14:52Z2024-06-24T18:15:14Z1            ┆ null │
26f6ffeb-36e1-4184-8173-5f2d58… ┆ d339bdd2-42e0-4165-9a90-08622c… ┆ null        ┆ null         ┆ … ┆ 2024-06-24T18:15:51Z2024-06-24T18:36:32Z1            ┆ null │
│ d672641c-05f9-4709-9792-024c4c… ┆ c392504a-98cf-4906-b9c7-f38136… ┆ null        ┆ null         ┆ … ┆ 2024-06-24T18:17:57Z2024-06-24T18:20:53Z1            ┆ null │
45bf3651-ce0b-4a3f-9a44-a3d23d… ┆ c2250bb4-47ab-4332-839b-a975e8… ┆ null        ┆ null         ┆ … ┆ 2024-06-24T18:41:08Z ┆ null                 ┆ 1            ┆ null │
45bf3651-ce0b-4a3f-9a44-a3d23d… ┆ c2250bb4-47ab-4332-839b-a975e8… ┆ null        ┆ null         ┆ … ┆ 2024-06-24T18:51:16Z ┆ null                 ┆ 1            ┆ null │
└─────────────────────────────────┴─────────────────────────────────┴─────────────┴──────────────┴───┴──────────────────────┴──────────────────────┴──────────────┴──────┘

Usage for Shiny applications is returned by the get_usage_shiny() function. Usage for all other content is returned by get_usage_static(). Renaming the “static” usage time variable to started, we can bind the rows of the resulting data frames.

library(connectapi)
library(dplyr)

client <- connect()

shiny_usage <- get_usage_shiny(client, limit = 5)
static_usage <- get_usage_static(client, limit = 5) |>
  rename(started = time)

usage <- bind_rows(static_usage, shiny_usage)
> usage
# A tibble: 10 × 9
   content_guid                         user_guid          variant_key started             rendering_id bundle_id path  data_version ended              
   <chr>                                <chr>              <chr>       <dttm>              <chr>        <chr>     <lgl>        <int> <dttm>             
 1 4ad40f49-b74d-4454-9211-a4df660b82da 434f97ab-4b97-444… dZnFUBMR    2023-10-27 16:19:40 3029259      49747     NA               3 NA                 
 2 5f10153e-09fd-4daa-b3c9-c3e3ce4817a8 7c86da77-ec11-4fe… IothZLX9    2023-10-27 16:21:14 3003691      41696     NA               3 NA                 
 3 4ad40f49-b74d-4454-9211-a4df660b82da 434f97ab-4b97-444… dZnFUBMR    2023-10-27 16:23:52 3029265      49747     NA               3 NA                 
 4 8ce9bb64-4805-4eb9-8c8f-d9f06b2666c9 434f97ab-4b97-444NA          2023-10-27 16:24:20 NA           46421     NA               3 NA                 
 5 1eb79752-cf15-47d1-888a-5d2e136a1e7c 434f97ab-4b97-444NA          2023-10-27 16:25:08 NA           37787     NA               3 NA                 
 6 5f8660cd-f304-4300-a6b3-962cb5451dae NA                 NA          2023-11-10 15:15:17 NA           NA        NA               1 2023-11-10 15:15:43
 7 5f8660cd-f304-4300-a6b3-962cb5451dae NA                 NA          2023-11-10 15:15:30 NA           NA        NA               1 2023-11-10 15:15:56
 8 774348be-a509-4f12-a9f7-6eebb5168a38 NA                 NA          2023-11-10 15:15:44 NA           NA        NA               1 2023-11-10 15:16:09
 9 571de2dd-be51-40e1-9dae-1c148d633dd2 434f97ab-4b97-444NA          2023-11-10 23:38:18 NA           NA        NA               1 2023-11-10 23:38:45
10 5f8660cd-f304-4300-a6b3-962cb5451dae NA                 NA          2023-11-11 03:15:17 NA           NA        NA               1 2023-11-11 03:15:42

Discussion

Usage data for Shiny applications and other content is reported differently. For instance, only Shiny applications have an ended column. The variant_key, rendering_id, bundle_id, and path variables are not provided for Shiny applications. See below for resources to learn more about these variables.

See also