Garb是一个用于从Google Analytics 中查询数据的Ruby 类。相关链接:
安装
$ sudo gem install garb
用法
用户验证
取回账户列表
查找配置文件
id可以是统计代码中形如UA-247543-9的web_property_id,亦可是查看配置时网址https://www.google.com/analytics/reporting/?reset=1&id=4662047中形如4662047的数字ID。
不可按域名查找。因为Google Analytics的配置文件并不是和域名绝对绑定的。但如果配置文件的名称就是域名的话,可以Garb::Profile.all取回所有配置文件再用域名比对title。
创建报告
Garb::Session.login('someone@gmail.com', password)
profile=Garb::Profile.first(‘UA-247543-9’)
report = Garb::Report.new(profile,:limit=>50)
report.metrics :visits,:pageviews
report.dimensions :keyword,:page_path
report.sort :visits.desc
report.filters :keyword.does_not_match => '(not set)'
report.results
此报告取回热门关键词来源及相对应的Page,如:
puts report.results.first
##<OpenStruct visits="91", pageviews="106", keyword="example", page_path="/garb.html">
可用的参数
- :start_date
- :end_date
- :limit
- :offset
指标和纬度
metrics 和 dimensions 可参考 analytics 创建自定义报告中的指标和纬度,详细列表可查看Google官方文档:Dimensions & Metrics。
排序
sort 默认升序排列,在参数后加.desc即为降序。
过滤
指标可用的过滤语法
eql => '==',
not_eql => '!=',
gt => '>',
gte => '>=',
lt => '<',
lte => '<='
纬度可用的语法
matches => '==',
does_not_match => '!=',
contains => '=~',
does_not_contain => '!~',
substring => '=@',
not_substring => '!@'
示例
{ 0 comments }