This is a simple (starter) example for pulling data using the v2.0 Reporting API for Adobe Analytics.

Background and Resources

The goal of this example is to simply “pull a simple table of data.” Because of the way the v2.0 API works, to keep it simple, this code is limited to a single dimension and a single metric.

This requires a different kind of API access than was required for the v1.4 API (which simply required the user to have Web services access, and then just used the client ID and secret from that). So, you will need to have – or have the assistance of someone who has – Adobe I/O Console admin access.

What’s needed (each of these values are plugged into the first code snippet below):

Once you’ve got those values, just plug them into the code below, and you just might be off to the races.

Basic Settings

Update the settings in the snippet below to specify the parameters of the query.

# Load the necessary libraries. 
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, jsonlite, httr, DT)

#####
# NOTE: Sys.getenv() is useful for storing credentials in .Renviron rather than hard-coding
# them into code that could inadvertently get published. If you want to just hardcode the
# values in, simply replace "Sys.getenv(...)" with the actual value.
#####

# Set the API key from the Adobe I/O console
api_key <- Sys.getenv("ADOBE_API_KEY")

# This is an even bigger pain to get, but it's your company ID
company_id <- Sys.getenv("ADOBE_API_COMPANY_ID")

# Set the report suite ID
aa_rsid <- Sys.getenv("ADOBE_API_RSID")

# This token will expire periodically and will need to be updated. Note that it starts with "Bearer"
# and is the entire string following that in the "CURL REQUEST" of the Analysis Workspace 
# Oberon XML debugger
aa_token <- "Bearer eyJ4NXUiOiJpbXNfbmExLWtleS0xLmNlciIsImFsZyI6IlJTMjU2In0.eyJpZCI6IjE1NjE4MzExMTkyOTVfYWU3NDE5OGQtNDdhOS00ODljLThjZGQtZmIyMzExNmFhOWNjX3VlMSIsImNsaWVudF9pZCI6IlNpdGVDYXRhbHlzdDIiLCJ1c2VyX2lkIjoiRjYxQkE5NTQ1QTdCMzIzRDBBNDk1RDZDQEFkb2JlSUQiLCJzdGF0ZSI6IntcInNlc3Npb25cIjpcImh0dHBzOi8vaW1zLW5hMS5hZG9iZWxvZ2luLmNvbS9pbXMvc2Vzc2lvbi92MS9aR1E1TURZME1qWXRZVE5tWkMwME1HVXdMVGt5TWprdFlXRmxNV0l6TWpNNVlq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MMJf31EY5xIfDWT_GffrEj_eofPcaoCQ"

# Set the start and end dates
start_date <- Sys.Date() - 30
end_date <- Sys.Date() - 1

# Set the dimension and metrics for the report
aa_dim <- "page"
aa_metric <- "pageviews"

# Set the number of rows to return
num_rows <- 50

Pull the Data

# Create the date range string
timeframe <- paste0(start_date, "T00:00:00.000/", end_date + 1, "T00:00:00.000")

# Build the body of the JSON request. This is the messy part, but it's what you can most easily
# tinker with if you view the JSON in the Oberaon XML debugger in Workspace
request_body_json <- sprintf('{
    "rsid": "%s",
    "globalFilters": [
        {
            "type": "dateRange",
            "dateRange": "%s"
        }
    ],
    "metricContainer": {
        "metrics": [
            {
                "columnId": "0",
                "id": "metrics/%s",
                "sort": "desc"
            }
        ]
    },
    "dimension": "variables/%s",
    "settings": {
        "countRepeatInstances": true,
        "limit": %s,
        "page": 0
    },
    "statistics": {
        "functions": [
            "col-max",
            "col-min"
        ]
    }
}', aa_rsid, timeframe, aa_metric, aa_dim, num_rows)

# Buld the request URL.
request_url <- paste0("https://analytics.adobe.io/api/", company_id, "/reports/ranked")

# Actually make the request and then extract the content from it
result <- POST(request_url,
                 body = request_body_json,
                 add_headers(.headers = c("Accept" = "application/json",
                                          "Authorization" = aa_token,
                                          "Content-Type" = "application/json",
                                          "x-api-key" = api_key,
                                          "x-proxy-global-company-id" = company_id))) %>% 
  content() # Extract the content from the result of the call

# Get just the data into a data frame. There may be a more efficient way, but the
# below is essentially lifted (inelegantly -- this source has very clean approaches
# in and of themselves) from https://cfss.uchicago.edu/notes/simplify-nested-lists/
result_df <- map_df(result$rows, magrittr::extract, "value") %>%   # Get the dimension values
  cbind(metric = map_dbl(result$rows, c(3,1)))          # Get the metric values

# Update the column names
names(result_df) = c(aa_dim, aa_metric)

# Output the results as a dynamic table (DT)
datatable(result_df)

The code for this is available on GitHub. Feel free to log an issue there or contact Tim Wilson if you have questions or comments (or if you’re interested in developing / co-developing an R package for the v2.0 API).