Skip to content

Commit

Permalink
Add ahoy_email example
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuap committed Sep 16, 2024
1 parent 6de7857 commit 8f2a603
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -601,14 +601,14 @@ See the
[Rails documentation](https://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#method-i-rescue_from)
for additional details.

### Extending Campaign Mailers with Macros
### Extending campaign mailers

The campaign generator does not create a Mailer class for campaigns. In order to
enhance a campaign with a macro from another gem (such as for adding analytics),
you can do so by extending the `Heya::ApplicationMailer` class.

- Create a new file at `app/mailers/heya/application_mailer.rb`
- Add the following to it:
1. Create a new file at `app/mailers/heya/application_mailer.rb`
2. Add the following to it:

```ruby
module Heya
Expand All @@ -618,6 +618,46 @@ module Heya
end
```

For example, here's how to extended `Heya::ApplicationMailer` to include [Ahoy
Email's](https://github.com/ankane/ahoy_email)
[has_history](https://github.com/ankane/ahoy_email?tab=readme-ov-file#message-history)
and
[track_clicks](https://github.com/ankane/ahoy_email?tab=readme-ov-file#usage)
macros:

```ruby
# app/mailers/heya/application_mailer.rb
module Heya
class ApplicationMailer < ActionMailer::Base
has_history
track_clicks campaign: -> {
params[:step].campaign.name
}
end
end
```

This does two things:

1. `has_history` enables history tracking for all Heya emails
2. The `track_clicks` block appends the name of the campaign to all
(non-unsubscribe) links in an email so that each campaign can keep its data
separate from the other campaigns.

The result of this is that you can run a command like this in the console:

```irb
AhoyEmail.stats "OnboardingCampaign"
```

...and receive a result:

```irb
=> {:sends=>1, :clicks=>2, :unique_clicks=>1, :ctr=>100.0}
```

### Campaigns FAQ

**What happens when:**
Expand Down

0 comments on commit 8f2a603

Please sign in to comment.