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.

1 2 3 Page 2
Page 2 of 3

In addition, you can focus your query by criteria like visits from search, visits with conversions (assuming you've set that up in Google Analytics beforehand) and even visits just from tablets, by including segments in a query. Finally, you can also create your own filters to narrow your results.

Google Analytics
Google's Query Explorer helps you figure out what data is available and how to structure a query.

Google has created a Query Explorer for the Google Analytics API. It's a great resource to help you figure out what data is available and how to structure a query. If you're new to the Google Analytics API, play around with Query Explorer for a bit to see what data you can extract and the variables you need to pull the data you want. Further information on the terms to use for various queries is available in the API documentation.

Once you decide on what you'd like to include in your query, here's the syntax for using R to get the data:

myresults <- ga$getData(id,"","",
metrics = "",
dimensions = "",
sort = "",
filters = "",
segment = "",
start = 1,
max = 1000)

You fill in information for your specific query between the various quotation marks, of course. Note that dates are in the format yyyy-mm-dd, such as "2013-10-30."

Here's a specific example: Say I want to see the top ten referrers for visits to my site in September. My start date is September 1 and my end date is September 30. My metric is visits -- called "ga:visits" by the API -- and my dimension is their sources -- called "ga:source."

I'll further refine the query to get just my top 10 referrers:

myresults <- ga$getData(id,"2013-09-01","2013-09-30",
metrics = "ga:visits",
dimensions = "ga:source",
sort = "-ga:visits",
start = 1, max = 10)

Here's a breakdown of that query:

  • ga$getData is using the getData method of my ga Google Analytics API-accessing object.
  • The first argument, id, is the profile number for my account, which I already stored in a variable called id.
  • Next are the start and end dates for my query, followed by the metric I want ("ga:visits")).
  • Since I want to know the visits by source, I specify the dimension as "ga.source".
  • I only want the top 10 referrers, so I need to sort the ga:visits results in descending order. I do that on the next line by putting a minus sign in front of ga:visits when setting the sort criteria.
  • Finally I specifically ask to start at the first result with a maximum of 10 to return at most 10 listings.

The results are stored in the variable myresults. Type


at the R prompt in your R terminal window to see what data has been returned.

Google Analytics
The results from a query searching for a site's top 10 referrers.

If I wanted to see the overall number of visits without breaking it down by source, I wouldn't include the dimensions, sort, start or max in the query. Instead, I'd just use a simple:

myresults <- ga$getData(id,"2013-09-01","2013-09-30", metrics = "ga:visits")

That returns a listing of number of visits per day. I can have it return results by different time periods by adding the time dimension of my choice -- for example by week:

myresultsPVsByWeek <- ga$getData(id,"2013-09-01","2013-09-30",
metrics = "ga:visits",
dimensions = "ga:week")

Or, I can get page views for the entire year by month:

myresultsPVsByMonth <- ga$getData(id,"2013-01-01","2013-12-31",
metrics = "ga:pageviews",
dimensions = "ga:month")

You can seek more than one metric at a time:

myresultsPVsVisits <- ga$getData(id,"2013-01-01","2013-12-31",
metrics = "ga:visits, ga:pageviews",
dimensions = "ga:month")

(For those who know R and are used to combining items using R's concatenate c() function, you don't use that when combining items within a ga$getData query.)

1 2 3 Page 2
Page 2 of 3
Bing’s AI chatbot came to work for me. I had to fire it.
Shop Tech Products at Amazon