Chinaunix首页 | 论坛 | 博客
  • 博客访问: 518825
  • 博文数量: 260
  • 博客积分: 10435
  • 博客等级: 上将
  • 技术积分: 1939
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 14:50
文章分类

全部博文(260)

文章存档

2011年(22)

2010年(209)

2009年(29)

我的朋友

分类: Python/Ruby

2011-05-12 16:28:55

http://www.lampblog.net/2011/03/google-analytics-%E6%95%B0%E6%8D%AE-api/

今天认真学习了Google Analytics 数据 API,其实也很简单的,这个讲一讲,给其它想学Google Analytics 数据 API做个参考。

一、请求账号信息

这里使用linux shell的curl方法请求,其它JavaScript,Java,HTTP,NET,Python,PHP,Python,Ruby的操作也类似,这 里不做介绍,详情参考 /gdataLibraries.html

处理shell脚本 accountFeed.sh

01#!/bin/bash
02#
03# Copyright 2009 Google Inc. All Rights Reserved
04# Licensed under the Apache License, Version 2.0 (the "License");
05# you may not use this file except in compliance with the License.
06# You may obtain a copy of the License at
07#
08#
09#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Access the Account Feed of the GA Data Export API through cURL
17 
18USER_EMAIL="xxxxxxx@gmail.com" #Insert your Google Account email here
19USER_PASS="xxxxxxxxx" #Insert your password here
20 
21googleAuth="$(curl -s \
22  -d Email=$USER_EMAIL \
23  -d Passwd=$USER_PASS \
24  -d accountType=GOOGLE \
25  -d source=curl-accountFeed-v2 \
26  -d service=analytics \
27  | awk /Auth=.*/)"
28 
29feedUri="\
30?prettyprint=true"
31 
32curl $feedUri --silent \
33  --header "Authorization: GoogleLogin $googleAuth" \
34  --header "GData-Version: 2"

将自己的Google Analytics账号换上,shell上运行

1chmod u+x accountFeed.sh
2./accountFeed.sh >account.xml

得到关于账号信息的xml文档。

从图中找到站点配置文件的tableid,如上面红色区域。

二、请求报告信息

处理shell脚本 dataFeed.sh

01#!/bin/bash
02#
03# Copyright 2009 Google Inc. All Rights Reserved
04# Licensed under the Apache License, Version 2.0 (the "License");
05# you may not use this file except in compliance with the License.
06# You may obtain a copy of the License at
07#
08#
09#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16# Access the Data Feed of the GA Data Export API through cURL
17 
18USER_EMAIL="XXXXX@gmail.com" #Insert your Google Account email address here
19USER_PASS="xxxxxxxx" #Insert your password here
20TABLE_ID="ga:xxxxxxxx" #Insert your table ID here (ie ga:1234)
21 
22googleAuth="$(curl -s \
23  -d Email=$USER_EMAIL \
24  -d Passwd=$USER_PASS \
25  -d accountType=GOOGLE \
26  -d source=curl-dataFeed-v2 \
27  -d service=analytics \
28  | awk /Auth=.*/)"
29 
30feedUri="\
31?ids=$TABLE_ID\
32&start-date=2011-01-01\
33&end-date=2011-01-31\
34&dimensions=ga:source,ga:medium\
35&metrics=ga:visits,ga:bounces\
36&sort=-ga:visits\
37&filters=ga:medium%3D%3Dreferral\
38&max-results=5\
39&prettyprint=true"
40 
41curl $feedUri --silent \
42  --header "Authorization: GoogleLogin $googleAuth" \
43  --header "GData-Version: 2"

将自己的Google Analytics账号和前面得到的tableid换上,shell上运行



1chmod u+x dataFeed.sh
2./dataFeed.sh >data.xml

得到报告数据信息xml,如下图

三、数据供稿请求参数说明

对数据供稿的请求参数做下说明:

字段 内容 是否必须 说明
基准网址 数据供稿请求的基准网址。
ids ids=ga:12345 Google Analytics(分析)报告数据的唯一表格 ID
dimensions dimensions=ga:source,ga:medium Google Analytics(分析)报告的主要数据键
metrics metrics=ga:visits,ga:bounces 配置文件中用户活动的汇总统计信息
sort sort=-ga:visits 指示所返回数据的排序顺序和方向
filters filters=ga:medium%3D%3Dreferral 将限制您对 Google Analytics(分析)服务器的请求所返回的数据。
segment segment=dynamic::ga:medium%3D%3Dreferral 有关高级细分的常规信息
start-date start-date=2009-04-20 所有 Google Analytics(分析)供稿请求必须指定开始和结束日期范围
end-date end-date=2009-05-20 所有 Google Analytics(分析)供稿请求必须指定开始和结束日期范围
Start-index start-index=10 起始索引
max-results max-results=100 此供稿可包含的最大条目数
v 参数 v=2 所请求的供稿版本
prettyprint prettyprint=true 为供稿 XML 添加额外空格,以提高其可读性


阅读(2513) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~