Using Google Analytics with Processing
2012
Google Analytics
We have been working on a project which visualizes visitor statistics of our website. Luckily there is Google offering the service Google Analytics, which is going to provide us with all the necessary data. Google Analytics offers a huge amount of information about a website, but at the same time this makes the API quite complex and harder to grasp quickly. We are going to explain all necessary steps how to set up your Processing application in order to retrieve data from Google Analytics.

Google Libraries
You'll need a couple of libraries by Google (Google Data Client Libraries), which we packed in an archive for you here.
It contains all necessary libraries you need. Just paste the folders inside the archive into your sketchbook/libraries/ folder and everything should be working fine. If you need it only once you can also put all the jar files in the sketch code folder.

Connect with Google Analytics
You have to log in to your Google Analytics account (and you have to have a website included into Google Analytics of course) and get your TableId. Go to Google Analytics, log in, go to the home page and check your address bar, it should look something like this:
https://www.google.com/analytics/web/startDate?pli=1#dashboard/default/a7544494w33317544pXXXXXXXX/

Note the end of the adress, saying XXXXXXXX, instead of this it should say a number- your tableId!

Now that you have all necessary information we can start with the Processing sketch:

processing code

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import com.google.gdata.client.analytics.AnalyticsService; 
import com.google.gdata.client.analytics.DataQuery;
import com.google.gdata.data.analytics.DataEntry;
import com.google.gdata.data.analytics.DataFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import com.google.common.collect.Maps;
import java.util.Date;

private AnalyticsService analyticsService;
private static final String CLIENT_USERNAME = "*email*";
private static final String CLIENT_PASS = "*emailpass*";
private static final String TABLE_ID = "ga:*tableId*";

DataQuery query;
DataFeed feed;

void setup() {
size(100, 100);
initAnalyticsService();
}

void draw() {
println("Visitors today so far: "+getVisitors());
noLoop();
}

private void initAnalyticsService() {
analyticsService=new AnalyticsService("analyticsService");
try {
analyticsService.setUserCredentials(CLIENT_USERNAME,CLIENT_PASS);
}
catch (AuthenticationException e) {
e.printStackTrace();
}
}

public int getVisitors() {
int visits = 0;
try {
query=new DataQuery(new URL(
"https://www.google.com/analytics/feeds/data"));
}
catch (MalformedURLException e) {
e.printStackTrace();
}
query.setStartDate("2012-06-14");
query.setEndDate("2012-06-14");
query.setDimensions("ga:visitCount");
query.setMetrics("ga:visitors");
query.setIds(TABLE_ID);
try {
feed=analyticsService.getFeed(query.getUrl(),DataFeed.class);
}
catch (IOException e) {
e.printStackTrace();
}
catch (ServiceException e) {
e.printStackTrace();
}
for (DataEntry entry : feed.getEntries()) {
visits+=Integer.parseInt(entry.stringValueOf("ga:visitors"));
}
return visits;
}

The parts that you may want to change according to your needs are the calls to setStartDate(String), setEndDate(String), setDimensions(String), setMetrics(String). You can set any date range by changing the dates you pass to the methods, so you could for example get the number of visits for your site over the entire last 30 days.

There are dimensions and metrics. Have a look at this list of all available dimensions and metrics ( https://developers.google.com/analytics/devguides/reporting/core/dimsmets ) you can use and combine in your sketch. Simply pass them into the setMetrics method separated by a comma. Moreover there is a method query.setSort(String) which sorts your results by your set metrics. According to your example simply add query.setMetrics("ga:visitors") or query.setMetrics("-ga:visitors").


The following example is the most basic Google Analytics sketch possible. In order to give you an idea of the possibilities, we’ve created a graph displaying the visitors of the last 7 days. You can download the processing sketch at the end of the article.

Simple GraphWe advice you to use our helper class, in order to get your sketch not too messed up. Just drop it the folder of your sketch and create a new instance of it- this is where we get our Google Analytics data from; you can retrieve all data though this class and never have to touch it again once it’s set up.


A sketch with this setup would look something like this:

processing code

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
import java.util.Date;
import com.google.gdata.client.analytics.AnalyticsService;
import com.google.common.collect.Maps;
import com.google.gdata.util.AuthenticationException;

GoogleAnalyticsHandler gahandler;

void setup() {
size(300, 300);
gahandler = new GoogleAnalyticsHandler();
}

void draw() {
println("Visitors today so far:"+gahandler.getVisitors(new Date()));
noLoop();
}