Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何获取谷歌分析统计数据?_Ruby On Rails_Ruby On Rails 3_Google Analytics_Google Analytics Api - Fatal编程技术网

Ruby on rails 如何获取谷歌分析统计数据?

Ruby on rails 如何获取谷歌分析统计数据?,ruby-on-rails,ruby-on-rails-3,google-analytics,google-analytics-api,Ruby On Rails,Ruby On Rails 3,Google Analytics,Google Analytics Api,这是最好的选择吗 我有一个有用户的网站example.com,我想让他们在example.com上看到他们的谷歌分析统计数据,我该怎么做 我可以看到,但我不知道如何开始。我还使用google api ruby客户端gem,并按照您提供的链接()中概述的方式进行设置 只需按照链接中概述的步骤设置Google Analytics客户端: # you need to set this according to your situation/needs SERVICE_ACCOUNT_EMAIL_ADD

这是最好的选择吗

我有一个有用户的网站example.com,我想让他们在example.com上看到他们的谷歌分析统计数据,我该怎么做

我可以看到,但我不知道如何开始。

我还使用google api ruby客户端gem,并按照您提供的链接()中概述的方式进行设置

只需按照链接中概述的步骤设置Google Analytics客户端:

# you need to set this according to your situation/needs
SERVICE_ACCOUNT_EMAIL_ADDRESS = '...' # looks like 12345@developer.gserviceaccount.com
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
要在应用程序中显示用户页面的统计信息,您必须在进行查询时调整度量和过滤器参数。例如,上面的查询将返回一个结果对象,其中包含url为example.com/url/to/user的页面的所有页面视图



警告:这个答案是很久以前写的,谷歌发布了一个新的、不兼容的gem版本。请参考

Legato从未被放弃过,它是针对GA API构建可维护查询的更好方法。我已经请那个要点的作者纠正他错误的笔记。Legato始终支持GA API版本3。救了我一天。。谢谢@severin。在“生成查询”部分中,PROILE应拼写为PROFILE。不过没什么大不了的。@severin什么是“PROFILE=”…“#您的GA配置文件id看起来像“GA:12345”?你从哪里拿到的身份证?那是分析帐户id吗。?如果是这样的话,我们需要在它前面加上“ga:”前缀吗?@severin当我运行“api_method=client.discovered_api('analytics','v3').data.ga.get“它的前导未知关键字:interval from/home/brahmos/.rvm/gems/ruby-2.0.0-p576/gems/google-api-client-0.7.1/lib/google/api_-client.rb:595:in'execute!”@h、 APP.y通过导航到“Admin”找到配置文件id,然后转到视图的“视图设置”;在那里你会看到一个“视图ID”:这是配置文件ID(谷歌将其从配置文件ID重命名为视图ID),例如“12345”。然后你必须将个人资料设置为“ga:12345”。@severin知道了。非常感谢。我被搜索分级。