2010-10-13 58 views
0

我使用Google Analytics API的Garb Ruby包装为我的应用程序构建了一个pageview计数器。这样做意味着在“LIB”文件夹中创建一个新的模块,我在其中创建这样一个类:在Rails中,有没有办法在类(不是控制器)中调用request.request_uri?

#lib/Analyze.rb 

... 
class Report 
    extend Garb::Resource 
    metrics :pageviews 
    dimensions :page_path 
    filters :page_path.eql => '/' #the path of the page I instantiate this class on 
end 

#followed by a method for instantiating this class 

我需要过滤器:page_path.eql =>是在我称之为页面的路径方法。我尝试了像request.request_uri或url_for(:action =>'show':controllers =>'users':id => params [:id])但不知道如何在这个类中指定页面路径定义。

回答

1

这将打破MVC封装 - 我认为你应该在你的application_controller中创建一个之前的过滤器,将你需要的请求数据传递给你的类。

编辑

如果你想建立一个特定网页一次性的报告,我认为你需要做这样的事情:

profile = Garb::Profile.first('UA-XXXX-XX') 

report = Garb::Report.new(profile) 
report.metrics :pageviews 
report.dimensions :page_path 
report.filters :page_path.eql => request.request_uri 

report.results 

同样,如果你”在每一页上都有这个,过滤器是明智的,我想。不过肯定会让你的应用减慢很多。

This is covered in the docs.

+0

将请求数据传回类是什么样的?我知道我不能将一个变量传递给类定义。对不起,还是一个相当新的程序员。 – kateray 2010-10-13 15:23:12

+0

我想你想为每个页面构建一次性报告 - 我已经更新了我的回答 – Codebeef 2010-10-13 16:01:10

+0

好的,这对我有用,但也会让它变慢。如果你想出任何其他的方式,爱知道。谢谢。 – kateray 2010-10-13 16:24:15

相关问题