mixpanel analytics class and tracking for page view, signin, signup, subscription and contact message

jamesperet 9 years ago
parent
commit
5c8cd9ab6a

+ 1 - 0
Gemfile

@@ -63,6 +63,7 @@ gem 'rails_12factor', group: :production
63 63
 gem 'gibbon'
64 64
 gem 'redis'
65 65
 gem 'resque', '~> 1.22.0', :require => "resque/server"
66
+gem 'mixpanel-ruby'
66 67
 
67 68
 group :test do
68 69
   gem "rspec"

+ 2 - 0
Gemfile.lock

@@ -158,6 +158,7 @@ GEM
158 158
       subexec (~> 0.2.1)
159 159
     mini_portile (0.6.0)
160 160
     minitest (4.7.5)
161
+    mixpanel-ruby (1.6.0)
161 162
     multi_json (1.10.1)
162 163
     multi_test (0.1.1)
163 164
     multi_xml (0.5.5)
@@ -316,6 +317,7 @@ DEPENDENCIES
316 317
   less-rails
317 318
   letter_opener
318 319
   mini_magick
320
+  mixpanel-ruby
319 321
   pg
320 322
   rails (= 4.0.4)
321 323
   rails_12factor

+ 16 - 0
app/controllers/application_controller.rb

@@ -11,6 +11,8 @@ class ApplicationController < ActionController::Base
11 11
   
12 12
   before_filter :configure_permitted_parameters, if: :devise_controller?
13 13
   
14
+  before_filter :analytics
15
+  
14 16
   def configure_permitted_parameters
15 17
     devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:first_name, :last_name, :email, :current_password, :avatar, :password, :password_confirmation, :avatar_tmp, :avatar_processing) }
16 18
     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :email, :password) }
@@ -22,6 +24,20 @@ class ApplicationController < ActionController::Base
22 24
     I18n.default_locale = @config.default_language
23 25
   end
24 26
   
27
+  def analytics
28
+    if current_user != nil
29
+      id = current_user.full_name
30
+    else
31
+      id = 'Guest_'+ Time.now.to_i.to_s
32
+    end
33
+    @analytics ||= Analytics.new(id)
34
+  end
35
+  
36
+  def after_sign_in_path_for(resource_or_scope)
37
+      @analytics.track_user_sign_in(current_user)
38
+      stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
39
+  end
40
+  
25 41
   protected
26 42
 
27 43
     def check_admin_mode

+ 1 - 0
app/controllers/start_controller.rb

@@ -3,6 +3,7 @@ class StartController < ApplicationController
3 3
   def index
4 4
     @blog_posts = BlogPost.order('created_at DESC').take(4)
5 5
     @subscription = Subscription.new
6
+    @analytics.track('Home page view')
6 7
   end
7 8
 
8 9
 end

+ 2 - 0
app/controllers/users/sessions_controller.rb

@@ -9,4 +9,6 @@ class Users::SessionsController < Devise::SessionsController
9 9
   # def create
10 10
   #   super
11 11
   # end
12
+
13
+  
12 14
 end

+ 38 - 0
app/models/analytics.rb

@@ -0,0 +1,38 @@
1
+class Analytics 
2
+
3
+  require 'mixpanel-ruby'
4
+  
5
+  
6
+  def initialize(id)
7
+    @tracker = Mixpanel::Tracker.new(ENV["MIXPANEL_PROJECT_TOKEN"])
8
+    @id = id
9
+  end 
10
+  
11
+  def track(page)
12
+    @tracker.track(@id, page)
13
+  end
14
+  
15
+  def track_user_registration(user)
16
+    identify(user)
17
+    @tracker.track(user.full_name, 'User registration')
18
+  end 
19
+  
20
+  def track_user_sign_in(user)
21
+    identify(user)
22
+    @tracker.track(user.full_name, 'User sign in')
23
+    @tracker.people.plus_one(user.full_name, "logins")
24
+  end 
25
+  
26
+  private 
27
+  
28
+  def identify(user)
29
+    @tracker.people.set(user.full_name, {
30
+        '$first_name' => user.first_name,
31
+        '$last_name' => user.last_name,
32
+        '$email' => user.email,
33
+        'admin' => user.admin.to_s
34
+    })
35
+   
36
+  end 
37
+  
38
+end

+ 7 - 0
app/models/contact_message.rb

@@ -3,6 +3,7 @@ class ContactMessage < ActiveRecord::Base
3 3
   
4 4
   after_create do
5 5
     send_contact_message
6
+    track_analytics
6 7
   end
7 8
   
8 9
   def name
@@ -18,4 +19,10 @@ class ContactMessage < ActiveRecord::Base
18 19
     Resque.enqueue(SendContactMessage, self.id)
19 20
   end
20 21
   
22
+  def track_analytics
23
+    # Mixpanel Tracking Analytics
24
+    @analytics = Analytics.new(self.user != nil ? self.user.full_name : ('Guest_'+ Time.now.to_i.to_s))
25
+    @analytics.track('Contact message sent')
26
+  end
27
+  
21 28
 end

+ 7 - 0
app/models/subscription.rb

@@ -3,6 +3,7 @@ class Subscription < ActiveRecord::Base
3 3
   after_create do
4 4
     subscribe_to_mailchimp
5 5
     send_newsletter_subscription_email
6
+    track_analytics
6 7
   end
7 8
   
8 9
   def full_name
@@ -28,4 +29,10 @@ class Subscription < ActiveRecord::Base
28 29
     end
29 30
   end
30 31
   
32
+  def track_analytics
33
+    # Mixpanel Tracking Analytics
34
+    @analytics = Analytics.new(self.full_name)
35
+    @analytics.track('Newsletter subscription')
36
+  end
37
+  
31 38
 end

+ 2 - 0
app/models/user.rb

@@ -29,6 +29,8 @@ class User < ActiveRecord::Base
29 29
       Subscription.create(first_name: self.first_name, last_name: self.last_name, email: self.email)
30 30
     end
31 31
     # Mixpanel Tracking Analytics
32
+    @analytics = Analytics.new(self.full_name)
33
+    @analytics.track_user_registration(self)
32 34
   end
33 35
   
34 36
 end