Caffeinate

Ruby on Rails drip campaign engine.

Are there docs?

Since you asked.

Usage

Given a mailer like this:

class AbandonedCartMailer < ActionMailer::Base 
  def you_forgot_something(cart)
    mail(to: cart.user.email, subject: "You forgot something!")
  end

  def selling_out_soon(cart)
    mail(to: cart.user.email, subject: "Selling out soon!")
  end 
end 

Create a Campaign

Caffeinate::Campaign.create!(name: "Abandoned Cart", slug: "abandoned_cart") 

Create a Caffeinate::Dripper

class AbandonedCartDripper < Caffeinate::Dripper::Base
  # This should match a Caffeinate::Campaign#slug
  campaign :abandoned_cart 

  # A block to subscribe your users automatically 
  # You can invoke this by calling `AbandonedCartDripper.subscribe!`,
  # probably in a background process, run at a given interval 
  subscribes do 
    Cart.left_joins(:cart_items)
        .includes(:user)
        .where(completed_at: nil)
        .where(updated_at: 1.day.ago..2.days.ago)
        .having('count(cart_items.id) = 0').each do |cart|
      subscribe(cart, user: cart.user)
    end 
  end 

  # Register your drips! Syntax is
  # drip <mailer_action_name>, mailer: <MailerClass>, delay: <ActiveSupport::Interval>
  drip :you_forgot_something, mailer: "AbandonedCartMailer", delay: 1.hour 
  drip :selling_out_soon, mailer: "AbandonedCartMailer", delay: 8.hours do 
    cart = mailing.subscriber
    if cart.completed?
      end! # you can also invoke `unsubscribe!` to cancel this mailing and all future mailings
      return false
    end 
  end 
end 

Automatically subscribe eligible carts to it by running:

AbandonedCartDripper.subscribe!

This would typically run in a background job, queued up at a given interval.

And then, once it's done, start your engines!

AbandonedCartDripper.perform!

This, too, would typically run in a background job, queued up at a given interval.

Installation

Add this line to your application's Gemfile:

gem 'caffeinate'

And then do the bundle:

$ bundle

Add do some housekeeping:

$ rails g caffeinate:install 

Followed by a migrate:

$ rails db:migrate

Upcoming features/todo

  • Ability to optionally use relative start time when creating a step
  • Logo
  • Conversion tracking
  • Custom field support on CampaignSubscription
  • GUI (?)
  • REST API (?)

Contributing

Just do it.

Contributors & thanks

  • Thanks to sourdoughdev for releasing the gem name to me. :)

License

The gem is available as open source under the terms of the MIT License.