How to extract custom data from the Google Analytics API

You can use a programming language like R to bypass the Google Analytics Web interface to retrieve the data you want. Here's a step-by-step.

Google Analytics is a useful tool for measuring website usage -- everything from simple page views to the kind of complex ad campaign tracking marketers might need. However, I find the user interface to be, well, less than ideal. The good news is that Google Analytics provides a robust API that enables you to tap into your data programmatically, meaning you can conveniently pull and package data in ways that might not be as easy to do on the Web.

Google has tutorials that cover how to use this feature with Java, Python, PHP and JavaScript, but I prefer to tap into Google Analytics with R, a language that's specifically designed for data visualization and graphical analysis. Versions of R are available for Windows, Mac OS X, and Unix, and you can also get add-on packages for R that can streamline a lot of data work. (If you want to learn R basics, head to Computerworld Beginner's Guide to R.)

You don't need to know R to follow along with the steps here. In fact, after extracting data, you can save it to a CSV file to use in Excel, if you prefer.

Step one: Get R

First, if it's not on your system already, download and install R from the R Project for Statistical Computing website. When you run the R application, you'll see a console window where you can type in text commands. And, of course, make sure you've got a Google Analytics account and some data to work with.

Google Analytics
The R console window is where you can type in commands.

There are several R packages available that have functions specifically designed for Google Analytics, including ganalytics, RGoogleAnalytics and rga ("R Google Analytics"). I'll be using rga for this tutorial, but any of them would work.

Like ganalytics, rga resides on GitHub. To easily install any of the Google Analytics packages from GitHub, first install and load the R package devtools by typing the following commands into the R console window:


Then install and load rga from package author Bror Skardhamar's account:

install_github("rga", "skardhamar")

(You only have to run the first three commands once per machine, but you need to load library(rga) each time you open R.)

Step two: Allow rga to access your Google Analytics account

On a Mac, authentication is as easy: Create an instance of the Google Analytics API authentication object by typing the following in your R console window:"ga")

That will open a browser window that asks you to give rga permission to access your Google data. When you accept, you'll be given a code to cut and paste back into your R console window where it says, "Please enter code here."

In Windows, I find that adding a line of code before opening an rga instance helps with any authentication errors:

options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))"ga")

Next, you need to find the profile ID for your Google account, which is not found in the tracking code that you add to a website to allow Google Analytics to monitor your site. Instead, on your Google Analytics Admin page, go to View Settings and you'll see the ID under "View ID."

Google Analytics
You'll find your profile ID for your Google account by going to View Settings on your Google Analytics Admin page.

Or, run the command


in your R terminal window to get a list of all available profiles in your account; the profile ID will be listed in the first column.

Whichever way you find it, save that value in a variable so you don't have to keep typing it. You can use a command like:

id <- "1234567"

(Replace the number with your actual ID, and make sure to put it between quote marks.) This stores your profile ID as the variable "id."

Step 3: Extract data

Now we're ready to start pulling some data using the ga instance we just created. The getData method will actually extract data from your Google Analytics account that you can then store in another new R variable. If you want to see all available methods for your ga object, run:


You can query the Google API for metrics and dimensions. Metrics are things like page views, visits and organic searches; dimensions include information like traffic sources and visitor type. (See Google's Dimensions & Metrics Reference for full details.)

1 2 3 Page 1
Page 1 of 3
It’s time to break the ChatGPT habit
Shop Tech Products at Amazon