Skip to content

Commit fcfac17

Browse files
committed
Merge pull request #182 from seuros/analitycs
Fix bug with analytics.
2 parents df6f09b + 5bfa40c commit fcfac17

File tree

4 files changed

+89
-99
lines changed

4 files changed

+89
-99
lines changed

app/models/concerns/team_analytics.rb

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
module TeamAnalytics
2+
extend ActiveSupport::Concern
3+
# TODO, Get out out redis
4+
included do
5+
SECTIONS = %w(team-details members about-members big-headline big-quote challenges favourite-benefits
6+
organization-style office-images jobs stack protips why-work interview-steps
7+
locations team-blog)
8+
9+
def record_exit(viewer, exit_url, exit_target_type, furthest_scrolled, time_spent)
10+
epoch_now = Time.now.to_i
11+
user_id = (viewer.respond_to?(:id) && viewer.try(:id)) || viewer
12+
data = visitor_data(exit_url, exit_target_type, furthest_scrolled, time_spent, user_id, epoch_now, nil)
13+
Redis.current.zadd(user_detail_views_key, epoch_now, data)
14+
end
15+
16+
def detailed_visitors(since = 0)
17+
Redis.current.zrangebyscore(user_detail_views_key, since, Time.now.to_i).map do |visitor_string|
18+
visitor = some_crappy_method(visitor_string)
19+
visitor[:user] = identify_visitor(visitor[:user_id])
20+
visitor
21+
end
22+
end
23+
24+
def simple_visitors(since = 0)
25+
all_visitors = Redis.current.zrangebyscore(user_views_key, since, Time.now.to_i, withscores: true) +
26+
Redis.current.zrangebyscore(user_anon_views_key, since, Time.now.to_i, withscores: true)
27+
Hash[*all_visitors.flatten].map do |viewer_id, timestamp|
28+
visitor_data(nil, nil, nil, 0, viewer_id, timestamp, identify_visitor(viewer_id))
29+
end
30+
end
31+
32+
def visitors(since = 0)
33+
detailed_visitors = self.detailed_visitors
34+
first_detailed_visit = detailed_visitors.last.nil? ? updated_at : detailed_visitors.first[:visited_at]
35+
self.detailed_visitors(since) + simple_visitors(since == 0 ? first_detailed_visit.to_i : since)
36+
end
37+
38+
def aggregate_visitors(since = 0)
39+
aggregate = {}
40+
visitors(since).map do |visitor|
41+
user_id = visitor[:user_id].to_i
42+
aggregate[user_id] ||= visitor
43+
aggregate[user_id].merge!(visitor) do |key, old, new|
44+
case key
45+
when :time_spent
46+
old.to_i + new.to_i
47+
when :visited_at
48+
[old.to_i, new.to_i].max
49+
when :furthest_scrolled
50+
SECTIONS[[SECTIONS.index(old) || 0, SECTIONS.index(new) || 0].max]
51+
else
52+
old.nil? ? new : old
53+
end
54+
end
55+
aggregate[user_id][:visits] ||= 0
56+
aggregate[user_id][:visits] += 1
57+
58+
end
59+
aggregate.values.sort { |a, b| b[:visited_at] <=> a[:visited_at] }
60+
end
61+
62+
def sections_up_to(furthest)
63+
SECTIONS.slice(0, SECTIONS.index(furthest))
64+
end
65+
66+
def number_of_completed_sections(*excluded_sections)
67+
completed_sections = 0
68+
69+
sections = (SECTIONS - excluded_sections).map do |section|
70+
"has_#{section.gsub(/-/, '_')}?"
71+
end
72+
sections.each do |section_complete|
73+
completed_sections += 1 if self.respond_to?(section_complete) &&
74+
public_send(section_complete)
75+
end
76+
completed_sections
77+
end
78+
79+
private
80+
def some_crappy_method(hash_string_to_parse)
81+
# This code is bad and Mike should feel bad.
82+
JSON.parse('{' + hash_string_to_parse.gsub(/^{|}$/, '').split(', ')
83+
.map { |pair| pair.split('=>') }
84+
.map { |k, v| [k.gsub(/^:(\w*)/, '"\1"'), v == 'nil' ? 'null' : v].join(': ') }.join(', ') + '}')
85+
end
86+
end
87+
end

app/models/team.rb

Lines changed: 2 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Team
88
include Tire::Model::Search
99
include LeaderboardRedisRank
1010
include SearchModule
11+
include TeamAnalytics
1112

1213
# Disabled Team indexing because it slows down updates
1314
# we should BG this
@@ -403,7 +404,7 @@ def has_featured_links?
403404
end
404405

405406
def has_upcoming_events?
406-
!upcoming_events.blank?
407+
false
407408
end
408409

