ahoy_email 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d178d2bd4a2631bad0bce962cd35daecc019034270530f350596d14172e46ca0
4
- data.tar.gz: af5eb88ec75a4100c5215a71e39fb825d7c7e4823fd601c3cd6473b7409db3dd
3
+ metadata.gz: 69eb9c77891a37c75ef7f7683211c90159cbb8aefd64fa14a0b1b35d2e16d0eb
4
+ data.tar.gz: a937ba8e9362d1c2551f5c193b9a3bbf1b4132f3df9ddb77f3f838f633fb2f07
5
5
  SHA512:
6
- metadata.gz: bcc3ac25d9a8323a4da3a4331a338ca377a375033886c9e447945654897ea3b89e178ffe7a4e02336838ba27e1b8f8dda0aef0f5261f1adb8c66167dbe758f98
7
- data.tar.gz: b433434716662eb90d9309c6dfdefe61752496777206e43d1d1d9ddadfee9aa664cf1daa1272d4df62a393d6e979e62932194829dd68b84720fd47661fada6e0
6
+ metadata.gz: 4b12c970be432d7db95fdb8eaade5766f69fc7826a94f41474ba5c2a35da1ea85b6317f83250cb1c8f41c7fe876028b1377958709d2c84ff9bd303926ac8ab5a
7
+ data.tar.gz: 96dcc647ee3777e27b14d7628025d96ea92efce7160d36da0b066b72d9dc5aac8196008699a93673d81d0fdcb909aa69874b349dffdbaf0e32e4d7511a32df5a
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 2.0.1 (2021-03-08)
2
+
3
+ - Added database subscriber
4
+
1
5
  ## 2.0.0 (2021-03-06)
2
6
 
3
7
  - Made `to` field encrypted by default for new installations
data/README.md CHANGED
@@ -200,7 +200,23 @@ Skip specific links with:
200
200
 
201
201
  ## Click Analytics
202
202
 
203
- You can track click-through rate to see how well campaigns are performing. Stats can be stored in any data store, and there’s a built-in integration with Redis.
203
+ You can track click-through rate to see how well campaigns are performing. Stats can be stored in your database, Redis, or any other data store.
204
+
205
+ #### Database
206
+
207
+ Run:
208
+
209
+ ```sh
210
+ rails generate ahoy:clicks
211
+ rails db:migrate
212
+ ```
213
+
214
+ And create `config/initializers/ahoy_email.rb` with:
215
+
216
+ ```ruby
217
+ AhoyEmail.subscribers << AhoyEmail::DatabaseSubscriber
218
+ AhoyEmail.api = true
219
+ ```
204
220
 
205
221
  #### Redis
206
222
 
@@ -232,7 +248,7 @@ class EmailSubscriber
232
248
  # your code
233
249
  end
234
250
 
235
- def stats(campaign = nil)
251
+ def stats(campaign)
236
252
  # optional, for AhoyEmail.stats
237
253
  end
238
254
  end
@@ -241,7 +257,7 @@ AhoyEmail.subscribers << EmailSubscriber
241
257
  AhoyEmail.api = true
242
258
  ````
243
259
 
244
- ### Setup
260
+ ### Usage
245
261
 
246
262
  Add to mailers you want to track
247
263
 
@@ -251,6 +267,8 @@ class CouponMailer < ApplicationMailer
251
267
  end
252
268
  ```
253
269
 
270
+ If storing stats in the database, the mailer should also use `has_history`
271
+
254
272
  Use only and except to limit actions
255
273
 
256
274
  ```ruby
@@ -287,13 +305,7 @@ AhoyEmail.default_options[:url_options] = {host: "mydomain.com"}
287
305
 
288
306
  ### Stats
289
307
 
290
- Get stats for all campaigns
291
-
292
- ```ruby
293
- AhoyEmail.stats
294
- ```
295
-
296
- Get stats for a specific campaign
308
+ Get stats for a campaign
297
309
 
