Author Topic: using-google-analytics-with-processing[待翻译]  (Read 3298 times)

vinjn

  • SuperManager
  • Hero Member
  • *****
  • Posts: 587
using-google-analytics-with-processing[待翻译]
« on: June 22, 2012, 11:21:50 AM »
via onformative



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:



Code
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.



We 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:

Code
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();
}

附件下载见原帖

鱿鱼一一

  • Newbie
  • *
  • Posts: 2
Re: using-google-analytics-with-processing[待翻译]
« Reply #1 on: August 02, 2012, 08:58:13 PM »
原帖现在打开显示找不到网页·是要翻墙吗?

RavenKwok

  • Moderator 版主
  • Sr. Member
  • *****
  • Posts: 277
  • Artist/ Animator/ Coder/ Cynical Asshole
Re: using-google-analytics-with-processing[待翻译]
« Reply #2 on: August 03, 2012, 01:43:34 AM »
要开十八大了,这种问题不必跟帖问的。

鱿鱼一一

  • Newbie
  • *
  • Posts: 2
Re: using-google-analytics-with-processing[待翻译]
« Reply #3 on: August 04, 2012, 09:30:22 PM »
我下载了页面的analytics-tutorial的processing sketch和gdata-lib-pack。但是打开程序报错说NullpointerException.跳出来的是空白窗口。他在页面上说要申请analytics账号和tableld,这两个我都还没用上。请教步骤····还有那个微薄的用户头像读取,我申请appkey不到,可以用土豆网或者别的网的代替么?打开土豆网分享链接的时候可以看到他的appkey。

Tags: