Module: MixpanelRails::InstanceMethods

Defined in:
lib/mixpanel_rails.rb

Instance Method Summary collapse

Instance Method Details

#mixpanel_queueObject



55
56
57
# File 'lib/mixpanel_rails.rb', line 55

def mixpanel_queue
  session[:mixpanel_queue] ||= []
end

#process_mixpanel_queueObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mixpanel_rails.rb', line 31

def process_mixpanel_queue
  unless response.redirect_url && request.host == URI.parse(response.redirect_url).host
    mixpanel = Mixpanel::Tracker.new(MixpanelRails::Railtie.config.mixpanel_rails.token, request.env, true)
    distinct_id = mixpanel_distinct_id.bind(self).call
    params = {}
    params[:distinct_id] = distinct_id if distinct_id
    params.merge!(register_with_mixpanel)
    if request.env["Rack-Middleware-PDFKit"] || response.redirect_url
      mixpanel_queue.each {|s| mixpanel.track_event(s, params) }
    else
      name_tag = mixpanel_name_tag.bind(self).call
      mixpanel.append_api(:register, params)
      mixpanel.append_api(:name_tag, name_tag) if name_tag
      mixpanel_queue.each {|s| mixpanel.append_api :track, s }
    end
    session.delete :register_with_mixpanel
    session.delete :mixpanel_queue
  end
end

#register_with_mixpanelObject



51
52
53
# File 'lib/mixpanel_rails.rb', line 51

def register_with_mixpanel
  session[:register_with_mixpanel] ||= {}
end

#track_with_mixpanel(s) ⇒ Object



27
28
29
# File 'lib/mixpanel_rails.rb', line 27

def track_with_mixpanel(s)
  mixpanel_queue << s
end