statikbe/craft-campaign-monitor

Add subscribers to a Campaign Monitor list

5.0.1 2024-06-24 09:42 UTC

This package is auto-updated.

Last update: 2024-10-27 14:23:31 UTC


README

Campaign Monitor integration for Craft CMS. Subscribe to a mailing list through a form.

Requirements

This plugin requires Craft CMS 4.0.0 or later.

Installation

To install the plugin, follow these instructions.

  1. Open your terminal and go to your Craft project:
   cd /path/to/project
  1. Then tell Composer to load the plugin:
    composer require statikbe/craft-campaign-monitor
  1. In the Control Panel, go to Settings → Plugins and click the “Install” button for Campaign Monitor.

Usage

To use:

After installing and enabling the plugin, you'll need to enter your API Key and Client ID from your Campaign Monitor account under Settings > Campaign Monitor.

By default, a contact in Campaign Monitor has the following fields:

  • Email (required)
  • Name

Basic Subscribe Form

You can implement a subscribe form in your templates using the following code. Note that Resubscribe will be set to true.

<form method="post">
    {{ csrfInput() }}
    {{ actionInput('campaign-monitor/subscribe') }}
    {{ redirectInput('foo/bar') }}
    {{ hiddenInput('listId', 'ListID'|hash) }}

    {{ craft.app.session.getFlash('error') }}
      
    <label for="email">E-mail</label>
    <input type="email" name="email" required />
      
    {# Use firstname + lastname fields, or fullname (optional) #}
    <label for="firstname">First Name</label>
    <input type="text" name="firstname" />
    <label for="lastname">Last Name</label>
    <input type="text" name="lastname" />
    {# <label for="fullname">Full Name</label>
      <input type="text" name="fullname" /> #}
      
    <button type="submit">Subscribe</button>
</form>

Subscribe form with custom fields

Campaign Monitor custom fields can be added in the fields namespace. For example fields[city], where "city" is the handle of the custom field in Campaign Monitor.

 <form method="post">
    {{ csrfInput() }}
    {{ actionInput('campaign-monitor/subscribe') }}
    {{ redirectInput('foo/bar') }}
    {{ hiddenInput('listId', 'ListID'|hash) }}

    {{ craft.app.session.getFlash('error') }}

    <label for="email">E-mail</label>
    <input type="email" name="email" required />

    <label for="CustomTextField">Custom Text Field</label>
    <input type="text" name="fields[CustomTextField]" id="CustomTextField" />

    <label for="CustomNumberField">Custom Number Field</label>
    <input type="number" name="fields[CustomNumberField]" id="CustomNumberField" />

    <label for="CustomDateField">Custom Date Field</label>
    <input type="date" name="fields[CustomDateField]" id="CustomDateField" />

    <button type="submit">Subscribe</button>
</form>

Credits

Heavily inspired by clearbold/craft-campaignmonitor-service and clearbold/craft-campaignmonitor-lists.