From d1820abaca599e05bb2aca1a59c924cd576e2eb4 Mon Sep 17 00:00:00 2001 From: Sreenadh TC Date: Fri, 30 Jul 2021 00:10:29 +0530 Subject: [PATCH 1/2] add support for enabling click_tracking for sendgrid emails --- lib/bamboo/adapters/send_grid_adapter.ex | 13 +++++++++++++ lib/bamboo/adapters/send_grid_helper.ex | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/lib/bamboo/adapters/send_grid_adapter.ex b/lib/bamboo/adapters/send_grid_adapter.ex index 0164fb19..9d13f889 100644 --- a/lib/bamboo/adapters/send_grid_adapter.ex +++ b/lib/bamboo/adapters/send_grid_adapter.ex @@ -129,6 +129,7 @@ defmodule Bamboo.SendGridAdapter do |> put_asm_group_id(email) |> put_bypass_list_management(email) |> put_google_analytics(email) + |> put_click_tracking(email) |> put_ip_pool_name(email) |> put_unique_args(email) end @@ -355,6 +356,18 @@ defmodule Bamboo.SendGridAdapter do defp put_google_analytics(body, _), do: body + defp put_click_tracking(body, %Email{private: %{click_tracking_enabled: enabled}}) do + tracking_settings = + body + |> Map.get(:tracking_settings, %{}) + |> Map.put(:click_tracking, %{enabled: enabled, enable_text: enabled}) + + body + |> Map.put(:tracking_settings, tracking_settings) + end + + defp put_click_tracking(body, _), do: body + defp put_attachments(body, %Email{attachments: []}), do: body defp put_attachments(body, %Email{attachments: attachments}) do diff --git a/lib/bamboo/adapters/send_grid_helper.ex b/lib/bamboo/adapters/send_grid_helper.ex index f3db5084..39390528 100644 --- a/lib/bamboo/adapters/send_grid_helper.ex +++ b/lib/bamboo/adapters/send_grid_helper.ex @@ -23,6 +23,7 @@ defmodule Bamboo.SendGridHelper do @send_at_field :sendgrid_send_at @ip_pool_name_field :ip_pool_name @unique_args :unique_args + @click_tracking_enabled :click_tracking_enabled @doc """ Specify the template for SendGrid to use for the context of the substitution @@ -200,6 +201,27 @@ defmodule Bamboo.SendGridHelper do raise "expected with_google_analytics enabled parameter to be a boolean" end + @doc """ + Instruct SendGrid to enable or disable Click Tracking for a particular email. + + Read more about SendGrid click tracking [here](https://docs.sendgrid.com/ui/account-and-settings/tracking#click-tracking) + + ## Example + + email + |> with_click_tracking(true) + + email + |> with_click_tracking(false) + """ + def with_click_tracking(email, enabled) + when is_boolean(enabled), + do: Email.put_private(email, @click_tracking_enabled, enabled) + + def with_click_tracking(_email, _enabled) do + raise "expected with_click_tracking enabled parameter to be a boolean" + end + @doc """ Schedule a time for SendGrid to deliver the email. From cec4d5044a0cc6a69d5063d4affc2c5e676205ae Mon Sep 17 00:00:00 2001 From: Sreenadh TC Date: Fri, 30 Jul 2021 00:10:56 +0530 Subject: [PATCH 2/2] add sendgrid tests for click_tracking settings --- .../adapters/send_grid_adapter_test.exs | 32 +++++++++++++++++++ .../bamboo/adapters/send_grid_helper_test.exs | 18 +++++++++++ 2 files changed, 50 insertions(+) diff --git a/test/lib/bamboo/adapters/send_grid_adapter_test.exs b/test/lib/bamboo/adapters/send_grid_adapter_test.exs index 9cd5cf85..6a461b06 100644 --- a/test/lib/bamboo/adapters/send_grid_adapter_test.exs +++ b/test/lib/bamboo/adapters/send_grid_adapter_test.exs @@ -335,6 +335,38 @@ defmodule Bamboo.SendGridAdapterTest do assert params["tracking_settings"]["ganalytics"]["utm_content"] == "content" end + test "deliver/2 correctly handles when with_click_tracking is enabled" do + email = + new_email( + from: {"From", "from@foo.com"}, + subject: "My Subject" + ) + + email + |> Bamboo.SendGridHelper.with_click_tracking(true) + |> SendGridAdapter.deliver(@config) + + assert_receive {:fake_sendgrid, %{params: params}} + assert params["tracking_settings"]["click_tracking"]["enabled"] == true + assert params["tracking_settings"]["click_tracking"]["enable_text"] == true + end + + test "deliver/2 correctly handles when with_click_tracking is disabled" do + email = + new_email( + from: {"From", "from@foo.com"}, + subject: "My Subject" + ) + + email + |> Bamboo.SendGridHelper.with_click_tracking(false) + |> SendGridAdapter.deliver(@config) + + assert_receive {:fake_sendgrid, %{params: params}} + assert params["tracking_settings"]["click_tracking"]["enabled"] == false + assert params["tracking_settings"]["click_tracking"]["enable_text"] == false + end + test "deliver/2 correctly handles a sendgrid_send_at timestamp" do email = new_email( diff --git a/test/lib/bamboo/adapters/send_grid_helper_test.exs b/test/lib/bamboo/adapters/send_grid_helper_test.exs index c817bbb1..88d83f09 100644 --- a/test/lib/bamboo/adapters/send_grid_helper_test.exs +++ b/test/lib/bamboo/adapters/send_grid_helper_test.exs @@ -183,6 +183,24 @@ defmodule Bamboo.SendGridHelperTest do end end + test "with_click_tracking/2 with utm_params", %{email: email} do + email = with_click_tracking(email, true) + + assert email.private[:click_tracking_enabled] == true + end + + test "with_click_tracking/2 with enabled set false", %{email: email} do + email = with_click_tracking(email, false) + + assert email.private[:click_tracking_enabled] == false + end + + test "with_click_tracking/2 raises on non-boolean enabled parameter", %{email: email} do + assert_raise RuntimeError, fn -> + with_click_tracking(email, 1) + end + end + describe "with_send_at/2" do test "adds the correct property for a DateTime input", %{email: email} do {:ok, datetime, _} = DateTime.from_iso8601("2020-01-31T15:46:00Z")