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

Add Postmark email opens and links tracking options #8

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions lib/bamboo/postmark_adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ defmodule Bamboo.PostmarkAdapter do
|> email_params()
|> maybe_put_template_params(email)
|> maybe_put_tag_params(email)
|> maybe_put_tracking_params(email)
end

defp maybe_put_template_params(params, %{private:
Expand All @@ -103,6 +104,17 @@ defmodule Bamboo.PostmarkAdapter do
params
end

defp maybe_put_tracking_params(params, %{private:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no trailing white-space at the end of a line.

%{track_opens: track_opens, track_links: track_links}}) do
params
|> Map.put(:"TrackOpens", track_opens)
|> Map.put(:"TrackLinks", track_links)
end

defp maybe_put_tracking_params(params, _) do
params
end

defp email_params(email) do
recipients = recipients(email)
%{
Expand All @@ -113,8 +125,7 @@ defmodule Bamboo.PostmarkAdapter do
"Subject": email.subject,
"TextBody": email.text_body,
"HtmlBody": email.html_body,
"Headers": email_headers(email),
"TrackOpens": true
"Headers": email_headers(email)
}
end

Expand Down
13 changes: 13 additions & 0 deletions lib/bamboo/postmark_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,17 @@ defmodule Bamboo.PostmarkHelper do
|> Email.put_private(:template_id, template_id)
|> Email.put_private(:template_model, template_model)
end

@doc """
Enable Postmark email open tracking and link tracking.

## Example
tracking(email, opens: true, links: "HtmlAndText")
"""
def tracking(email, options) do
%{opens: opens, links: links} = Map.merge(%{opens: false, links: "None"}, options)
email
|> Email.put_private(:track_opens, opens)
|> Email.put_private(:track_links, links)
end
end
8 changes: 8 additions & 0 deletions test/lib/bamboo/postmark_adapter_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ defmodule Bamboo.PostmarkAdapterTest do
assert_receive {:fake_postmark, %{params: %{"Tag" => "some_tag"}}}
end

test "deliver/2 puts tracking params" do
email = new_email() |> PostmarkHelper.tracking(%{opens: true, links: "HtmlOnly"})
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a function call when a pipeline is only one function long


email |> PostmarkAdapter.deliver(@config)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a function call when a pipeline is only one function long


assert_receive {:fake_postmark, %{params: %{"TrackLinks" => "HtmlOnly", "TrackOpens" => true}}}
end

test "raises if the response is not a success" do
email = new_email(from: "INVALID_EMAIL")

Expand Down