2010-10-13 57 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

相關問題