Many of our clients send email newsletters with Campaign Monitor. Their contacts often live in a Laravel application: users, members or customers. The two systems must agree. When a person changes their email address in the application, the list must change too. When a person unsubscribes in Campaign Monitor, the application must know.

We built this sync several times. Now it is a package: Laravel Campaign Monitor.

What the package does

Laravel Campaign Monitor connects Eloquent models to Campaign Monitor. It uses model events and queued jobs.

  • It adds, updates and unsubscribes a subscriber when the model changes.
  • It creates, updates and deletes a list from a model, if you want to manage lists in your application.
  • It keeps the options of a multi-select custom field in step with a model.
  • It receives webhooks from Campaign Monitor and sends them as Laravel events.
  • It gives Artisan commands to import subscribers and to manage webhooks.
  • It gives the official Campaign Monitor PHP client through a facade, for all other API calls.

Install the package

composer require bernskiold/laravel-campaign-monitor

Add your API key and client ID to the .env file. The integration is off by default, so you must turn it on:

CAMPAIGN_MONITOR_API_KEY=your-api-key
CAMPAIGN_MONITOR_CLIENT_ID=your-client-id
CAMPAIGN_MONITOR_ACTIVE=true

Leave CAMPAIGN_MONITOR_ACTIVE as false in local and test environments. The jobs then skip the API calls.

Sync a subscriber

Implement the CampaignMonitorSubscriber contract on the model, and add the SyncsWithCampaignMonitorSubscribers trait. Then tell the package two things: which lists to use, and which data to send.

use Bernskiold\LaravelCampaignMonitor\Concerns\SyncsWithCampaignMonitorSubscribers;
use Bernskiold\LaravelCampaignMonitor\Contracts\CampaignMonitorSubscriber;
use Bernskiold\LaravelCampaignMonitor\Data\SubscriberDetails;

class Member extends Model implements CampaignMonitorSubscriber
{
    use SyncsWithCampaignMonitorSubscribers;

    public function getCampaignMonitorListIds(): array
    {
        return ['your-list-id'];
    }

    public function getCampaignMonitorSubscriberDetails(): SubscriberDetails
    {
        return SubscriberDetails::make()
            ->email($this->email)
            ->name($this->name)
            ->customField('MemberSince', $this->created_at)
            ->consentsToTracking();
    }
}

When you create a member, a job adds the subscriber to each list. When you update the member, a job updates the subscriber. When you delete the member, a job unsubscribes the person.

If the model uses soft deletes, a force delete removes the subscriber from the list. A restore adds the subscriber again.

The jobs run on the campaign-monitor queue. Make sure a worker listens to this queue. If Campaign Monitor reports that you exceeded the rate limit, the job waits 60 seconds and tries again.

Control when to sync

Override shouldSyncWithCampaignMonitor() to sync only some models. For example, sync only members who agreed to receive the newsletter:

public function shouldSyncWithCampaignMonitor(): bool
{
    return $this->newsletter_consent;
}

Campaign Monitor does not add a person again after they unsubscribe. To add them again, return the list IDs from getCampaignMonitorListsToResubscribe(). You can also restart the automated journeys for a list with getCampaignMonitorListsToRerunWorkflows().

Sync lists and custom fields

Some applications create a list for each event, course or group. For this, use the CampaignMonitorList contract and the SyncsWithCampaignMonitorList trait. You give the list title and the custom fields with ListDetails and ListCustomFields. The package creates the list in Campaign Monitor and sends a CampaignMonitorListCreatedEvent with the new list ID. Keep this ID on your model.

A multi-select custom field often shows values from your application, such as a list of offices. Use the CampaignMonitorField contract and the SyncsWithCampaignMonitorField trait on that model. When the model changes, the package updates the options of the field.

Receive webhooks

Campaign Monitor can tell your application when a person subscribes, updates their details or unsubscribes. The package adds three routes below webhooks/campaign-monitor. Each route sends a Laravel event:

  • CampaignMonitorSubscriberSubscribedEvent
  • CampaignMonitorSubscriberUpdatedEvent
  • CampaignMonitorSubscriberDeactivatedEvent

Write a usual listener for each event. For example, mark a member as unsubscribed when you get the deactivated event.

To register a webhook in Campaign Monitor, use the command. It asks for the list ID, the URL and the events:

php artisan campaign-monitor:create-webhook

Other commands list, test, activate, deactivate and delete webhooks.

Import existing subscribers

When you add the package to an existing application, the list is empty. Use the import command to send all models to a list. The command sends the models in batches of 1,000.

php artisan campaign-monitor:import-subscribers your-list-id "App\Models\Member"

Add --resubscribe to add people again who unsubscribed before.

A limit to know

The package uses Eloquent model events. Mass updates and mass deletes do not send these events. For example, Member::query()->update([...]) does not update Campaign Monitor. After a mass change, run the import command, or dispatch the jobs yourself.

Get the package

Laravel Campaign Monitor is open source, with the MIT license. The code is on GitHub. If you find a problem or have an idea, open an issue. We are happy to hear from you.