298
310
  ```ruby
299
311
  AhoyEmail.stats("my-campaign")
@@ -0,0 +1,5 @@
1
+ module Ahoy
2
+ class Click < ActiveRecord::Base
3
+ self.table_name = "ahoy_clicks"
4
+ end
5
+ end
data/lib/ahoy_email.rb CHANGED
@@ -16,6 +16,7 @@ require "ahoy_email/utils"
16
16
  require "ahoy_email/version"
17
17
 
18
18
  # subscribers
19
+ require "ahoy_email/database_subscriber"
19
20
  require "ahoy_email/message_subscriber"
20
21
  require "ahoy_email/redis_subscriber"
21
22
 
@@ -0,0 +1,31 @@
1
+ module AhoyEmail
2
+ class DatabaseSubscriber
3
+ def track_send(event)
4
+ # use has_history to store on Ahoy::Messages
5
+ end
6
+
7
+ def track_click(event)
8
+ Ahoy::Click.create!(campaign: event[:campaign], token: event[:token])
9
+ end
10
+
11
+ def stats(campaign)
12
+ sends = Ahoy::Message.where(campaign: campaign).count
13
+ result = Ahoy::Click.where(campaign: campaign).select("COUNT(*) AS clicks, COUNT(DISTINCT token) AS unique_clicks").to_a[0]
14
+ clicks = result.clicks
15
+ unique_clicks = result.unique_clicks
16
+
17
+ if sends > 0 || clicks > 0
18
+ {
19
+ sends: sends,
20
+ clicks: clicks,
21
+ unique_clicks: unique_clicks,
22
+ ctr: 100 * unique_clicks / sends.to_f
23
+ }
24
+ end
25
+ end
26
+
27
+ def campaigns
28
+ Ahoy::Message.where.not(campaign: nil).distinct.pluck(:campaign)
29
+ end
30
+ end
31
+ end
@@ -72,7 +72,7 @@ module AhoyEmail
72
72
  sends: sends.value.to_i,
73
73
  clicks: clicks.value.to_i,
74
74
  unique_clicks: unique_clicks.value,
75
- ctr: (100.0 * unique_clicks.value / sends.value.to_f).round(1)
75
+ ctr: 100 * unique_clicks.value / sends.value.to_f
76
76
  }
77
77
  end
78
78
  end
@@ -1,3 +1,3 @@
1
1
  module AhoyEmail
2
- VERSION = "2.0.0"
2
+ VERSION = "2.0.1"
3
3
  end
@@ -0,0 +1,20 @@
1
+ require "rails/generators/active_record"
2
+
3
+ module Ahoy
4
+ module Generators
5
+ module Clicks
6
+ class ActiverecordGenerator < Rails::Generators::Base
7
+ include ActiveRecord::Generators::Migration
8
+ source_root File.join(__dir__, "templates")
9
+
10
+ def copy_migration
11
+ migration_template "migration.rb", "db/migrate/create_ahoy_clicks.rb", migration_version: migration_version
12
+ end
13
+
14
+ def migration_version
15
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :ahoy_clicks do |t|
4
+ t.string :campaign, index: true
5
+ t.string :token
6
+ end
7
+
8
+ add_column :ahoy_messages, :campaign, :string
9
+ add_index :ahoy_messages, :campaign
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require "rails/generators"
2
+
3
+ module Ahoy
4
+ module Generators
5
+ class ClicksGenerator < Rails::Generators::Base
6
+ def copy_templates
7
+ invoke "ahoy:clicks:activerecord"
8
+ end
9
+ end
10
+ end
11
+ end
@@ -3,8 +3,6 @@ require "rails/generators"
3
3
  module Ahoy
4
4
  module Generators
5
5
  class MessagesGenerator < Rails::Generators::Base
6
- source_root File.join(__dir__, "templates")
7
-
8
6
  class_option :unencrypted, type: :boolean
9
7
 
10
8
  def copy_templates
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ahoy_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-06 00:00:00.000000000 Z
11
+ date: 2021-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionmailer
@@ -77,9 +77,11 @@ files:
77
77
  - LICENSE.txt
78
78
  - README.md
79
79
  - app/controllers/ahoy/messages_controller.rb
80
+ - app/models/ahoy/click.rb
80
81
  - app/models/ahoy/message.rb
81
82
  - config/routes.rb
82
83
  - lib/ahoy_email.rb
84
+ - lib/ahoy_email/database_subscriber.rb
83
85
  - lib/ahoy_email/engine.rb
84
86
  - lib/ahoy_email/mailer.rb
85
87
  - lib/ahoy_email/message_subscriber.rb
@@ -89,6 +91,9 @@ files:
89
91
  - lib/ahoy_email/tracker.rb
90
92
  - lib/ahoy_email/utils.rb
91
93
  - lib/ahoy_email/version.rb
94
+ - lib/generators/ahoy/clicks/activerecord_generator.rb
95
+ - lib/generators/ahoy/clicks/templates/migration.rb.tt
96
+ - lib/generators/ahoy/clicks_generator.rb
92
97
  - lib/generators/ahoy/messages/activerecord_generator.rb
93
98
  - lib/generators/ahoy/messages/mongoid_generator.rb
94
99
  - lib/generators/ahoy/messages/templates/migration.rb.tt