This example pulls visits and pageviews by device type for the last 30 days and then plots the data as a scatterplot. Like a lot of these examples, it’s not super useful. But, it’s got to be a pretty generic example to work in a straight copy/paste world. Scatterplots can definitely be useful!
Be sure you’ve completed the steps on the Initial Setup page before running this code.
For the setup, we’re going to load a few libraries, load our specific Adobe Analytics credentials, and then authorize with Adobe.
# Load the necessary libraries. The 'typical' way to do this is with a series of
# 'library([library name])' calls. The use of the pacman package, though, adds a
# check that will install any missing packages before then loading them.
if (!require("pacman")) install.packages("pacman")
pacman::p_load(RSiteCatalyst,
tidyverse,
jsonlite) # Needed for unbox() in examples with inline segments
# Load the username, shared secret, and report suite ID
username <- Sys.getenv("ADOBE_API_USERNAME")
secret <- Sys.getenv("ADOBE_API_SECRET")
# Authorize Adobe Aalytics.
SCAuth(username, secret)
# Set the RSID and the date range. If you want to, you can swap out the Sys.getenv()
# call and just replace that with a hardcoded value for the RSID. And, the start
# and end date are currently set to choose the last 30 days, but those can be
# hardcoded as well.
rsid <- Sys.getenv("ADOBE_RSID")
start_date <- Sys.Date() - 31 # 30 days back from yesterday
end_date <- Sys.Date() - 1 # Yesterday
If that all runs with just some messages but no errors, then you’re set for the next chunk of code: pulling the data.
This is a simple query.
# Pull the data. See ?QueueTrended() for additional parameters.
aa_data <- QueueTrended(rsid,
date.from = start_date,
date.to = end_date,
metrics = c("pageviews", "visits"),
elements = "mobiledevicetype",
date.granularity = "day")
# Go ahead and do a quick inspection of the data that was returned. This isn't required,
# but it's a good check along the way.
head(aa_data)
datetime | name | url | pageviews | visits | segment.id | segment.name |
---|---|---|---|---|---|---|
2018-02-19 | Other | 267 | 148 | |||
2018-02-19 | Mobile Phone | 20 | 17 | |||
2018-02-19 | Tablet | 0 | 0 | |||
2018-02-20 | Other | 456 | 228 | |||
2018-02-20 | Mobile Phone | 102 | 76 | |||
2018-02-20 | Tablet | 0 | 0 |
There’s not a ton of data munging to be done here. We’ll change the date to be Date
instead of POSIXlt
to play nice with the plotting, and we’ll grab just the columns we need to do the data visualization. And, for giggles, we’ll change “Other” to be “Desktop/Laptop.”
# Convert the datetime from POSIXlt to Date
aa_data$datetime <- as.Date(aa_data$datetime)
# Clean up the table a bit
aa_data <- aa_data %>%
mutate(Device = ifelse(name=="Other", "Desktop/Laptop", name)) %>%
select(datetime, Device, visits, pageviews)
# Check what it looks like now
head(aa_data)
datetime | Device | visits | pageviews |
---|---|---|---|
2018-02-19 | Desktop/Laptop | 148 | 267 |
2018-02-19 | Mobile Phone | 17 | 20 |
2018-02-19 | Tablet | 0 | 0 |
2018-02-20 | Desktop/Laptop | 228 | 456 |
2018-02-20 | Mobile Phone | 76 | 102 |
2018-02-20 | Tablet | 0 | 0 |
This won’t be the prettiest plot, but this example isn’t diving into the details of ggplot2. If you want to read up on that, this page on dartistics.com is worth checking out.
# Create the plot.
gg <- ggplot(aa_data, mapping = aes(x = visits, y = pageviews, colour = Device)) +
geom_point() +
theme_light()
# Output the plot. You *could* just remove the "gg <-" in the code above, but it's
# generally a best practice to create a plot object and then output it, rather than
# outputting it on the fly.
print(gg)
This site is a sub-site to dartistics.com