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

Creating and sending a campaign from rails #218

Closed
jwgrenning opened this issue Sep 22, 2016 · 1 comment
Closed

Creating and sending a campaign from rails #218

jwgrenning opened this issue Sep 22, 2016 · 1 comment

Comments

@jwgrenning
Copy link

This is not really an issue, anymore. I got some email help from Amro and thought I'd post how I used gibbon to create and send a campaign from my website.

A little about my application. I do training and coaching. After a training session I add the registered course attendees via MailChimp API, then initiate my 'Follow up campaign" with info for them to continue the learning.

Here is the base class that holds my API key and list ID.

module MailList
  class GibbonApi
    def initialize
      @api = Gibbon::Request.new(api_key: ENV["MAILCHIMP_API_KEY"], debug: false)
      @api.timeout = 10
    end      
    def api
      @api
    end
    def list_id
      "MY-LIST-ID"
    end
  end
end

Here is the class for creating a campaign, sending it, and looking at some of the reply info.

module MailList

  # see 
  # http://developer.mailchimp.com/documentation/mailchimp/reference/overview/
  # http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/
  # see https://github.com/amro/gibbon

  class CampaignByCourseCode

    def initialize template_id, subject, from_name, reply_to, course_code
      @gibbon = MailList::GibbonApi.new
      @template_id = template_id
      @subject = subject
      @from_name = from_name
      @reply_to = reply_to
      @course_code = course_code
      @create_results = @send_results = "No results yet"
    end

    def create
      init_gibbon_parameters
      begin
        @create_results = @gibbon.api.campaigns.create(body: @campaign_body)
        @content_add_results = @gibbon.api.campaigns(campaign_id_from_create_results).content.upsert(body: @email_body)
        puts campaign_summary
        return true
      rescue Gibbon::MailChimpError => e
        @create_results = "Houston, we have a create_campaign problem: #{e.message} - #{e.raw_body}"
        puts @create_results
        return false
      end
    end

    def init_gibbon_parameters
      segment_options = {
          match: "any",
          conditions: [
            {
              condition_type: "SelectMerge",
              field: "COURSECODE",
              op: "contains",
              value: @course_code
            },
            {
              condition_type: "SelectMerge",
              field: "COURSECODE",
              op: "contains",
              value: "JWG-1" # always send to this CC
            }
          ]
      }

      recipients = {
        list_id: @gibbon.list_id,
        segment_opts: segment_options
      }

      settings = {
        subject_line: @subject,
        title: @subject,
        from_name: @from_name,
        reply_to: @reply_to,
      }

      @campaign_body = {
        type: "regular",
        recipients: recipients,
        settings: settings
      }

      @email_body = {
        template: {
          id: @template_id
        }
      }

    end

    def send

      begin
        @send_results = @gibbon.api.campaigns(campaign_id_from_create_results).actions.send.create
        @send_results = "Send handed over to the chimp succesfully" if @send_results == nil
        puts @send_results
        return true
      rescue Gibbon::MailChimpError => e
        @send_results = "Houston, we have a chimp send problem: #{e.message} - #{e.raw_body}"
        puts @send_results
        return false
      end
    end

    def pretty_create_results
      "Create campaign result: " + JSON.pretty_generate(@create_results).gsub('\n', "\n")
    end

    def pretty_content_add_results
      "Insert contents result: " + JSON.pretty_generate(@content_add_results).gsub('\n', "\n")
    end

    def subject
      @subject
    end

    def create_results
      @create_results
    end

    def send_results
      @send_results
    end

    def campaign_summary
      "Campaign #{campaign_id_from_create_results}: Recipient count #{recipient_count_from_create_results} for Course #{course_code_from_create_results}"
    end

    def all_recipient_info_from_create_results
      @create_results['recipients']
    end

    def list_id_from_create_results
      @create_results['recipients']['list_id']
    end

    def list_name_from_create_results
      @create_results['recipients']['list_name']
    end

    def segment_text_from_create_results
      @create_results["recipients"]["segment_text"]
    end

    def recipient_count_from_create_results
      @create_results["recipients"]["recipient_count"]
    end

    def course_code_from_create_results
      @create_results["recipients"]["segment_opts"]["conditions"][0]["value"]
    end

    def plain_text_email_body_from_create_results
      @content_add_results["plain_text"]
    end

    def html_email_body_from_create_results
      @content_add_results["html"]
    end

    def campaign_id_from_create_results
      @create_results["id"]
    end

  end

end

From rails console you can do this to create and send a campaign to all the subscribers that have my 'course_code' matching merge field 'COURSECODE'

template_id = 123445
subject = "Your subject"
from_name = "Your name"
reply_to = "me@me.com"
course_code = "CC-42"

campaign = MailList::CampaignByCourseCode.new template_id, subject, from_name, reply_to, course_code
campaign.create
campaign.send

You can ask the campaign object for some of the results info from its JSON reply.

Thanks for the cool gem. It really was helpful!

James

@amro
Copy link
Owner

amro commented Sep 29, 2016

Thanks @jwgrenning !

@amro amro closed this as completed Sep 29, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants