ahoy_email 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d178d2bd4a2631bad0bce962cd35daecc019034270530f350596d14172e46ca0
4
- data.tar.gz: af5eb88ec75a4100c5215a71e39fb825d7c7e4823fd601c3cd6473b7409db3dd
3
+ metadata.gz: b84f8c566d81a470ba99f4318c2d7dd3f1b3fae6b05155491f486cbdbff7c0a6
4
+ data.tar.gz: 8ab62441cf6df6b4ab6af401a9cbed8864ac634d28dd728f6b83bfd743dce284
5
5
  SHA512:
6
- metadata.gz: bcc3ac25d9a8323a4da3a4331a338ca377a375033886c9e447945654897ea3b89e178ffe7a4e02336838ba27e1b8f8dda0aef0f5261f1adb8c66167dbe758f98
7
- data.tar.gz: b433434716662eb90d9309c6dfdefe61752496777206e43d1d1d9ddadfee9aa664cf1daa1272d4df62a393d6e979e62932194829dd68b84720fd47661fada6e0
6
+ metadata.gz: cf70b92f9ba7b0d91324eae1927fe49464c3b3a76293272570559526064d20a1df5a3859a350dac9d6708113b787e8566a8db58cb6bc881d463ae2f4581865c8
7
+ data.tar.gz: 62019c44c7e66c5d3858c9862d20276dbb876d024ca21d1d8691e3969944778c493fa3e988e0e22e2253c7515a6d453d2f11c794867941fa024017c9f28eeb02
data/CHANGELOG.md CHANGED
@@ -1,3 +1,19 @@
1
+ ## 2.1.0 (2021-09-25)
2
+
3
+ - Fixed mailer `default_url_options` not being applied to click links
4
+
5
+ ## 2.0.3 (2021-03-31)
6
+
7
+ - Fixed `utm_params` and `track_clicks` stripping `<head>` tags from messages
8
+
9
+ ## 2.0.2 (2021-03-14)
10
+
11
+ - Added support for Mongoid to database subscriber
12
+
13
+ ## 2.0.1 (2021-03-08)
14
+
15
+ - Added database subscriber
16
+
1
17
  ## 2.0.0 (2021-03-06)
2
18
 
3
19
  - 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
@@ -267,6 +285,14 @@ class CouponMailer < ApplicationMailer
267
285
  end
268
286
  ```
269
287
 
288
+ You can also use a proc
289
+
290
+ ```ruby
291
+ class CouponMailer < ApplicationMailer
292
+ track_clicks campaign: -> { "coupon-#{action_name}" }
293
+ end
294
+ ```
295
+
270
296
  Skip specific links with:
271
297
 
272
298
  ```erb
@@ -287,13 +313,7 @@ AhoyEmail.default_options[:url_options] = {host: "mydomain.com"}
287
313
 
288
314
  ### Stats
289
315
 
290
- Get stats for all campaigns
291
-
292
- ```ruby
293
- AhoyEmail.stats
294
- ```
295
-
296
- Get stats for a specific campaign
316
+ Get stats for a campaign
297
317
 
