首页 > Google Analytics > Google Analytics 数据 API

Google Analytics 数据 API

2011年3月1日 发表评论 阅读评论

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

一、请求账号信息

这里使用linux shell的curl方法请求,其它JavaScript,Java,HTTP,NET,Python,PHP,Python,Ruby的操作也类似,这里不做介绍,详情参考http://code.google.com/intl/zh-CN/apis/analytics/docs/gdata/gdataLibraries.html

处理shell脚本 accountFeed.sh

#!/bin/bash
#
# Copyright 2009 Google Inc. All Rights Reserved
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Access the Account Feed of the GA Data Export API through cURL

USER_EMAIL="xxxxxxx@gmail.com" #Insert your Google Account email here
USER_PASS="xxxxxxxxx" #Insert your password here

googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s \
  -d Email=$USER_EMAIL \
  -d Passwd=$USER_PASS \
  -d accountType=GOOGLE \
  -d source=curl-accountFeed-v2 \
  -d service=analytics \
  | awk /Auth=.*/)"

feedUri="https://www.google.com/analytics/feeds/accounts/default\
?prettyprint=true"

curl $feedUri --silent \
  --header "Authorization: GoogleLogin $googleAuth" \
  --header "GData-Version: 2"

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

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

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

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

二、请求报告信息

处理shell脚本 dataFeed.sh

#!/bin/bash
#
# Copyright 2009 Google Inc. All Rights Reserved
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Access the Data Feed of the GA Data Export API through cURL

USER_EMAIL="XXXXX@gmail.com" #Insert your Google Account email address here
USER_PASS="xxxxxxxx" #Insert your password here
TABLE_ID="ga:xxxxxxxx" #Insert your table ID here (ie ga:1234)

googleAuth="$(curl https://www.google.com/accounts/ClientLogin -s \
  -d Email=$USER_EMAIL \
  -d Passwd=$USER_PASS \
  -d accountType=GOOGLE \
  -d source=curl-dataFeed-v2 \
  -d service=analytics \
  | awk /Auth=.*/)"

feedUri="https://www.google.com/analytics/feeds/data\
?ids=$TABLE_ID\
&start-date=2011-01-01\
&end-date=2011-01-31\
&dimensions=ga:source,ga:medium\
&metrics=ga:visits,ga:bounces\
&sort=-ga:visits\
&filters=ga:medium%3D%3Dreferral\
&max-results=5\
&prettyprint=true"

curl $feedUri --silent \
  --header "Authorization: GoogleLogin $googleAuth" \
  --header "GData-Version: 2"

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



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

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

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

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

字段 内容 是否必须 说明
基准网址 https://www.google.com/analytics/feeds/data 数据供稿请求的基准网址。
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 添加额外空格,以提高其可读性

  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.