lewagon/ahoy_email

Name: ahoy_email

Owner: Le Wagon

Description: Simple, powerful email tracking for Rails

Forked from: ankane/ahoy_email

Created: 2017-10-02 13:32:02.0

Updated: 2017-10-02 13:32:03.0

Pushed: 2017-08-03 13:12:36.0

Homepage: null

Size: 120

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Ahoy Email

:postbox: Simple, powerful email tracking for Rails

You get:

Works with any email service.

:bullettrain_side: To manage unsubscribes, check out Mailkick

:fire: To track visits and events, check out Ahoy

Build Status

Installation

Add this line to your application?s Gemfile:

'ahoy_email'

And run the generator. This creates a model to store messages.

s generate ahoy_email:install
 db:migrate
How It Works

Ahoy creates an Ahoy::Message every time an email is sent by default.

Users

Ahoy tracks the user a message is sent to - not just the email address. This gives you a full history of messages for each user, even if he or she changes addresses.

By default, Ahoy tries User.where(email: message.to.first).first to find the user.

You can pass a specific user with:

s UserMailer < ActionMailer::Base
f welcome_email(user)
# ...
track user: user
mail to: user.email
d

The user association is polymorphic, so use it with any model.

To get all messages sent to a user, add an association:

s User < ActiveRecord::Base
s_many :messages, class_name: "Ahoy::Message"

And run:

.messages
Opens

An invisible pixel is added right before the </body> tag in HTML emails.

If the recipient has images enabled in his or her email client, the pixel is loaded and the open time recorded.

Use track open: false to skip this.

Clicks

A redirect is added to links to track clicks in HTML emails.

://chartkick.com

becomes

://you.io/ahoy/messages/rAnDoMtOkEn/click?url=http%3A%2F%2Fchartkick.com&signature=...

A signature is added to prevent open redirects.

Use track click: false to skip tracking, or skip specific links with:

ata-skip-click="true" href="...">Can't touch this</a>
UTM Parameters

UTM parameters are added to links if they don?t already exist.

The defaults are:

Use track utm_params: false to skip tagging, or skip specific links with:

ata-skip-utm-params="true" href="...">Break it down</a>
Extra Attributes

Create a migration to add extra attributes to the ahoy_messages table, for example:

s AddCampaignIdToAhoyMessages < ActiveRecord::Migration
f change
add_column :ahoy_messages, :campaign_id, :integer
d

Then use:

k extra: {campaign_id: 1}
Customize
Tracking

Skip tracking of attributes by removing them from your model. You can safely remove:

Configuration

There are 3 places to set options. Here?s the order of precedence.

Action
s UserMailer < ActionMailer::Base
f welcome_email(user)
# ...
track user: user
mail to: user.email
d

Mailer
s UserMailer < ActionMailer::Base
ack utm_campaign: "boom"

Global
Email.track open: false
Events

Subscribe to open and click events. Create an initializer config/initializers/ahoy_email.rb with:

s EmailSubscriber
f open(event)
# any code you want
d

f click(event)
# any code you want
d


Email.subscribers << EmailSubscriber.new

Here?s an example if you use Ahoy to track visits and events:

s EmailSubscriber
f open(event)
event[:controller].ahoy.track "Email opened", message_id: event[:message].id
d

f click(event)
event[:controller].ahoy.track "Email clicked", message_id: event[:message].id, url: event[:url]
d


Email.subscribers << EmailSubscriber.new
Reference

You can use a Proc for any option.

k utm_campaign: proc { |message, mailer| mailer.action_name + Time.now.year }

Disable tracking for an email

k message: false

Or specific actions

k only: [:welcome_email]
k except: [:welcome_email]

Or by default

Email.track message: false

Customize domain

k url_options: {host: "mydomain.com"}

Use a different model

Email.message_model = UserMessage
Upgrading
0.2.3

Optionally, you can store UTM parameters by adding utm_source, utm_medium, and utm_campaign columns to your message model.

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.