Skip to content

Commit aa064fb

Browse files
authored
[feature] email tracking opt-in
* basic implementation for email tracking opt-in # tracking pixel <img width="1540" alt="Screen Shot 2024-10-06 at 12 16 19 PM" src="https://github.com/user-attachments/assets/531b63a2-348d-4953-a0ca-eca4c6d5a922"> # UI <img width="1157" alt="Screen Shot 2024-10-06 at 12 16 42 PM" src="https://github.com/user-attachments/assets/d7b9cb70-12bd-4d7b-a7a8-b420eb7d73c6">
1 parent 5fd4f5e commit aa064fb

File tree

9 files changed

+46
-4
lines changed

9 files changed

+46
-4
lines changed

app/controllers/comfy/admin/web_settings_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def subdomain_params
4646
:email_name,
4747
:email_signature,
4848
:enable_2fa,
49-
:email_notification_strategy
49+
:email_notification_strategy,
50+
:track_email_opens,
5051
)
5152
end
5253
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Mailbox::TrackingController < ApplicationController
2+
def open
3+
emails = params[:emails]
4+
message_id = params[:message_id]
5+
email_uuid = params[:email_uuid]
6+
message = Message.find_by(id: message_id, email_message_id: email_uuid)
7+
ip = request.ip
8+
user_agent = request.user_agent
9+
10+
if message
11+
message.update(opened: true)
12+
# mark as opened
13+
end
14+
15+
render json: { status: 200, code: 'OK' }
16+
end
17+
end

app/mailers/e_mailer.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ def ship
33
@message = params[:message]
44
@message_thread = params[:message_thread]
55
@subdomain = Subdomain.current
6+
@recipients = @message_thread.recipients
67
@from = if @subdomain.email_name.present?
78
"#{@subdomain.email_name} <#{@subdomain.name}@#{ENV["APP_HOST"]}>"
89
else
@@ -30,7 +31,7 @@ def ship
3031

3132
mail_settings = {
3233
# This will make the mail addresses visible to all (no Blank Carbon Copy)
33-
to: @message_thread.recipients,
34+
to: @recipients,
3435
subject: @message_thread.subject,
3536
from: @from,
3637
message_id: email_message_id(@message)

app/views/comfy/admin/web_settings/_form.haml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,15 @@
8282
= f.check_box :forum_enabled
8383
%label
8484
Forum
85+
.form-group
86+
= f.check_box :track_email_opens
87+
%label
88+
Email Tracking
8589
.form-group#tracking-checkbox
8690
= f.check_box :tracking_enabled
8791
%label
88-
Tracking
92+
Web Visit Tracking
93+
8994

9095
.form-group.consent-text
9196
= f.label :cookies_consent_ui

app/views/e_mailer/ship.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
44
</head>
55
<body>
6+
<% if @subdomain.track_email_opens %>
7+
<% tracking_link = "#{root_url(subdomain: @subdomain.subdomain_name)}email_tracking/open/?emails=#{@recipients.join(',')}&message_id=#{@message.id}&email_uuid=#{@message.email_message_id}" %>
8+
<img src='<%= tracking_link %>' width="1" height="1">
9+
<% end %>
610
<%= @message.content %>
711
<% if @subdomain.email_signature.present? %>
812
<div style="margin-top: 20px">

app/views/mailbox/message_threads/show.html.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
.card-body
1313
.card-subtitle.mb-2.text-muted
1414
= message.from
15+
- if !message.from && Subdomain.current.track_email_opens
16+
.card-subtitle.mb-2.text-muted
17+
= message.opened ? 'opened' : 'not opened yet'
1518
.card-text.bg-light.px-2.py-3
1619
= message.content
1720
- if message.attachments.any?

config/routes.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ def self.matches?(request)
5151
end
5252
end
5353

54+
# email tracking
55+
get 'email_tracking/open', to: 'mailbox/tracking#open'
56+
5457
# calendar / meetings functionality
5558
resources :calendars, controller: 'comfy/admin/calendars'
5659
resources :meetings
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddTrackingForEmails < ActiveRecord::Migration[6.1]
2+
def change
3+
add_column :messages, :opened, :boolean, default: false
4+
add_column :subdomains, :track_email_opens, :boolean, default: false
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)