This example shows an alternative way of defining dimension or metric filters by using functions from the ganalytics package. The current development version of googleAnalyticsR supports filters defined with ganalytics. ganalytics provides functions that makes it easy to define filters using natural R language operators. As of the most recent update to this site, this requires a “dev” (development) version of two packages(both packages exist on CRAN, but they require the dev versions to use the examples here):

This actually means this example has the incidental educational value of showing how to use packages from GitHub! You can read up on the purpose of ganalytics – developed by Johann de Boeron GitHub. But, in short, it’s intended to “support R users in defining reporting queries using natural R expressions instead of being concerned about API technical intricacies like query syntax, character code escaping and API limitations.”

Setup/Config

This example requires development versions of the googleAnalyticsR (>=0.5.0.9000) and ganalytics (>=0.10.4.9000) R packages available on GitHub, so the setup code below is a bit different (it has some additional code for loading a couple of packages from GitHub).

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 Google Analytics credentials, and then authorize with Google.

# Load the necessary libraries. These libraries aren't all necessarily required for every
# example, but, for simplicity's sake, we're going ahead and including them in every example.
# The "typical" way to load these is simply with "library([package name])." But, the handy
# thing about using the approach below -- which uses the pacman package -- is that it will
# check that each package exists and actually install any that are missing before loading
# the package.
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse,         # Includes dplyr, ggplot2, and others; very key!
               devtools,          # Generally handy
               googleVis,         # Useful for some of the visualizations
               scales)            # Useful for some number formatting in the visualizations

# A function to check that a sufficiently current version of a specific package is 
# installed and loaded. This isn't particularly elegant, but it works.
package_check <- function(package, min_version, github_location){
  # Check if ANY version of the package is installed. This is clunky, but p_load_current_gh
  # wasn't playing nice (and this does need some conditional checking.)
  if(package %in% rownames(installed.packages())){
    # IF a version of the package is already installed, then check the *version* of that
    # package to make sure it's current enough. If it'snot, then re-install from GitHub
    if(packageVersion(package) < min_version) {
      devtools::install_github(github_location)
    }
  } else {
    devtools::install_github(github_location)
  }
  # Load the package
  library(package, character.only = TRUE)
}

# As needed, install and load googleAnalyticsR and ganalytics from GitHub
package_check("googleAnalyticsR", "0.5.0.9001", "MarkEdmondson1234/googleAnalyticsR")
package_check("ganalytics", "0.10.4.9000", "jdeboer/ganalytics")

# Authorize GA. Depending on if you've done this already and a .ga-httr-oauth file has
# been saved or not, this may pop you over to a browser to authenticate.
ga_auth(token = ".ga-httr-oauth")

# Set the view ID 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 view ID. And, the start 
# and end date are currently set to choose the last 30 days, but those can be 
# hardcoded as well.
view_id <- Sys.getenv("GA_VIEW_ID")
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.

Pull the Data

In this example, we’ll define the following filters: * Device category is desktop or tablet - a dimension filter using an OR condition. * New visitors using either a desktop or tablet device - a dimension filter involving both an AND and an OR condition. * At least one goal completion or transaction - a metric filter using an OR condition.

The above list of filters will be defined using ganalytics expressions as follows:

# Device category is desktop or tablet - a dimension filter using an OR condition.
desktop_or_tablet <- Expr(~deviceCategory == "desktop") | Expr(~deviceCategory == "tablet")

# New visitors using either a desktop or tablet device - a dimension filter involving both an AND and an OR condition.
new_desktop_and_tablet_visitors <- Expr(~userType == "new") & desktop_or_tablet

# At least one goal completion or transaction - a metric filter using an OR condition.
at_least_one_conversion <- Expr(~goalCompletionsAll > 0) | Expr(~transactions > 0)

We can now use googleAnalyticsR to pull the data. Chances are, the table below will only show a desktop row. That’s because the site we use for this doesn’t have a ton of traffic, and it rarely gets tablet conversions! But, trust us, this would return multiple rows on a site with more traffic and conversions.

# Pull the data
results <- google_analytics(
  viewId = view_id,
  date_range = c(start_date, end_date),
  metrics = c("users", "sessions", "goalCompletionsAll", "transactions"),
  dimensions = c("deviceCategory", "userType"),
  dim_filters = new_desktop_and_tablet_visitors,
  met_filters = at_least_one_conversion
)

# Display the results
results
deviceCategory userType users sessions goalCompletionsAll transactions
desktop New Visitor 3433 3416 12 0

This site is a sub-site to dartistics.com