Interactively querying Google Analytics reports

Johann de Boer

2018-06-07

ganalytics

Travis-CI Build Status

Classes and methods for interactive use of the Google Analytics core reporting, real-time reporting, multi-channel funnel reporting, metadata, configuration management and Google Tag Manager APIs.

The aim of this package is 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.

This package provides functions for querying the Google Analytics core reporting, real-time reporting, multi-channel funnel reporting and management APIs, as well as the Google Tag Manager API. Write methods are also provided for the Google Analytics Management and Google Tag Manager APIs so that you can, for example, change tag, property or view settings.

Updates

Support for GoogleAnalyticsR integration is now available for segments and table filter objects. You can supply these objects to the google_analytics function in GoogleAnalyticsR by using as(), supplying the appropriate GoogleAnalyticsR class names, which are "segment_ga4" for segments and ".filter_clauses_ga4" for table filters. Soon GoogleanalyticsR will implictly coerce ganalytics segments and table filters so that you do not need to explictly coerce using as().

Many new functions have been provided for writing segmentation expressions:

Multi-channel funnel (MCF) and real-time (RT) queries can now be constructed, but work is still needed to process the response from these queries - stay tuned for updates on this.

Instead of using Or, And, and Not, it is now possible to use familiar R language Boolean operators, | (Or), & (And), and ! (Not) instead (thanks to @hadley for suggestion #2). It is important to keep in mind however that Google Analytics requires Or to have precedence over And, which is the opposite to the natural precedence given by R when using the | and & operators. Therefore, remember to use parentheses ( ) to enforce the correct order of operation to your Boolean expressions. For example my_filter <- !bounced & (completed_goal | transacted) is a valid structure for a Google Analytics reporting API filter expression.

You can use query the Google Analytics Management API to obtain details in R about the configuration of your accounts, properties and views, such as goals you have defined. There are write methods available too, but these have not been fully tested so use with extreme care. If you wish to use these functions, it is recommended that you test these using test login, otherwise avoid using the “INSERT”, “UPDATE” and “DELETE” methods.

There is also some basic support for the Google Tag Manager API, but again, this is a work in progress so take care with the write methods above.

Installation

1. Install the necessary packages into R via the GitHub repository

Prerequisites

  • Ensure you have installed the latest version of R

2. Prepare your Google API application (you only need to do this once)

  • Browse to [Google API Console] (https://code.google.com/apis/console/)
  • Check you are signed into Google with the account you wish to use.
  • Choose Create Project from the Google API Console and give your project a name (or choose an existing project if you have one already).
  • From the APIs page, enable the Analytics API. You may also want to enable the Tag Manager API if you wish to try that.
  • You will need to agree and accept the Google APIs and Analytics API Terms of Service to proceed.
  • Go to the Credentials page, click Add credentials, choose OAuth 2.0 client ID, then select “Other”.
  • Note your Client ID and Client Secret and download the JSON file to your R working directory.

Note: For further information about Google APIs, please refer to the References section at the end of this document.

4. Authenticate and attempt your first query with ganalytics

  • ganalytics needs to know the ID of the Google Analytics view that you wish to query. You can obtain this in a number of ways:
    • Using the Google Analytics Query Explorer tool
    • From the Admin page in Google Analytics under View Settings, or
    • The browser’s address bar while viewing a report in Google Analytics - look for the digits between the letter ‘p’ and trailing ‘/’, e.g. .../a11111111w22222222p33333333/ shows a view ID of 33333333.
  • Alternatively, ganalytics can look up the view ID for you:
    • If you have access to only one Google Analytics account, with one property, then ganalytics will automatically select the default view for you from that property.
    • Otherwise it will select the default view of the first property from the first account that it finds in the list of accounts that you have access to.
  • Return to R and execute the following to load the ganalytics package:

  • If you have successfully set your system environment variables in step 3 above, then you can execute the following, optionally providing the email address you use to sign-in to Google Analytics:

  • Otherwise do one of the following:
  • Now formulate and run your Google Analytics query, remembering to substitute view_id with the view ID you wish to use:

  • You should then be directed to accounts.google.com within your default web browser asking you to sign-in to your Google account if you are not already. Once signed-in you will be asked to grant read-only access to your Google Analytics account for the Google API project you created in step 1.
  • Make sure you are signed into the Google account you wish to use, then grant access by selecting “Allow access”. You can then close the page and return back to R.

If you have successfully executed all of the above R commands you should see the output of the default ganalytics query; sessions by day for the past 7 days. For example:

        date sessions
1 2015-03-27     2988
2 2015-03-28     1594
3 2015-03-29     1912
4 2015-03-30     3061
5 2015-03-31     2609
6 2015-04-01     2762
7 2015-04-02     2179
8 2015-04-03     1552

Note: A small file will be saved to your home directory (‘My Documents’ in Windows) to cache your new reusable authentication token.

Examples

As demonstrated in the installation steps above, before executing any of the following examples:

  1. Load the ganalytics package
  2. Generate a gaQuery object using the GaQuery() function and assigning the object to a variable name such as myQuery.

Asumptions

The following examples assume you have successfully completed the above steps and have named your Google Analytics query object: myQuery.

Thanks to:

Useful references

  1. Google Analytics Core Reporting API reference guide
  2. Google Analytics Dimensions and Metrics reference
  3. Creating a Google API project
  4. Generating an OAuth 2.0 client ID for Google APIs
  5. Using OAuth 2.0 for Installed Applications
  6. EnvPane utility for setting environment variables in OSX
  7. Setting environment variables in Ubuntu Linux

Notes

Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Google Analytics and Google Tag Manager are trademarks of Google.