Skip to content

Commit 381d257

Browse files
warneroWarner Onstine
andauthored
feat: SendGrid subscription_tracking setting (#655)
* Adding in missing `subscription_tracking` setting (#1) * Adding in missing `subscription_tracking` setting This adds the capability to send a value for `subscription_tracking` that is included in the `tracking_settings` section of the SendGrid mail body * code cleanup * Feature/add bypass unsubscribe management enabled (#2) * adding new field bypass_unsubscribe_management --------- Co-authored-by: Warner Onstine <warner.onstine@revelry.co>
1 parent 1869a18 commit 381d257

File tree

4 files changed

+148
-0
lines changed

4 files changed

+148
-0
lines changed

lib/bamboo/adapters/send_grid_adapter.ex

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ defmodule Bamboo.SendGridAdapter do
131131
|> put_settings(config)
132132
|> put_asm_group_id(email)
133133
|> put_bypass_list_management(email)
134+
|> put_bypass_unsubscribe_management(email)
134135
|> put_google_analytics(email)
135136
|> put_click_tracking(email)
137+
|> put_subscription_tracking(email)
136138
|> put_ip_pool_name(email)
137139
|> put_custom_args(email)
138140
end
@@ -339,6 +341,18 @@ defmodule Bamboo.SendGridAdapter do
339341

340342
defp put_bypass_list_management(body, _), do: body
341343

344+
defp put_bypass_unsubscribe_management(body, %Email{private: %{bypass_unsubscribe_management: enabled}})
345+
when is_boolean(enabled) do
346+
mail_settings =
347+
body
348+
|> Map.get(:mail_settings, %{})
349+
|> Map.put(:bypass_unsubscribe_management, %{enable: enabled})
350+
351+
Map.put(body, :mail_settings, mail_settings)
352+
end
353+
354+
defp put_bypass_unsubscribe_management(body, _), do: body
355+
342356
defp put_google_analytics(body, %Email{
343357
private: %{google_analytics_enabled: enabled, google_analytics_utm_params: utm_params}
344358
}) do
@@ -365,6 +379,17 @@ defmodule Bamboo.SendGridAdapter do
365379

366380
defp put_click_tracking(body, _), do: body
367381

382+
defp put_subscription_tracking(body, %Email{private: %{subscription_tracking_enabled: enabled}}) do
383+
tracking_settings =
384+
body
385+
|> Map.get(:tracking_settings, %{})
386+
|> Map.put(:subscription_tracking, %{enable: enabled, enable_text: enabled})
387+
388+
Map.put(body, :tracking_settings, tracking_settings)
389+
end
390+
391+
defp put_subscription_tracking(body, _), do: body
392+
368393
defp put_attachments(body, %Email{attachments: []}), do: body
369394

370395
defp put_attachments(body, %Email{attachments: attachments}) do

lib/bamboo/adapters/send_grid_helper.ex

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ defmodule Bamboo.SendGridHelper do
1616
@categories :categories
1717
@asm_group_id :asm_group_id
1818
@bypass_list_management :bypass_list_management
19+
@bypass_unsubscribe_management :bypass_unsubscribe_management
1920
@google_analytics_enabled :google_analytics_enabled
2021
@google_analytics_utm_params :google_analytics_utm_params
2122
@additional_personalizations :additional_personalizations
@@ -24,6 +25,7 @@ defmodule Bamboo.SendGridHelper do
2425
@ip_pool_name_field :ip_pool_name
2526
@custom_args :custom_args
2627
@click_tracking_enabled :click_tracking_enabled
28+
@subscription_tracking_enabled :subscription_tracking_enabled
2729

2830
@doc """
2931
Specify the template for SendGrid to use for the context of the substitution
@@ -162,6 +164,28 @@ defmodule Bamboo.SendGridHelper do
162164
raise "expected bypass_list_management parameter to be a boolean, got #{enabled}"
163165
end
164166

167+
@doc """
168+
Instruct SendGrid to bypass unsubscribe list management for this email.
169+
170+
If enabled, SendGrid will ignore any email suppression (such as
171+
unsubscriptions, bounces, spam filters) for this email. This is useful for
172+
emails that all users must receive, such as Terms of Service updates, or
173+
password resets.
174+
175+
## Example
176+
177+
email
178+
|> with_bypass_unsubscribe_management(true)
179+
"""
180+
def with_bypass_unsubscribe_management(email, enabled) when is_boolean(enabled) do
181+
email
182+
|> Email.put_private(@bypass_unsubscribe_management, enabled)
183+
end
184+
185+
def with_bypass_unsubscribe_management(_email, enabled) do
186+
raise "expected bypass_unsubscribe_management parameter to be a boolean, got #{enabled}"
187+
end
188+
165189
@doc """
166190
Instruct SendGrid to enable or disable Google Analytics tracking, and
167191
optionally set the UTM parameters for it.
@@ -213,6 +237,27 @@ defmodule Bamboo.SendGridHelper do
213237
raise "expected with_click_tracking enabled parameter to be a boolean"
214238
end
215239

240+
@doc """
241+
Instruct SendGrid to enable or disable Subscription Tracking for a particular email.
242+
243+
Read more about SendGrid click tracking [here](https://docs.sendgrid.com/ui/account-and-settings/tracking#subscription-tracking)
244+
245+
## Example
246+
247+
email
248+
|> with_subscription_tracking(true)
249+
250+
email
251+
|> with_subscription_tracking(false)
252+
"""
253+
def with_subscription_tracking(email, enabled)
254+
when is_boolean(enabled),
255+
do: Email.put_private(email, @subscription_tracking_enabled, enabled)
256+
257+
def with_subscription_tracking(_email, _enabled) do
258+
raise "expected with_subscription_tracking enabled parameter to be a boolean"
259+
end
260+
216261
@doc """
217262
Schedule a time for SendGrid to deliver the email.
218263

test/lib/bamboo/adapters/send_grid_adapter_test.exs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ defmodule Bamboo.SendGridAdapterTest do
274274
assert params["mail_settings"]["bypass_list_management"]["enable"] == true
275275
end
276276

277+
278+
test "deliver/2 correctly handles a bypass_unsubscribe_management" do
279+
email =
280+
new_email(
281+
from: {"From", "from@foo.com"},
282+
subject: "My Subject"
283+
)
284+
285+
email
286+
|> Bamboo.SendGridHelper.with_bypass_unsubscribe_management(true)
287+
|> SendGridAdapter.deliver(@config)
288+
289+
assert_receive {:fake_sendgrid, %{params: params}}
290+
assert params["mail_settings"]["bypass_unsubscribe_management"]["enable"] == true
291+
end
292+
277293
test "deliver/2 correctly handles with_google_analytics that's enabled with no utm_params" do
278294
email =
279295
new_email(
@@ -364,6 +380,38 @@ defmodule Bamboo.SendGridAdapterTest do
364380
assert params["tracking_settings"]["click_tracking"]["enable_text"] == false
365381
end
366382

383+
test "deliver/2 correctly handles when with_subscription_tracking is enabled" do
384+
email =
385+
new_email(
386+
from: {"From", "from@foo.com"},
387+
subject: "My Subject"
388+
)
389+
390+
email
391+
|> Bamboo.SendGridHelper.with_subscription_tracking(true)
392+
|> SendGridAdapter.deliver(@config)
393+
394+
assert_receive {:fake_sendgrid, %{params: params}}
395+
assert params["tracking_settings"]["subscription_tracking"]["enable"] == true
396+
assert params["tracking_settings"]["subscription_tracking"]["enable_text"] == true
397+
end
398+
399+
test "deliver/2 correctly handles when with_subscription_tracking is disabled" do
400+
email =
401+
new_email(
402+
from: {"From", "from@foo.com"},
403+
subject: "My Subject"
404+
)
405+
406+
email
407+
|> Bamboo.SendGridHelper.with_subscription_tracking(false)
408+
|> SendGridAdapter.deliver(@config)
409+
410+
assert_receive {:fake_sendgrid, %{params: params}}
411+
assert params["tracking_settings"]["subscription_tracking"]["enable"] == false
412+
assert params["tracking_settings"]["subscription_tracking"]["enable_text"] == false
413+
end
414+
367415
test "deliver/2 correctly handles a sendgrid_send_at timestamp" do
368416
email =
369417
new_email(

test/lib/bamboo/adapters/send_grid_helper_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ defmodule Bamboo.SendGridHelperTest do
149149
end
150150
end
151151

152+
153+
test "with_bypass_unsubscribe_management/2 adds the correct property", %{email: email} do
154+
email = email |> with_bypass_unsubscribe_management(true)
155+
assert email.private[:bypass_unsubscribe_management] == true
156+
end
157+
158+
test "with_bypass_unsubscribe_management/2 raises on non-boolean parameter", %{email: email} do
159+
assert_raise RuntimeError, fn ->
160+
email |> with_bypass_unsubscribe_management(1)
161+
end
162+
end
163+
152164
test "with_google_analytics/3 with utm_params", %{email: email} do
153165
utm_params = %{
154166
utm_source: "source",
@@ -199,6 +211,24 @@ defmodule Bamboo.SendGridHelperTest do
199211
end
200212
end
201213

214+
test "with_subscription_tracking/2 with enabled set to true", %{email: email} do
215+
email = with_subscription_tracking(email, true)
216+
217+
assert email.private[:subscription_tracking_enabled] == true
218+
end
219+
220+
test "with_subscription_tracking/2 with enabled set false", %{email: email} do
221+
email = with_subscription_tracking(email, false)
222+
223+
assert email.private[:subscription_tracking_enabled] == false
224+
end
225+
226+
test "with_subscription_tracking/2 raises on non-boolean enabled parameter", %{email: email} do
227+
assert_raise RuntimeError, fn ->
228+
with_subscription_tracking(email, 1)
229+
end
230+
end
231+
202232
describe "with_send_at/2" do
203233
test "adds the correct property for a DateTime input", %{email: email} do
204234
{:ok, datetime, _} = DateTime.from_iso8601("2020-01-31T15:46:00Z")

0 commit comments

Comments
 (0)