Class: AhoyEmail::DatabaseSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/ahoy_email/database_subscriber.rb

Instance Method Summary collapse

Instance Method Details

#campaignsObject



34
35
36
37
38
39
40
# File 'lib/ahoy_email/database_subscriber.rb', line 34

def campaigns
  if defined?(ActiveRecord) && Ahoy::Message < ActiveRecord::Base
    Ahoy::Message.where.not(campaign: nil).distinct.pluck(:campaign)
  else
    Ahoy::Message.where(campaign: {"$ne" => nil}).distinct(:campaign)
  end
end

#stats(campaign) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ahoy_email/database_subscriber.rb', line 11

def stats(campaign)
  sends = Ahoy::Message.where(campaign: campaign).count

  if defined?(ActiveRecord) && Ahoy::Click < ActiveRecord::Base
    result = Ahoy::Click.where(campaign: campaign).select("COUNT(*) AS clicks, COUNT(DISTINCT token) AS unique_clicks").to_a[0]
    clicks = result.clicks
    unique_clicks = result.unique_clicks
  else
    clicks = Ahoy::Click.where(campaign: campaign).count
    # TODO use aggregation framework
    unique_clicks = Ahoy::Click.where(campaign: campaign).distinct(:token).count
  end

  if sends > 0 || clicks > 0
    {
      sends: sends,
      clicks: clicks,
      unique_clicks: unique_clicks,
      ctr: 100 * unique_clicks / sends.to_f
    }
  end
end

#track_click(event) ⇒ Object



7
8
9
# File 'lib/ahoy_email/database_subscriber.rb', line 7

def track_click(event)
  Ahoy::Click.create!(campaign: event[:campaign], token: event[:token])
end

#track_send(event) ⇒ Object



3
4
5
# File 'lib/ahoy_email/database_subscriber.rb', line 3

def track_send(event)
  # use has_history to store on Ahoy::Messages
end