298
318
  ```ruby
299
319
  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
@@ -0,0 +1,42 @@
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
+
14
+ if defined?(ActiveRecord) && Ahoy::Click < ActiveRecord::Base
15
+ result = Ahoy::Click.where(campaign: campaign).select("COUNT(*) AS clicks, COUNT(DISTINCT token) AS unique_clicks").to_a[0]
16
+ clicks = result.clicks
17
+ unique_clicks = result.unique_clicks
18
+ else
19
+ clicks = Ahoy::Click.where(campaign: campaign).count
20
+ # TODO use aggregation framework
21
+ unique_clicks = Ahoy::Click.where(campaign: campaign).distinct(:token).count
22
+ end
23
+
24
+ if sends > 0 || clicks > 0
25
+ {
26
+ sends: sends,
27
+ clicks: clicks,
28
+ unique_clicks: unique_clicks,
29
+ ctr: 100 * unique_clicks / sends.to_f
30
+ }
31
+ end
32
+ end
33
+
34
+ def campaigns
35
+ if defined?(ActiveRecord) && Ahoy::Message < ActiveRecord::Base
36
+ Ahoy::Message.where.not(campaign: nil).distinct.pluck(:campaign)
37
+ else
38
+ Ahoy::Message.where(campaign: {"$ne" => nil}).distinct(:campaign)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -53,7 +53,7 @@ module AhoyEmail
53
53
  if html_part?
54
54
  part = message.html_part || message
55
55
 
56
- doc = Nokogiri::HTML::DocumentFragment.parse(part.body.raw_source)
56
+ doc = Nokogiri::HTML::Document.parse(part.body.raw_source)
57
57
  doc.css("a[href]").each do |link|
58
58
  uri = parse_uri(link["href"])
59
59
  next unless trackable?(uri)
@@ -123,7 +123,7 @@ module AhoyEmail
123
123
  end
124
124
 
125
125
  def url_for(opt)
126
- opt = (ActionMailer::Base.default_url_options || {})
126
+ opt = (mailer.default_url_options || {})
127
127
  .merge(options[:url_options])
128
128
  .merge(opt)
129
129
  AhoyEmail::Engine.routes.url_helpers.url_for(opt)
@@ -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.1.0"
3
3
  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,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,15 @@
1
+ require "rails/generators"
2
+
3
+ module Ahoy
4
+ module Generators
5
+ module Clicks
6
+ class MongoidGenerator < Rails::Generators::Base
7
+ source_root File.join(__dir__, "templates")
8
+
9
+ def copy_templates
10
+ template "mongoid.rb", "app/models/ahoy/click.rb"
11
+ end
12
+ end
13
+ end
14
+ end
15
+ 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,8 @@
1
+ class Ahoy::Click
2
+ include Mongoid::Document
3
+
4
+ field :campaign, type: String
5
+ field :token, type: String
6
+
7
+ index({campaign: 1})
8
+ end
@@ -0,0 +1,37 @@
1
+ require "rails/generators"
2
+
3
+ module Ahoy
4
+ module Generators
5
+ class ClicksGenerator < Rails::Generators::Base
6
+ def copy_templates
7
+ activerecord = defined?(ActiveRecord)
8
+ mongoid = defined?(Mongoid)
9
+
10
+ selection =
11
+ if activerecord && mongoid
12
+ puts <<-MSG
13
+
14
+ Which data store would you like to use?
15
+ 1. Active Record (default)
16
+ 2. Mongoid
17
+ MSG
18
+
19
+ ask(">")
20
+ elsif activerecord
21
+ "1"
22
+ else
23
+ "2"
24
+ end
25
+
26
+ case selection
27
+ when "", "1"
28
+ invoke "ahoy:clicks:activerecord"
29
+ when "2"
30
+ invoke "ahoy:clicks:mongoid"
31
+ else
32
+ abort "Error: must enter a number [1-2]"
33
+ end
34
+ end
35
+ end
36
+ end
37
+ 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.1.0
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-09-25 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,11 @@ 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/mongoid_generator.rb
96
+ - lib/generators/ahoy/clicks/templates/migration.rb.tt
97
+ - lib/generators/ahoy/clicks/templates/mongoid.rb.tt
98
+ - lib/generators/ahoy/clicks_generator.rb
92
99
  - lib/generators/ahoy/messages/activerecord_generator.rb
93
100
  - lib/generators/ahoy/messages/mongoid_generator.rb
94
101
  - lib/generators/ahoy/messages/templates/migration.rb.tt
@@ -115,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
122
  - !ruby/object:Gem::Version
116
123
  version: '0'
117
124
  requirements: []
118
- rubygems_version: 3.2.3
125
+ rubygems_version: 3.2.22
119
126
  signing_key:
120
127
  specification_version: 4
121
128
  summary: First-party email analytics for Rails