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 3
Page 3 of 3

Want to just see visits that came from, say, Google News each month this year? Add a filter, such as

myresultsGNvisits <- ga$getData(id, start.date = "2013-01-01", end.date = "2013-12-31",
metrics = "ga:visits",
filters = "ga:source=~news.google.com",
dimensions = "ga:month")

I used =~ rather than == because the latter would set the filter to only those referrals that exactly equal news.google.com. By using the =~ operator instead, it uses more powerful regular expression searching, which in this case would match anything containing news.google.com. (Regular expressions allow much more robust pattern searching.)

As before, for each of these queries, type

myresults

(or the appropriate results variable) at the prompt in your R window to see what's returned.

Google Analytics
The query has been refined to show the visits that came from Google News each month for a year.

Step 4: Manipulate your data

Now that you've got your data, what can you do with it?

If you're not an R enthusiast, the easiest thing is to save the results to a CSV file. R's write.csv() function first lists what you want to save and then the file name. To save the myresults variable to a file called data.csv, type:

write.csv(myresults, file="data.csv", row.names=FALSE)

The optional row.names=FALSE eliminates an extra column with the row numbers, just to keep the file uncluttered. The resulting file looks something like this (but hopefully with many more visits):

"month","visits"

"01",625

"02",790

"03",395

"04",219

"05",927

"06",151

"07",231

"08",244

"09",231

You can then use that data in the spreadsheet or graphing program of your choice.

You can also analyze your data right within R, of course, without exporting to a spreadsheet. Let me first pull some real data -- visits and page views -- from a personal site I set up years ago that I no longer tend to but that still gets occasional visitors:

mydata <- ga$getData(id, start.date="2013-01-01", end.date="2013-12-31",
metrics = "ga:visits, ga:pageviews",
dimensions = "ga:month")

Google Analytics
Data on monthly visits to a site.

You can use R's str() function to find out how the mydata object is structured.

Google Analytics
This shows how the mydata object is structured.

Like the other results above, it's an R data frame with character strings as the month number and numbers for the data. That makes it easy to run simple analyses and generate basic graphs within R, such as

barplot(mydata$visits, main="Visits by month", xlab="Month", names.arg=mydata$month, las=1, col=rainbow(9))

Google Analytics
You can generate basic graphs within R, such as this one, which shows the number of visits to a site for each month.

The R barplot() command above uses the number of visits for the graph's y axis values (you can refer to a specific column in a data frame with the syntax dataframename$columnname) and names.arg as names on the x axis. The command main specifies the graph title, xlab is the x-axis label and col=rainbow(9) tells R to choose nine colors from its rainbow palette to color the bars. The nonintuitive command las=1 tells R to set both the x- and y-axis labels horizontally (0 makes them parallel to the axis, 2 perpendicular to the axis, and 3 vertical).

For more on creating graphs from R data frames, see our article "Beginner's guide to R: Painless data visualization."

Conclusion

Google Analytics is a powerful tool, but the Web interface is not always easy to navigate. If you'd like more customizable tools to extract data -- and easier automation of data requests -- consider using a programmatic approach with the Google Analytics API. And if you don't already have a favorite language for API work, R is a good choice.

Download this article as part of our free PDF: Advanced Beginner's Guide to R.

1 2 3 Page 3
Page 3 of 3
5 tips for working with SharePoint Online
Shop Tech Products at Amazon