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

background

In service development, it was decided to manage e-mail newsletter distribution with Mailchimp.

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

Implemented the process to subscribe to the mailchimp side mailing list.

There are few Japanese documents that can be used as a reference for the Mailchimp API, so

I hope you find this article useful.

Application configuration

Programming language: Ruby

Framework: Rails

It is a very general Ruby + Rails environment.

Mailchimp API settings

Install Gem

Describe the following in the Gemfile

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

Get API key

Have your API key ready to use the Mailchimp API.

First, click on Mailchimp's own icon.

A menu will appear, so click "Account".

スクリーンショット 2020-07-19 0.26.21.png

Click "Extras" on the next screen.

Press "API keys".

スクリーンショット 2020-07-19 0.27.14.png

Press "Create A Key" on the screen that appears after that.

スクリーンショット 2020-07-19 0.35.31.png

Save the "API key" that came out.

Obtaining an Audiens ID

Mailchimp has the concept of a list to register user's mail information.

This is called "Audience".

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

To do this, we get the ID of the list, the "Audiens ID".

From here as well, it is a tick and Mailchimp console operation.

First, press "Audience" in the header. Then press "Audience dashboard".

スクリーンショット 2020-07-19 0.45.18.png

Then click "Manage Audience"-> "Settings".

スクリーンショット 2020-07-19 0.45.44.png

Click "Settings" → "Audience name and defaults".

スクリーンショット 2020-07-19 0.45.59.png

It was finally done. Audience ID. Make a note of this.

スクリーンショット 2020-07-19 0.44.58.png

Setting environment variables

Enter the obtained API key and Audience ID into .env.

.env


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

Implementation

Create a model for Mailchimp.

First, create a class.

I have referenced a part of this article (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
  #Refer to the account merge tag for the hash value of the third argument(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 surname.

is.

lists. Subscribe in ʻadd_member` is an API method,

It will register your 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 with Mailchimp. You can register your name and birthday.

For content, see the Mailchimp console

「Audience」→「Settings」→「Audience fields and * |MERGE| * tags」

Please refer to the.

スクリーンショット 2020-07-19 10.14.29.png

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

As with your email address, enter a Hash value keyed by Mergetag.

Register your email address in Mailchimp

Incorporate into the user registration process like this.

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

Surprisingly easy ^ ^

Have a good Mailchimp life ~

Recommended Posts

[Ruby + Rails] When you want to register in Mailchimp's mail list together with user registration
When you want to bind InputStream in JDBI3
[Ruby] When you want to replace multiple characters
[Rails] [Parent-child relationship] I want to register the foreign key in the child with nil when the parent is deleted.
When you want to dynamically replace Annotation in Java8
VScode extension to learn efficiently when you are just starting programming with ruby, rails
Code to use when you want to process Json with only standard library in Java
What to do when you launch an application with rails
How to write in Model class when you want to save binary data in DB with PlayFramework
When you want to explicitly write OR or AND with ransack
When you want to implement Java library testing in Spock with multi-module in Gradle in Android Studio 3
docker-compose.yml when you want to keep mysql running with docker
lombok.config when you want to pass @Qualifier to @RequiredArgsConstructor with lombok
Things to keep in mind when using Sidekiq with Rails
How to write when you want to keep line breaks and output while avoiding XSS in Rails
ProxyFactory is convenient when you want to test AOP in Spring!
I want to add a browsing function with ruby on rails
6 points to doubt when user registration is not possible with devise
[Rails] I want to add data to Params when transitioning with link_to
When you want to add a string type column with a limited length with the `rails generate migration` command
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
When you want Rails to disable a session for a specific controller only
Summary of means when you want to communicate with HTTP on Android
What to do when Blocked Host: "host name" appears in Ruby on Rails
[Rails] How to get the user information currently logged in with devise
If you want to make a zip file with Ruby, it's rubyzip.
Basic Rails commands you want to learn
[Rails] How to register multiple records in the intermediate table with many-to-many association
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
Code used when you want to process Json with only the standard library in Java (improved version) gson unnecessary
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
I want to use arrow notation in Ruby
A note when you want Tuple in Java
When you want to use the method outside
[Swift] Use nonzeroBitCount when you want popcnt in Swift
One way to redirect_to with parameters in rails
I want to play with Firestore from Rails
[Ruby on Rails] How to install Bootstrap in Rails
[Rails] I want to load CSS with webpacker
I want to get the value in Ruby
How to delete the tweet associated with the user when you delete it at the same time
[rails] After option useful when you want to change the order of DB columns
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
I want to change the path after new registration after logging in with multiple devises.
Docker command to create Rails project with a single blow in environment without Ruby