今天认真学习了Google Analytics 数据 API,其实也很简单的,这个讲一讲,给其它想学Google Analytics 数据 API做个参考。
一、请求账号信息
这里使用linux
shell的curl方法请求,其它JavaScript,Java,HTTP,NET,Python,PHP,Python,Ruby的操作也类似,这
里不做介绍,详情参考
/gdataLibraries.html
处理shell脚本 accountFeed.sh
| 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 |
| 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. |
| 16 | # Access the Account Feed of the GA Data Export API through cURL |
| 18 | USER_EMAIL="xxxxxxx@gmail.com" #Insert your Google Account email here |
| 19 | USER_PASS="xxxxxxxxx" #Insert your password here |
| 21 | googleAuth="$(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 \ |
| 32 | curl $feedUri --silent \ |
| 33 | --header "Authorization: GoogleLogin $googleAuth" \ |
| 34 | --header "GData-Version: 2" |
将自己的Google Analytics账号换上,shell上运行
| 1 | chmod u+x accountFeed.sh |
| 2 | ./accountFeed.sh >account.xml |
得到关于账号信息的xml文档。

从图中找到站点配置文件的tableid,如上面红色区域。
二、请求报告信息
处理shell脚本 dataFeed.sh
| 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 |
| 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. |
| 16 | # Access the Data Feed of the GA Data Export API through cURL |
| 18 | USER_EMAIL="XXXXX@gmail.com" #Insert your Google Account email address here |
| 19 | USER_PASS="xxxxxxxx" #Insert your password here |
| 20 | TABLE_ID="ga:xxxxxxxx" #Insert your table ID here (ie ga:1234) |
| 22 | googleAuth="$(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 \ |
| 32 | &start-date=2011-01-01\ |
| 34 | &dimensions=ga:source,ga:medium\ |
| 35 | &metrics=ga:visits,ga:bounces\ |
| 37 | &filters=ga:medium%3D%3Dreferral\ |
| 41 | curl $feedUri --silent \ |
| 42 | --header "Authorization: GoogleLogin $googleAuth" \ |
| 43 | --header "GData-Version: 2" |
将自己的Google Analytics账号和前面得到的tableid换上,shell上运行
得到报告数据信息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 添加额外空格,以提高其可读性 |