409410
def has_team_blog?
@@ -717,13 +718,6 @@ def fix_website_url!
717718
end
718719
end
719720

720-
#Will delete , it not even working
721-
def upcoming_events
722-
team_members.collect do |member|
723-
724-
end
725-
end
726-
727721
def active_jobs
728722
jobs[0...4]
729723
end
@@ -741,59 +735,10 @@ def all_jobs
741735
Opportunity.where(team_document_id: self.id.to_s).order('created_at DESC')
742736
end
743737

744-
def record_exit(viewer, exit_url, exit_target_type, furthest_scrolled, time_spent)
745-
epoch_now = Time.now.to_i
746-
data = visitor_data(exit_url, exit_target_type, furthest_scrolled, time_spent, (viewer.respond_to?(:id) && viewer.try(:id)) || viewer, epoch_now, nil)
747-
Redis.current.zadd(user_detail_views_key, epoch_now, data)
748-
end
749-
750-
def detailed_visitors(since = 0)
751-
Redis.current.zrangebyscore(user_detail_views_key, since, Time.now.to_i).map do |visitor_string|
752-
visitor = HashStringParser.better_than_eval(visitor_string)
753-
visitor[:user] = identify_visitor(visitor[:user_id])
754-
visitor
755-
end
756-
end
757-
758-
def simple_visitors(since = 0)
759-
all_visitors = Redis.current.zrangebyscore(user_views_key, since, Time.now.to_i, withscores: true) + fRedis.current.zrangebyscore(user_anon_views_key, since, Time.now.to_i, withscores: true)
760-
Hash[*all_visitors.flatten].collect do |viewer_id, timestamp|
761-
visitor_data(nil, nil, nil, 0, viewer_id, timestamp, identify_visitor(viewer_id))
762-
end
763-
end
764738

765-
def visitors(since=0)
766-
detailed_visitors = self.detailed_visitors
767-
first_detailed_visit = detailed_visitors.last.nil? ? self.updated_at : detailed_visitors.first[:visited_at]
768-
self.detailed_visitors(since) + self.simple_visitors(since == 0 ? first_detailed_visit.to_i : since)
769-
end
770739

771-
SECTIONS = %w(team-details members about-members big-headline big-quote challenges favourite-benefits organization-style office-images jobs stack protips why-work interview-steps locations team-blog)
772740
SECTION_FIELDS = %w(about headline big_quote our_challenge benefit_description_1 organization_way office_photos stack_list reason_name_1 interview_steps team_locations blog_feed)
773741

774-
def aggregate_visitors(since=0)
775-
aggregate ={}
776-
visitors(since).map do |visitor|
777-
user_id = visitor[:user_id].to_i
778-
aggregate[user_id] ||= visitor
779-
aggregate[user_id].merge!(visitor) do |key, old, new|
780-
case key
781-
when :time_spent
782-
old.to_i + new.to_i
783-
when :visited_at
784-
[old.to_i, new.to_i].max
785-
when :furthest_scrolled
786-
SECTIONS[[SECTIONS.index(old) || 0, SECTIONS.index(new) || 0].max]
787-
else
788-
old.nil? ? new : old
789-
end
790-
end
791-
aggregate[user_id][:visits] ||= 0
792-
aggregate[user_id][:visits] += 1
793-
794-
end
795-
aggregate.values.sort { |a, b| b[:visited_at] <=> a[:visited_at] }
796-
end
797742

798743
def visitors_interested_in_jobs
799744
aggregate_visitors.select { |visitor| visitor[:exit_target_type] == 'job-opportunity' }.collect { |visitor| visitor[:user_id] }
@@ -807,10 +752,6 @@ def click_through_rate
807752
self.visitors_interested_in_jobs.count/self.total_views(self.upgraded_at)
808753
end
809754

810-
def sections_up_to(furthest)
811-
SECTIONS.slice(0, SECTIONS.index(furthest))
812-
end
813-
814755
def coderwall?
815756
slug == 'coderwall'
816757
end
@@ -846,15 +787,6 @@ def has_specified_enough_info?
846787
number_of_completed_sections >= 6
847788
end
848789

849-
def number_of_completed_sections(*excluded_sections)
850-
completed_sections = 0
851-
852-
(SECTIONS - excluded_sections).map { |section| "has_#{section.gsub(/-/, '_')}?" }.each do |section_complete|
853-
completed_sections +=1 if self.respond_to?(section_complete) && self.send(section_complete)
854-
end
855-
completed_sections
856-
end
857-
858790
def has_team_details?
859791
has_external_link? and !self.about.nil? and !self.avatar.nil?
860792
end

lib/hash_string_parser.rb

Lines changed: 0 additions & 10 deletions
This file was deleted.

spec/lib/hash_string_parser_spec.rb

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)