是Google API Ruby client最好的选择?如何提取Google Analytics统计信息?
我有一个网站example.com与用户,我希望他们看到他们在example.com上的谷歌分析统计,我该怎么做?我可以看到the example但我无法弄清楚如何开始。
是Google API Ruby client最好的选择?如何提取Google Analytics统计信息?
我有一个网站example.com与用户,我希望他们看到他们在example.com上的谷歌分析统计,我该怎么做?我可以看到the example但我无法弄清楚如何开始。
我也使用google-api-ruby-client gem,并按照您提供的链接(https://gist.github.com/joost/5344705)中概述的相同方式进行设置。
只要按照建立一个谷歌Analytics(分析)客户端的链接中列出的步骤:
# you need to set this according to your situation/needs
SERVICE_ACCOUNT_EMAIL_ADDRESS = '...' # looks like [email protected]
PATH_TO_KEY_FILE = '...' # the path to the downloaded .p12 key file
PROFILE = '...' # your GA profile id, looks like 'ga:12345'
require 'google/api_client'
# set up a client instance
client = Google::APIClient.new
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:issuer => SERVICE_ACCOUNT_EMAIL_ADDRESS,
:signing_key => Google::APIClient::PKCS12.load_key(PATH_TO_KEY_FILE, 'notasecret')
).tap { |auth| auth.fetch_access_token! }
api_method = client.discovered_api('analytics','v3').data.ga.get
# make queries
result = client.execute(:api_method => api_method, :parameters => {
'ids' => PROFILE,
'start-date' => Date.new(1970,1,1).to_s,
'end-date' => Date.today.to_s,
'dimensions' => 'ga:pagePath',
'metrics' => 'ga:pageviews',
'filters' => 'ga:pagePath==/url/to/user'
})
puts result.data.rows.inspect
要将您的应用为用户显示的页面的统计数据,你必须调整指标和过滤器进行查询时的参数。例如,上面的查询将返回一个结果对象,其中包含网页的所有综合浏览量,网址为example.com/url/to/user。
警告:这个答案是很久以前写的和谷歌发布了创业板的新的,不兼容的版本。请参考https://github.com/google/google-api-ruby-client/blob/master/MIGRATING.md
保存我的一天..谢谢@severin。在make查询部分,PROILE应该拼写为PROFILE。虽然没有什么大不了的。 – Kumar
谢谢@库马尔。我修正了这个错字,谢谢你指出! – severin
@severin什么是“PROFILE ='...'#您的GA个人资料ID,看起来像'ga:12345'”? 你从哪里拿到了这个ID? 是分析帐户ID。 如果是这样,我们需要用它的前缀'ga:'。? – Aparichith
Legato从未被放弃,并且是一种更好的方式来构建针对GA API的可维护查询。我已经要求该Gist的作者纠正他的错误笔记。 Legato支持GA API版本3. https://github.com/tpitale/legato/commit/0def82f9bdb9cf259d4d91d5bd2f17759231bb29 –