Class: ActionMailer::MixpanelInterceptor

Inherits:
Object
  • Object
show all
Defined in:
lib/action_mailer/mixpanel_interceptor.rb

Class Method Summary collapse

Class Method Details

.activate!(token) ⇒ Object



14
15
16
17
# File 'lib/action_mailer/mixpanel_interceptor.rb', line 14

def activate!(token)
  self.token = token
  ::ActionMailer::Base.register_interceptor(self)
end

.delivering_email(mail) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/action_mailer/mixpanel_interceptor.rb', line 19

def delivering_email(mail)
  # Remove all the Mixpanel headers from the email
  opts = ::Mixpanel::Mail::OPTIONS.inject({}) do |sum, key|
    if field = pop_mp_header(mail, key)
      sum[key] = field.value
    end
    sum
  end

  # Skip Mixpanel if the campaign is not specified
  return unless opts['campaign']

  # Skip Mixpanel if we don't have HTML
  html = mail.html_part ? mail.html_part.decoded : nil
  return unless html.present?

  # Generate email distinct_id for Mixpanel
  id = Digest::MD5.hexdigest(mail.header['To'].to_s)

  begin
    mail.html_part.body = mp_mail.add_tracking(id, html, opts)
  rescue => e
    mail.html_part.body = html
    logger.warn("Failed to Mixpanelize Mail: #{e}")
    logger.debug(e.backtrace.join("\n"))
  end
end