当前位置:首页 > 未分类 > 正文内容

How to Import MailChimp Subscribers to Google Sheets

ceacer3周前 (05-02)未分类7051

The Gmail Mail Merge addon can now import the email addresses of subscribers from your MailChimp mailing lists into Google Sheets. If you wish to send emails to your subscribers directly from Gmail, instead of using MailChimp mail servers, this is the way to go.

As a developer, you can use Google Apps Script to import subscriber lists, HTML campaigns, performance reports and any other data from MailChimp to Google Sheets for analysis. You can use the MailChimp OAuth2 library but in this example, we’ll use the developer key directly to connect to MailChimp.

Get the MailChimp Developer Key

In your Mailchimp account, navigate to the Account page. In the drop-down menu, select Extras, and then API keys. Click Create A Key and make a note of it.

Google Apps Script - Get MailChimp Audiences

const MAILCHIMP_API_KEY = '<>';

// MailChimp API key includes the data center id
// that your MailChimp account is associated with
const makeHttpRequest = (endpoint, params = {}) => {
  const [, mailchimpDataCenter] = MAILCHIMP_API_KEY.split('-');
  const url = `https://${mailchimpDataCenter}.api.mailchimp.com/3.0/${endpoint}`;
  const qs = Object.keys(params)
    .map((key) => `${key}=${params[key]}`)
    .join('&');
  const apiUrl = qs ? `${url}?${qs}` : url;
  const request = UrlFetchApp.fetch(apiUrl, {
    method: 'GET',
    headers: {
      Authorization: `Basic ${Utilities.base64Encode(`labnol:${MAILCHIMP_API_KEY}`)}`,
    },
  });
  return JSON.parse(request);
};

const getListMembers = (id, offset) => {
  const { members } = makeHttpRequest(`lists/${id}/members`, {
    count: 100,
    offset,
    fields: 'members.email_address',
    status: 'subscribed',
    sort_field: 'last_changed',
    sort_dir: 'DESC',
  });
  return members.map(({ email_address: email }) => [email]);
};

// Get a list of all subscribers of a specific
// MailChimp mailing list, you can retrieve the email address,
// name and subscription statues of subscribers
const getMailChimpListMembers = (id) => {
  let hasMore = true;
  let data = [];
  do {
    const emails = getListMembers(id, data.length);
    data = [...data, ...emails];
    hasMore = emails.length > 0;
  } while (hasMore);
  return data;
};

// Get a list of all audiences / lists from MailChimp
const getMailChimpLists = () => {
  const params = {
    count: 10,
    fields: 'lists.id,lists.name',
    sort_field: 'date_created',
    sort_dir: 'DESC',
  };
  const { lists = [] } = makeHttpRequest('lists', params);
  return lists.map(({ id, name }) => ({
    id,
    name,
    members: getMailChimpListMembers(id),
  }));
};

The GetMailChimpLists method will bring all the lists and associated email addresses in a JSON object that you can easily write to Google Sheets using the SpreadsheetApp service.

相关文章

Send Personalized Emails with SMTP and Mail Merge in Google Sheets

Send Personalized Emails with SMTP and Mail Merge in Google Sheets

The Mail merge add-on lets you send personalized emails to multiple recipients in one go. The emails...

How to Email Google Sheets Automatically on a Recurring Schedule

How to Email Google Sheets Automatically on a Recurring Schedule

The Email Spreadsheets add-on for Google Sheets can save office workers a ton of time by automating...

Google Maps Formulas for Google Sheets

Google Maps Formulas for Google Sheets

You can bring the power of Google Maps to your Google Sheets using simple formulas with no coding. Y...

Formulas in Google Sheets Disappear When New Rows Are Added

Formulas in Google Sheets Disappear When New Rows Are Added

An order form, created in Google Forms, requires customers to provide their full name, the item quan...

How Teachers can Email Parents of Students from Google Forms

How Teachers can Email Parents of Students from Google Forms

A school provides email accounts for students that are enrolled in high school. The school has publ...

Send WhatsApp Messages with a Google Sheets Function

Send WhatsApp Messages with a Google Sheets Function

In a previous tutorial, you learned how to send WhatsApp messages from Google Sheets using the offic...