ganalytics provides functions that makes it easy to define filters and segments using natural R language comparison and logical operators. This example demonstrates how to define dynamic segments using functions from the ganalytics package and using those segments with the googleAnalyticsR package. The current development version of googleAnalyticsR supports segments and filters defined with ganalytics.

Setup/Config

Once installed, load these packages. Please refer to the googleAnalyticsR package documentation on configuration steps you may need to complete in order to use the Google Analytics APIs.

library(googleAnalyticsR)
## 2019-03-02 20:01:49> Set API cache
## 2019-03-02 20:01:49> No environment argument found, looked in GA_AUTH_FILE
library(ganalytics)
## Welcome to ganalytics.
## 
## To get started, use the GoogleApiCreds() function to set credentials for
## authenticating with your Google Analytics API project. E.g:
##  creds <- GoogleApiCreds("<my_user_name>@gmail.com", ".my_app_oauth_creds.json")
## 
## Then, check out the list of available demos and package documentation with:
##  vignette(package = "ganalytics")
##  vignette("README",  "ganalytics")
##  package?ganalytics
##  demo(package = "ganalytics")
##  demo("gademo", "ganalytics")
## 
## For help with authentication, enter:
##  ?ganalytics::GoogleApiCreds
## 
## Please note: Authentication will automatically direct you to Google's website
## where you will be asked to grant permission for your project to access Google
## Analytics - this will open within your default web browser. Unless a file path
## is provided at the time of authentication, tokens will be cached by httr in your
## home directory.
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(purrr)
library(knitr)

ga_auth(file.path("~", "ga.oauth"))
## Token cache file: ~/ga.oauth
view_id <- "117987738"
start_date <- "2018-05-01"
end_date <- "2018-06-30"

Pull the Data

In this example, we'll define a list of six segments:

The above list of dynamic segments is defined using ganalytics expressions as follows:

bounces <- Expr(~bounces != 0)
conversions <- Expr(~goalCompletionsAll > 0) | Expr(~transactions > 0)
mobile_or_tablet <- Expr(~deviceCategory %in% c("mobile", "tablet"))
multi_session_users <- Include(PerUser(Expr(~sessions > 1)), scope = "users")
new_desktop_users <- Expr(~deviceCategory == "desktop") & Expr(~userType == "new")

my_segment_list <- list(
  bounced_sessions = PerSession(bounces),
  mobile_or_tablet = mobile_or_tablet,
  converters = PerUser(conversions),
  multi_session_users = multi_session_users,
  new_desktop_users = new_desktop_users,
  bounced_before_converting = Sequence(bounces, conversions, scope = "users")
)

Because the Google Analytics Reporting API can only be used to query 4 segments at a time, we need to break our list segments into chunks.

segment_chunks <- split(my_segment_list, (seq_along(my_segment_list) - 1L) %/% 4L)

We can now use googleAnalyticsR to query each chunk of segments and bind the results into a single data.frame. For each segment, we will request a count of users and sessions.

results <- map_df(segment_chunks, function(chunk) {
  google_analytics(
    viewId = view_id,
    date_range = c(start_date, end_date),
    metrics = c("users", "sessions"),
    dimensions = c("segment"),
    segments = Segments(chunk)
  )
})
kable(results)
segment users sessions
bounced_sessions 839 991
converters 1066 1388
mobile_or_tablet 322 437
multi_session_users 171 519
bounced_before_converting 667 950
new_desktop_users 962 933

Data Munging and Visualization

We will compare users and sessions for each segment using a horizontal column chart. To do this we need to transform the results table into long format in which the count of users and sessions for each segment are on separate rows.

results_long <- results %>%
  gather(metric, count, users, sessions)
kable(results_long)
segment metric count
bounced_sessions users 839
converters users 1066
mobile_or_tablet users 322
multi_session_users users 171
bounced_before_converting users 667
new_desktop_users users 962
bounced_sessions sessions 991
converters sessions 1388
mobile_or_tablet sessions 437
multi_session_users sessions 519
bounced_before_converting sessions 950
new_desktop_users sessions 933
ggplot(results_long) +
  aes(segment, count, fill = metric) +
  geom_col(position = "dodge") +
  guides(fill = guide_legend(reverse = TRUE)) +
  coord_flip() +
  theme(axis.text.y.left = element_text(hjust = 0))

plot of chunk visualize-results