由于公司业务的统计功能 需要使用一些第三方的统计分析软件。国内的也有一些分析软件,但是功能没有谷歌分析的全面。 由于我是后台java开发的,这个谷歌分析是由产品查来使用的,我大概看了一下,功能还挺多。在需要统计的页面上加入一些简单的js代码,就会将数据发送到谷歌分析那,进行分析能统计使用的语言 城市 浏览器 操作系统 服务提供商 和一些会话数,自定义的一些东西。 我的任务是将上面的一些数据拉下来放到数据库里面做统计分析。谷歌分析API-V3版本(目前有V4版本的但是是全英的,V3有中文版本)V3 流程: 一、创建客户端 ID 1、打开 Developers Console 的权限页上的服务帐户部分。 点击创建服务帐户。 2、在创建服务帐户窗口中,键入服务帐户的名称,然后选择提供新的私钥。如果您希望将 Google Apps 全域授权授予该服务帐户,另请选中启用 Google Apps 全网域委派功能。然后,点击创建。 3、您的新公钥/私钥对已生成并下载到您的计算机;您可以将其用作该密钥的唯一副本。您负责安全存储该密钥。 4、当系统提示您提供“密钥类型”时,选择 P12,并将生成的密钥另存为 client_secrets.p12;在本教程的后面,您将需要用到该密钥。 二、安装google客户端库 pom :
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-analytics</artifactId>
<version>v3-rev134-1.22.0</version>
</dependency>
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-gson</artifactId>
<version>1.19.1</version>
</dependency>
代码:
package net.newcapec.campus.h5.service.impl;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import net.newcapec.campus.h5.Job.ThreadTask;
import net.newcapec.campus.h5.entity.GaDataLog;
import net.newcapec.campus.h5.entity.GaLog;
import net.newcapec.campus.h5.manager.GaDataManager;
import net.newcapec.campus.h5.manager.GaLogManager;
import net.newcapec.campus.h5.service.GaDataService;
import net.newcapec.campus.quickaccess.utils.PreferenceUtils;
import net.newcapec.v3.extend.orm.condition.Conditions;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.AnalyticsScopes;
import com.google.api.services.analytics.model.Accounts;
import com.google.api.services.analytics.model.GaData;
import com.google.api.services.analytics.model.Profiles;
import com.google.api.services.analytics.model.Webproperties;
public class GaDataServiceImpl implements GaDataService{
private GaDataManager gaDataManager;
private GaLogManager gaLogManager;
private PreferenceUtils preferenceUtils;
public void setPreferenceUtils(PreferenceUtils preferenceUtils) {
this.preferenceUtils = preferenceUtils;
}
public void setGaLogManager(GaLogManager gaLogManager) {
this.gaLogManager = gaLogManager;
}
public void setGaDataManager(GaDataManager gaDataManager) {
this.gaDataManager = gaDataManager;
}
/*private String APPLICATION_NAME = preferenceUtils.getGaApplicationName();
private JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
private String KEY_FILE_LOCATION = preferenceUtils.getGaKeyFileLocation();
private String SERVICE_ACCOUNT_EMAIL = preferenceUtils.getGaServiceAccountEmail();*/
//2016年9月14日15:32:10 使用线程池来多任务执行 暂时没有使用
//第二个版本 :支持多线程 并且记录每一天的数据量
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
public void execAllDataThreadPool() throws Exception {
Analytics analytics = this.initializeAnalytics();
String profile = getFirstProfileId(analytics);
System.out.println("First Profile Id: " + profile);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//先查询有没有同步过数据
List<GaLog> markTime = gaLogManager.findByCondition(Conditions.isNotNull("markTime"),Conditions.desc("markTime"));
String starDay = preferenceUtils.getGaDataStartTime(); //设置为参数便于修改 拉取的开始时间
String endDay = preferenceUtils.getGaDataEndTime(); 设置为参数便于修改 拉取的结束时间 由于是定时任务默认拉取到昨天的数据
//如果有标记就按照标记的开始做增量
if(markTime.size()>0){
Date markTime2 = markTime.get(0).getMarkTime();
long t =markTime2.getTime()+24*60*60*1000;
starDay = sdf.format(new Date(t)); //如果有标记 就是做增量的开始时间
}
long starLong = sdf.parse(starDay).getTime(); //开始时间的long
Date as = new Date(new Date().getTime()-24*60*60*1000);
String yesterday = sdf.format(as); //昨天是几月几日
if(!endDay.equals("yesterday")){ //如果配置了结束时间 就开始时间到结束时间 的时间段
yesterday = endDay;
}
long endLong = sdf.parse(yesterday).getTime(); //结束的时间long
for(;starLong<=endLong;starLong=starLong+86400000){
fixedThreadPool.execute(new ThreadTask(analytics,profile,starLong));
}
}
//下面为第一个版本
@Override
public String execAllData() {
try {
Analytics analytics = this.initializeAnalytics();
String profile = getFirstProfileId(analytics);
System.out.println("First Profile Id: " + profile);
//先查询有没有同步过数据
List<GaLog> markTime = gaLogManager.findByCondition(Conditions.isNotNull("markTime"),Conditions.desc("markTime"));
String starDay = preferenceUtils.getGaDataStartTime();
//如果有标记就按照标记的开始做增量
if(markTime.size()>0){
Date markTime2 = markTime.get(0).getMarkTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
starDay = sdf.format(markTime2);
}
GaData results = analytics.data().ga().get("ga:" + profile, starDay, "yesterday","ga:totalEvents").setDimensions("ga:eventLabel").setMaxResults(1).execute();
System.out.println(results.toString());
JSONObject total = JSONObject.parseObject(results.toString());
// 获取总条数
Integer totalResult = total.getInteger("totalResults");
Integer pageCount = 1;
// 总页数
if (totalResult > 0) {
pageCount = (totalResult + 1000 - 1) / 1000;
}
// 分页查询并插入
GaLog glg = new GaLog();
long st = System.currentTimeMillis();
for (int i = 1; i < pageCount; i++) {
// 一页的数据
// GaData pageData = getResults(analytics,
// profile,(i-1)*1000+1);
Integer startIndex = (i - 1) * 1000 + 1;
GaData pageData = analytics.data().ga().get("ga:" + profile, starDay, "yesterday","ga:totalEvents").setDimensions("ga:eventLabel").setMaxResults(1000).setStartIndex(startIndex).execute();
// 保存到数据库
JSONObject rows = JSONObject.parseObject(pageData.toString());
JSONArray rowsResult = rows.getJSONArray("rows");
GaLog gl = new GaLog();
gl.setPage(i);
long startTime = System.currentTimeMillis();
gl.setPageCount(rowsResult.size());
for (Object rr : rowsResult) {
JSONArray JA = (JSONArray) rr;
// ["{\"userId\":\"1635793\",\"time\":1471874473564,\"eventAction\":\"home||main||00\",\"label\":\"迎新广场首页\"}","1"]
// System.out.println(JA);
if (JA.size() >= 2) {
try {
JSONObject data = JSONObject.parseObject(JA.getString(0));
String userId = data.getString("userId");
Long time = data.getLong("time");
String eventAction = data.getString("eventAction");
String label = data.getString("label");
String arg = JA.getString(1);
GaDataLog gd = new GaDataLog();
gd.setArg(arg);
gd.setCreateDate(new Date());
gd.setData(data.toJSONString());
gd.setEventAction(eventAction);
gd.setLabel(label);
gd.setTime(time);
gd.setUserId(userId);
gaDataManager.save(gd);
} catch (Exception e) {
}
}
}
gl.setUseTime(System.currentTimeMillis() - startTime);
gaLogManager.save(gl);
}
glg.setUseTime(System.currentTimeMillis() - st);
gaLogManager.save(glg);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private Analytics initializeAnalytics() throws Exception {
// Initializes an authorized analytics service object.
// Construct a GoogleCredential object with the service account email
// and p12 file downloaded from the developer console.
HttpTransport httpTransport = GoogleNetHttpTransport
.newTrustedTransport();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(GsonFactory.getDefaultInstance())
.setServiceAccountId(preferenceUtils.getGaServiceAccountEmail())
.setServiceAccountPrivateKeyFromP12File(
new File(preferenceUtils.getGaKeyFileLocation()))
.setServiceAccountScopes(AnalyticsScopes.all()).build();
// Construct the Analytics service object.
return new Analytics.Builder(httpTransport, GsonFactory.getDefaultInstance(), credential)
.setApplicationName(preferenceUtils.getGaApplicationName()).build();
}
private static String getFirstProfileId(Analytics analytics)
throws IOException {
// Get the first view (profile) ID for the authorized user.
String profileId = null;
// Query for the list of all accounts associated with the service
// account.
Accounts accounts = analytics.management().accounts().list().execute();
if (accounts.getItems().isEmpty()) {
System.err.println("No accounts found");
} else {
String firstAccountId = accounts.getItems().get(0).getId();
// Query for the list of properties associated with the first
// account.
Webproperties properties = analytics.management().webproperties()
.list(firstAccountId).execute();
if (properties.getItems().isEmpty()) {
System.err.println("No Webproperties found");
} else {
String firstWebpropertyId = properties.getItems().get(0)
.getId();
// Query for the list views (profiles) associated with the
// property.
Profiles profiles = analytics.management().profiles()
.list(firstAccountId, firstWebpropertyId).execute();
if (profiles.getItems().isEmpty()) {
System.err.println("No views (profiles) found");
} else {
// Return the first (view) profile associated with the
// property.
profileId = profiles.getItems().get(0).getId();
}
}
}
return profileId;
}
}
package net.newcapec.campus.h5.Job;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import net.newcapec.campus.h5.entity.GaDataLog;
import net.newcapec.campus.h5.entity.GaLog;
import net.newcapec.campus.h5.manager.GaDataManager;
import net.newcapec.campus.h5.manager.GaLogManager;
import net.newcapec.campus.h5.util.BeanUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.api.services.analytics.Analytics;
import com.google.api.services.analytics.model.GaData;
public class ThreadTask implements Runnable {
private Analytics analytics;
private String profile;
private long starLong;
public ThreadTask() {
}
public ThreadTask(Analytics analytics, String profile, long starLong) {
super();
this.analytics = analytics;
this.profile = profile;
this.starLong = starLong;
}
@Override
public void run() {
this.execData(analytics, profile, starLong);
}
// 一天的处理数据
private void execData(Analytics analytics, String profile, long starLong) {
try {
GaDataManager gaDataManager = (GaDataManager) BeanUtils
.getBean("gaDataManager");
GaLogManager gaLogManager = (GaLogManager) BeanUtils
.getBean("gaLogManager");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String onlyDay = sdf.format(new Date(starLong));
GaData results = analytics.data().ga()
.get("ga:" + profile, onlyDay, onlyDay, "ga:totalEvents")
.setDimensions("ga:eventLabel").setMaxResults(1).execute();
System.out.println(results.toString());
JSONObject total = JSONObject.parseObject(results.toString());
// 获取总条数
Integer totalResult = total.getInteger("totalResults");
Integer pageCount = 1;
// 总页数
if (totalResult >= 0) {
pageCount = (totalResult + 1000 - 1) / 1000;
}
// 记录每天的数量和用时
GaLog glg = new GaLog();
glg.setMarkNumber(totalResult);
glg.setMarkTime(new Date(starLong));
long st = System.currentTimeMillis();
// 分页查询并插入
for (int i = 1; i <= pageCount; i++) {
// 一页的数据
// GaData pageData = getResults(analytics,
// profile,(i-1)*1000+1);
Integer startIndex = (i - 1) * 1000 + 1;
GaData pageData = analytics
.data()
.ga()
.get("ga:" + profile, onlyDay, onlyDay,
"ga:totalEvents")
.setDimensions("ga:eventLabel").setMaxResults(1000)
.setStartIndex(startIndex).execute();
// 保存到数据库
JSONObject rows = JSONObject.parseObject(pageData.toString());
JSONArray rowsResult = rows.getJSONArray("rows");
GaLog gl = new GaLog();
gl.setPage(i);
long startTime = System.currentTimeMillis();
gl.setPageCount(rowsResult.size());
for (Object rr : rowsResult) {
JSONArray JA = (JSONArray) rr;
// ["{\"userId\":\"1635793\",\"time\":1471874473564,\"eventAction\":\"home||main||00\",\"label\":\"迎新广场首页\"}","1"]
// System.out.println(JA);
if (JA.size() >= 2) {
try {
JSONObject data = JSONObject.parseObject(JA
.getString(0));
String userId = data.getString("userId");
Long time = data.getLong("time");
String eventAction = data.getString("eventAction");
String label = data.getString("label");
String arg = JA.getString(1);
GaDataLog gd = new GaDataLog();
gd.setArg(arg);
gd.setCreateDate(new Date());
gd.setData(data.toJSONString());
gd.setEventAction(eventAction);
gd.setLabel(label);
gd.setTime(time);
gd.setUserId(userId);
gaDataManager.save(gd);
} catch (Exception e) {
}
}
}
gl.setUseTime(System.currentTimeMillis() - startTime);
// gaLogManager.save(gl);
}
glg.setUseTime(System.currentTimeMillis() - st);
glg.setMarkTime(new Date(starLong));
gaLogManager.save(glg);
} catch (IOException e) {
e.printStackTrace();
}
}
}