2 model methods for unique impression account - based on unique ip_address & request_hash

This commit is contained in:
cowboycoded 2011-02-17 09:53:01 -05:00
parent ade9f0c3e2
commit bfb03eeb1c
4 changed files with 31 additions and 11 deletions

View File

@ -15,6 +15,10 @@ module Impressionist
end end
def unique_impression_count(start_date=nil,end_date=Time.now) def unique_impression_count(start_date=nil,end_date=Time.now)
start_date.blank? ? impressions.group(:request_hash).all.size : impressions.where("created_at>=? and created_at<=?",start_date,end_date).group(:request_hash).all.size
end
def unique_impression_count_ip(start_date=nil,end_date=nil)
start_date.blank? ? impressions.group(:ip_address).all.size : impressions.where("created_at>=? and created_at<=?",start_date,end_date).group(:ip_address).all.size start_date.blank? ? impressions.group(:ip_address).all.size : impressions.where("created_at>=? and created_at<=?",start_date,end_date).group(:ip_address).all.size
end end
end end

View File

@ -1,7 +1,7 @@
PATH PATH
remote: /rails_plugins/mine/impressionist remote: /rails_plugins/mine/impressionist
specs: specs:
impressionist (0.2.3) impressionist (0.2.4)
GEM GEM
remote: http://rubygems.org/ remote: http://rubygems.org/

View File

@ -2,6 +2,7 @@
impression<%= i %>: impression<%= i %>:
impressionable_type: Article impressionable_type: Article
impressionable_id: 1 impressionable_id: 1
request_hash: a<%=i%>
ip_address: 127.0.0.<%=i%> ip_address: 127.0.0.<%=i%>
created_at: 2011-01-01 created_at: 2011-01-01
<% end %> <% end %>
@ -10,17 +11,28 @@ impression<%= i %>:
impression8: impression8:
impressionable_type: Article impressionable_type: Article
impressionable_id: 1 impressionable_id: 1
request_hash: a1
ip_address: 127.0.0.1 ip_address: 127.0.0.1
created_at: 2010-01-01 created_at: 2010-01-01
impression9: impression9:
impressionable_type: Article impressionable_type: Article
impressionable_id: 1 impressionable_id: 1
request_hash: a1
ip_address: 127.0.0.1 ip_address: 127.0.0.1
created_at: 2011-01-03 created_at: 2011-01-03
impression10: impression10:
impressionable_type: Article impressionable_type: Article
impressionable_id: 1 impressionable_id: 1
request_hash: a9
ip_address: 127.0.0.8
created_at: 2010-01-01
impression11:
impressionable_type: Article
impressionable_id: 1
request_hash: a10
ip_address: 127.0.0.1 ip_address: 127.0.0.1
created_at: 2010-01-01 created_at: 2010-01-01

View File

@ -9,7 +9,7 @@ describe Impression do
it "should save a blank impression for an Article that has 10 impressions" do it "should save a blank impression for an Article that has 10 impressions" do
@article.impressions.create @article.impressions.create
@article.impressions.size.should eq 11 @article.impressions.size.should eq 12
end end
it "should save an impression with a message" do it "should save an impression with a message" do
@ -17,27 +17,31 @@ describe Impression do
@article.impressions.last.message.should eq "test message" @article.impressions.last.message.should eq "test message"
end end
it "should return the view count with no date range specified" do it "should return the impression count with no date range specified" do
@article.impression_count.should eq 10 @article.impression_count.should eq 11
end end
it "should return unique view count with no date range specified" do it "should return unique impression count with no date range specified" do
@article.unique_impression_count.should eq 7 @article.unique_impression_count.should eq 9
end end
it "should return view count with only start date specified" do it "should return impression count with only start date specified" do
@article.impression_count("2011-01-01").should eq 8 @article.impression_count("2011-01-01").should eq 8
end end
it "should return view count with whole date range specified" do it "should return impression count with whole date range specified" do
@article.impression_count("2011-01-01","2011-01-02").should eq 7 @article.impression_count("2011-01-01","2011-01-02").should eq 7
end end
it "should return unique view count with only start date specified" do it "should return unique impression count with only start date specified" do
@article.unique_impression_count("2011-01-01").should eq 7 @article.unique_impression_count("2011-01-01").should eq 7
end end
it "should return unique view count with date range specified" do it "should return unique impression count with date range specified" do
@article.unique_impression_count("2011-01-01","2011-01-02").should eq 7 @article.unique_impression_count("2011-01-01","2011-01-02").should eq 7
end end
it "should return unique impression count using ip address (which in turn eliminates duplicate request_hashes)" do
@article.unique_impression_count_ip.should eq 8
end
end end