Skip to content

Commit

Permalink
[feature] email tracking opt-in
Browse files Browse the repository at this point in the history
* 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">
  • Loading branch information
donrestarone authored Oct 6, 2024
1 parent 5fd4f5e commit aa064fb
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/controllers/comfy/admin/web_settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def subdomain_params
:email_name,
:email_signature,
:enable_2fa,
:email_notification_strategy
:email_notification_strategy,
:track_email_opens,
)
end
end
17 changes: 17 additions & 0 deletions app/controllers/mailbox/tracking_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Mailbox::TrackingController < ApplicationController
def open
emails = params[:emails]
message_id = params[:message_id]
email_uuid = params[:email_uuid]
message = Message.find_by(id: message_id, email_message_id: email_uuid)
ip = request.ip
user_agent = request.user_agent

if message
message.update(opened: true)
# mark as opened
end

render json: { status: 200, code: 'OK' }
end
end
3 changes: 2 additions & 1 deletion app/mailers/e_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def ship
@message = params[:message]
@message_thread = params[:message_thread]
@subdomain = Subdomain.current
@recipients = @message_thread.recipients
@from = if @subdomain.email_name.present?
"#{@subdomain.email_name} <#{@subdomain.name}@#{ENV["APP_HOST"]}>"
else
Expand Down Expand Up @@ -30,7 +31,7 @@ def ship

mail_settings = {
# This will make the mail addresses visible to all (no Blank Carbon Copy)
to: @message_thread.recipients,
to: @recipients,
subject: @message_thread.subject,
from: @from,
message_id: email_message_id(@message)
Expand Down
7 changes: 6 additions & 1 deletion app/views/comfy/admin/web_settings/_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,15 @@
= f.check_box :forum_enabled
%label
Forum
.form-group
= f.check_box :track_email_opens
%label
Email Tracking
.form-group#tracking-checkbox
= f.check_box :tracking_enabled
%label
Tracking
Web Visit Tracking


.form-group.consent-text
= f.label :cookies_consent_ui
Expand Down
4 changes: 4 additions & 0 deletions app/views/e_mailer/ship.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
</head>
<body>
<% if @subdomain.track_email_opens %>
<% tracking_link = "#{root_url(subdomain: @subdomain.subdomain_name)}email_tracking/open/?emails=#{@recipients.join(',')}&message_id=#{@message.id}&email_uuid=#{@message.email_message_id}" %>
<img src='<%= tracking_link %>' width="1" height="1">
<% end %>
<%= @message.content %>
<% if @subdomain.email_signature.present? %>
<div style="margin-top: 20px">
Expand Down
3 changes: 3 additions & 0 deletions app/views/mailbox/message_threads/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
.card-body
.card-subtitle.mb-2.text-muted
= message.from
- if !message.from && Subdomain.current.track_email_opens
.card-subtitle.mb-2.text-muted
= message.opened ? 'opened' : 'not opened yet'
.card-text.bg-light.px-2.py-3
= message.content
- if message.attachments.any?
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def self.matches?(request)
end
end

# email tracking
get 'email_tracking/open', to: 'mailbox/tracking#open'

# calendar / meetings functionality
resources :calendars, controller: 'comfy/admin/calendars'
resources :meetings
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20241006155850_add_tracking_for_emails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddTrackingForEmails < ActiveRecord::Migration[6.1]
def change
add_column :messages, :opened, :boolean, default: false
add_column :subdomains, :track_email_opens, :boolean, default: false
end
end
4 changes: 3 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa064fb

Please sign in to comment.