Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] email tracking opt-in #1658

Merged
merged 4 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading