betacie/mailchimp-bundle

MailChimp API Symfony Bundle

0.1.0 2015-09-25 15:40 UTC

This package is not auto-updated.

Last update: 2024-09-12 00:04:46 UTC


README

Build Status

mailchimp-bundle

本插件可以帮助您将项目的新闻通讯订阅者同步到MailChimp。

您可以使用Symfony命令一次同步所有订阅者:new users will be added to MailChimp, existing users will be updated and user no longer in your project will be deleted from MailChimp.

您还可以通过事件逐个同步订阅/取消订阅。

此外,它还可以帮助您同步您的列表合并标签

设置

将插件添加到您的项目

composer require betacie/mailchimp-bundle

Betacie\MailChimpBundle\BetacieMailChimpBundle 添加到您的 AppKernel.php

$bundles = [
    // ...
    new Betacie\MailChimpBundle\BetacieMailChimpBundle(),
];

配置

您需要先在MailChimp的后端添加列表。

对于每个列表,您必须在您的 config.yml 中定义一个配置

betacie_mailchimp:
    api_key: YOURMAILCHIMPAPIKEY
    lists:
        list1:
            # optional language option, used only in full synchronization
            mc_language: 'fr'

            # optional merge tags you want to synchronize
            merge_tags:
                -
                    tag: FIRSTTAG
                    name: My first tag
                    options: {"field_type":"radio", "choices": ["foo", "bar"]}
                -
                    tag: SECONDTAG
                    name: My second tag
                    
            # provider used in full synchronization
            subscriber_providers: 'yourapp.provider1'

        list2:
            subscriber_providers: 'yourapp.provider2'

其中 listX 是您的MailChimp列表的名称,而 yourapp.providerX 是您的提供者服务的密钥,该服务将提供需要同步到MailChimp的订阅者。密钥 mc_language 是可选的,并将为该列表中的所有订阅者设置此语言,请参阅 接受的代码列表

定义列表和提供者仅在使用命令进行完整同步时是必要的。

使用

同步合并标签

合并标签(或合并变量)是您可以添加到您的订阅者中的值(例如用户的姓名或出生日期)。然后您可以在您的通讯中使用这些标签或创建基于它们的细分市场。

有关合并标签的更多信息,请参阅MailChimp上的此 指南

要同步,您需要首先在MailChimp后端创建您的列表。然后您需要根据上面所示的配置在您的 config.yml 中添加它们。您可以提供的 options 与在 MailChimp API 中找到的相同。

然后,您可以使用 app/console betacie:mailchimp:synchronize-merge-tags 命令同步标签。请注意,在MailChimp中存在但未在您的配置中定义的每个标签都将与相关值一起删除。

使用命令进行完整同步

您可以通过调用Symfony命令 app/console betacie:mailchimp:synchronize-subscribers 一次性同步您项目的所有订阅者。它将首先检索已经在MailChimp中存在的所有订阅者,并取消任何不在您项目中的订阅者(它们可能在项目端被删除),然后它将发送所有您的订阅者到MailChimp,新订阅者将被添加,现有订阅者将被更新。

config.yml配置您的列表之后,您需要至少创建一个将用于Symfony命令的 Provider。您的提供者应可通过服务密钥(与上面配置中的 subscriber_providers 中引用的相同)访问。

services:
    yourapp_mailchimp_subscriber_provider:
        class: YourApp\App\Newsletter\SubscriberProvider
        arguments: [@yourapp_user_repository]

应该实现 Betacie\MailChimpBundle\Provider\ProviderInterface 并返回一个包含 Betacie\MailChimpBundle\Subscriber\Subscriber 对象的数组。Subscriber 对象的第一个参数是其电子邮件地址,第二个参数是需要添加到 MailChimp 后端列表设置中的合并标签值数组,在 列表字段和 *|MERGE|* 标签 下(参见此 指南,了解如何在 MailChimp 中添加合并标签)。

<?php

namespace YourApp\App\Newsletter;

use Betacie\MailchimpBundle\Provider\ProviderInterface;
use Betacie\MailchimpBundle\Subscriber\Subscriber;
use YourApp\Model\User\UserRepository;
use YourApp\Model\User\User;

class SubscriberProvider implements ProviderInterface
{
    // these tags should match the one you added in MailChimp's backend
    const TAG_NICKNAME =           'NICKNAME';
    const TAG_GENDER =             'GENDER';
    const TAG_BIRTHDATE =          'BIRTHDATE';
    const TAG_LAST_ACTIVITY_DATE = 'LASTACTIVI';
    const TAG_REGISTRATION_DATE =  'REGISTRATI';
    const TAG_CITY =               'CITY';

    protected $userRepository;

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }

    public function getSubscribers()
    {
        $users = $this->userRepository->findSubscribers();

        $subscribers = array_map(function(User $user) {
            $subscriber = new Subscriber($user->getEmail(), [
                self::TAG_NICKNAME => $user->getNickname(),
                self::TAG_GENDER => $user->getGender(),
                self::TAG_BIRTHDATE => $user->getBirthdate() ? $user->getBirthdate()->format('Y-m-d') : null,
                self::TAG_CITY => $user->getCity(),
                self::TAG_LAST_ACTIVITY_DATE => $user->getLastActivityDate() ? $user->getLastActivityDate()->format('Y-m-d') : null,
                self::TAG_REGISTRATION_DATE => $user->getRegistrationDate() ? $user->getRegistrationDate()->format('Y-m-d') : null,
                // you don't need to specify "mc_language" tag if you added it in your config
                // you can also use all MailChimp configuration tags here as well
            ]);

            return $subscriber;
        }, $users);

        return $subscribers;
    }
}

使用事件进行单元同步

如果您需要实时同步,可以在控制器(或任何地方)上派发自定义事件。订阅事件可以用于添加新订阅者或更新现有订阅者。

以下是一个订阅事件派发的示例

<?php

use Betacie\MailchimpBundle\Event\SubscriberEvent;
use Betacie\MailchimpBundle\Subscriber\Subscriber;

// ...

public function newUser(User $user)
{
    // ...
	
    $subscriber = new Subscriber($user->getEmail(), [
		'FIRSTNAME' => $user->getFirstname(),
		'mc_language' => 'fr',
		// Important note : mc_language defined in config.yml will not be used, be sure to set it here if needed
		// as well as any other MailChimp tag you need.
	]);

	$this->container->get('event_dispatcher')->dispatch(
        SubscriberEvent::EVENT_SUBSCRIBE,
        new SubscriberEvent('your_list_name', $subscriber)
    );
}

如果您想告诉 MailChimp 一个现有的订阅者已更改其电子邮件地址,您可以通过将 new-email 选项添加到合并标签中来实现。

<?php

use Betacie\MailchimpBundle\Event\SubscriberEvent;
use Betacie\MailchimpBundle\Subscriber\Subscriber;

// ...

public function changedEmail($previousMail, $newEmail)
{
    // ...
	
    $subscriber = new Subscriber($previousEmail, [
		 'new-email' => $newEmail
    ]);

    $this->container->get('event_dispatcher')->dispatch(
        SubscriberEvent::EVENT_SUBSCRIBE,
        new SubscriberEvent('your_list_name', $subscriber)
    );
}

退订更简单,您只需要电子邮件地址,所有合并标签都将被忽略

<?php

use Betacie\MailchimpBundle\Event\SubscriberEvent;
use Betacie\MailchimpBundle\Subscriber\Subscriber;

// ...

public function deletedUser(User $user)
{
    // ...
	
    $subscriber = new Subscriber($user->getEmail());

    $this->container->get('event_dispatcher')->dispatch(
        SubscriberEvent::EVENT_UNSUBSCRIBE,
        new SubscriberEvent('your_list_name', $subscriber)
    );
}