[Ruby] [Ruby+Rails] When you want to register with Mailchimp mail list together with user registration

2 minute read

Background

During service development, mail magazine distribution will be managed by Mailchimp.

At that time, by tapping the API provided by Mailchimp at the time of user registration,

Implemented processing to register to the mailing list on Mailchimp side.

Since there are few Japanese documents that can be used as reference for the Mailchimp API,

I hope this article has helped you.

Application configuration

Programming language: Ruby

Framework: Rails

It is a very general Ruby+Rails environment.

Mailchimp API settings

Install Gem

The following is described in Gemfile

gem "mailchimp-api", require: "mailchimp"

Get API key

Prepare API key to use Mailchimp API.

First, click on your own Mailchimp icon.

A menu will appear, so click “Account”.

 Screenshot 2020-07-19 0.26.21.png

Click “Extras” on the next screen.

Press “API keys”.

Screenshot 2020-07-19 0.27.14.png

Press “Create A Key” on the screen that appears after that.

Screenshot 2020-07-19 0.35.31.png

Save the “API key” that came out.

Get Audiens ID

Mailchimp has the concept of a list for registering user mail information.

This is called “Audience”.

Therefore, which list should you register? Must be specified.

For that, we get the ID of the list, the “Audiens ID”.

From here, it is the tick and the console operation of Mailchimp.

First, press “Audience” in the header. Then press the “Audience dashboard”.

Screenshot 2020-07-19 0.45.18.png

Then click “Manage Audience” → “Settings”.

Screenshots 2020-07-19 0.45.44.png

Click “Settings” → “Audience name and defaults”.

Screenshot 2020-07-19 0.45.59.png

I was finally happy. Audience ID. Make a note of this.

Screenshot 2020-07-19 0.44.58.png

Setting environment variables

The obtained API key and Audience ID are put in .env.

.env


# Mailchimp
MAILCHIMP_API_KEY= # API key
MAILCHIMP_LIST_ID= # Audience ID

Implementation

Create a model for Mailchimp.

First, create a class.

This article is partially referenced (https://qiita.com/kon_yu/items/372bba35e60744633c94).

qiita.rb


class MailMagazine
  # Mailchimp API initialization
  def initialize
    @mailchimp = Mailchimp::API.new(ENV['MAILCHIMP_API_KEY'])
  end

  # Add to list
  # For the hash value of the third argument, refer to the merge tag of the account (lists/settings/merge-tags?id=XXX)
  def add_member(email, first_name, last_name)
    @mailchimp.lists.subscribe(
      ENV['MAILCHIMP_LIST_ID'],
      {email: email},
      {FNAME: first_name, LNAME: last_name},
      "html",
      false
    )
  end
end

Create a method called add_member and register it.

The first argument is the email address.

The second argument is the name.

The third argument is the family name.

is.

lists. subscribe in add_member is an API method,

Registers an email address in Mailchimp.

The first argument is Audience ID.

The second argument is the Hash value of the email address you want to register.

The third argument is auxiliary information that can be set in Mailchimp. You can register your name and birthday.

For details, see the Mailchimp console

“Audience” → “Settings” → “Audience fields and * MERGE * tags”

Please refer to the.

Screenshot 2020-07-19 10.14.29.png

You can check and register information other than email address like this.

Like the email address, enter the hash value with Mergetag as the key.

Register email address in Mailchimp

Incorporate like this in the user registration process.

mailchimp = MailMagazine.new
mailchimp.add_member(@user.email, @user.first_name, @user.last_name)

Surprisingly easy ^ ^

Good Mailchimp Life ~