Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for ConsentToTrack #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Adding subscriptions requires a List ID and an email, but there are a few other
* name
* array of custom fields (eg `{age: 22}`)
* boolean for whether to resubscribe addresses that have previously been removed (default true)
* whether the subscriber has given permission to have their email opens and clicks tracked. Values: 'Yes', 'No' or 'Unchanged' (default)

Note about the **list ID**: this is _not_ the parameter "listID" in the URL when viewing a list. Annoyingly, Campaign Monitor uses two IDs for lists. To find your List ID, view [this FAQ](https://createform.com/support/campaignmonitor-list).

Expand All @@ -65,6 +66,8 @@ The easiest way to add subscribers is by posting a form with their data. This fo
<!-- optional fields -->
<input type="hidden" name="resubscribe" value="0" /><!-- don't resubscribe if email has already opted out -->

<input type="hidden" name="consenttotrack" value="yes" /><!-- Tracking is permitted for this email address -->

<label for="name">Name</label>
<input type="text" id="name" name="name" />

Expand Down Expand Up @@ -100,7 +103,8 @@ Add additional parameters as needed:
{% set name = 'Steve' %}
{% set customFields = {age: 22, city: 'Chicago'} %}
{% set resubscribe = false %}
{% craft.oneCampaignMonitor.subscribe(listId, email, name, customFields, resubscribe) %}
{% set consenttotrack = 'Yes' %}
{% craft.oneCampaignMonitor.subscribe(listId, email, name, customFields, resubscribe, consenttotrack) %}
```

### Service (in controllers, etc)
Expand Down
3 changes: 2 additions & 1 deletion controllers/OneCampaignMonitor_SubscribersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public function actionAdd() {
$name = craft()->request->getParam('name') ?: '';
$customFields = craft()->request->getParam('customFields') ?: array();
$resubscribe = craft()->request->getParam('resubscribe') ?: true;
$consenttotrack = craft()->request->getParam('consenttotrack') ?: 'Unchanged';

$error = null;
try {
craft()->oneCampaignMonitor_subscribers->add($listId, $email, $name, $customFields, $resubscribe);
craft()->oneCampaignMonitor_subscribers->add($listId, $email, $name, $customFields, $resubscribe, $consenttotrack);
} catch (Exception $e) {
OneCampaignMonitorPlugin::log($e->getMessage(), LogLevel::Error);
$error = $e->getMessage();
Expand Down
15 changes: 8 additions & 7 deletions services/OneCampaignMonitor_SubscribersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class OneCampaignMonitor_SubscribersService extends OneCampaignMonitor_BaseServi
* @param $merge
* @throws Exception
*/
public function add($listId, $email, $name=null, $customFields=array(), $resubscribe=true) {
public function add($listId, $email, $name=null, $customFields=array(), $resubscribe=true, $consenttotrack='Unchanged') {
if (!$listId) {
throw new Exception('List ID is required');
}
Expand All @@ -28,7 +28,8 @@ public function add($listId, $email, $name=null, $customFields=array(), $resubsc
'EmailAddress' => $email,
'Name' => $name,
'CustomFields' => $this->parseCustomFields($customFields),
'Resubscribe' => $resubscribe
'Resubscribe' => $resubscribe,
'ConsentToTrack' => $consenttotrack
]);

$error = null;
Expand Down Expand Up @@ -57,7 +58,7 @@ public function exists($listId, $email) {
$connection = new \CS_REST_Subscribers($listId, $this->auth());

$result = $connection->get($email);

$error = null;
return $this->response($result, $error);
}
Expand All @@ -81,7 +82,7 @@ public function update($listId, $email, $name=null, $customFields=array(), $resu
}

$connection = new \CS_REST_Subscribers($listId, $this->auth());

$subscriber = [
'Resubscribe' => $resubscribe
];
Expand All @@ -94,7 +95,7 @@ public function update($listId, $email, $name=null, $customFields=array(), $resu

if ($mergeMultiFields) {
$result = $connection->get($email);

$error = null;
if (!$this->response($result, $error)) {
throw new Exception($error);
Expand Down Expand Up @@ -132,11 +133,11 @@ public function addOrUpdate($listId, $email, $name=null, $customFields=array(),
if (!empty($customFields)) {
craft()->oneCampaignMonitor_lists->ensureCustomFieldsExist($listId, $customFields);
}

if ($this->exists($listId, $email)) {
return $this->update($listId, $email, $name, $customFields, $resubscribe, $mergeMultiFields);
} else {
return $this->add($listId, $email, $name, $customFields);
}
}
}
}
4 changes: 2 additions & 2 deletions variables/OneCampaignMonitorVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace Craft;

class OneCampaignMonitorVariable {
public function subscribe($list_id, $email, $name=null, $customFields=array(), $resubscribe=true) {
return craft()->oneCampaignMonitor_subscribers->add($list_id, $email, $name, $customFields, $resubscribe);
public function subscribe($list_id, $email, $name=null, $customFields=array(), $resubscribe=true, $consenttotrack='Unchanged') {
return craft()->oneCampaignMonitor_subscribers->add($list_id, $email, $name, $customFields, $resubscribe, $consenttotrack);
}

public function hasSubscribed($listId) {
Expand Down