Custom Field for List Segmentation Triggered by Send Instead of Open

Colt405

New Member
Currently, MW has the ability to update a custom field based on an open. I would like to be able to update that custom field based on a 'send' instead of an open. The idea is to utilize one list per industry and take the customer through four email contacts, each contact is sent based on the custom field which would be updated after each send.

What is the best way to make that happen? Build an extension? Is there something already out there like this? I would assume this should be easy since there is already a function that "Changes
subscriber custom field value upon campaign open".

Any help and guidance would be appreciated.

@twisted1919 - killer app
 
@Colt405 - At the moment the app cannot do this, it's not really a common scenario, but you can do it from an extension if you will.
There is an action hook called: 'console_command_send_campaigns_after_send_to_subscriber' that is triggered right after an email is sent in the send-campaigns command, so from an extension you could do this:
PHP:
Yii::app()->hooks->addAction('console_command_send_campaigns_after_send_to_subscriber', function(
$campaign, $subscriber, $customer, $server, $sent, $response, $status
){
    $subscriber->copyToList(123); // 123 is the list_id
});
You can use the other variables like $campaign, $customer, etc to apply conditions before copy the subscriber to the other list.
Hope it helps.

L.E: Above i just shown how to copy to another list, but you can do the same to load the custom fields and set a proper value to them, i.e:
PHP:
Yii::app()->hooks->addAction('console_command_send_campaigns_after_send_to_subscriber', function(
$campaign, $subscriber, $customer, $server, $sent, $response, $status
){
   // say the field tag is FNAME
   $field = ListField::model()->findByAttributes(array(
      'list_id' => $subscriber->list_id,
      'tag'     => 'FNAME'
   ));
   // now set a new value:
   $fieldValue = ListFieldValue::model()->findByAttributes(array(
      'field_id' => $field->field_id,
      'subscriber_id' => $subscriber->subscriber_id,
   ));
   $fieldValue->value = 'The new FNAME';
   $fieldValue->save();
});
 
Last edited:
Thank you for your reply @twisted1919.

I have used the same structure and code like your example you have provided and I upload and activate the extension from the back end. To check whether the Run() function runs properly when the app is in customer section, I have use some notify command and saw that the extension was loaded properly because it throws me that notify message but the normal work which I intend to do with this extension is not working. Actually this extension should update a custom field of the subscribers after sending out the email campaign which is not functioning.

I am providing you the code which I wrote for it. I have colour coded the hoot portion which is the main part to change the custom field value to another value after each campaign send. Those part is actually like your example code you have given me.

Can you please help?



class ExampleExt extends ExtensionInit
{
// name of the extension as shown in the backend panel
public $name = 'Update Segment';

// description of the extension as shown in backend panel
public $description = 'This will update the segment value after sending an email campaign';

// current version of this extension
public $version = '1.0';

// minimum app version
public $minAppVersion = '1.3.6.2';

// the author name
public $author = 'Mojo Developer';

// author website
public $website = 'http://www.mojocreator.com/';

// contact email address
public $email = 'developer@mojocreator.com';


public $allowedApps = array('backend', 'customer');

public $notAllowedApps = array();

public $cliEnabled = true;

// can this extension be deleted? this only applies to core extensions.
protected $_canBeDeleted = true;

// can this extension be disabled? this only applies to core extensions.
protected $_canBeDisabled = true;

/**
* The run method is the entry point of the extension.
* This method is called by mailwizz at the right time to run the extension.
*/
public function run()
{

Yii::import('ext-example.common.models.*');


if ($this->isAppName('backend')) {

/**
* Add the url rules.
* Best is to follow the pattern below for your extension to avoid name clashes.
* ext_example_settings is actually the controller file defined in controllers folder.
*/
Yii::app()->urlManager->addRules(array(
array('ext_example_settings/index', 'pattern' => 'extensions/example/settings'),
array('ext_example_settings/<action>', 'pattern' => 'extensions/example/settings/*'),
));

/**
* And now we register the controller for the above rules.
*
* Please note that you can register controllers and urls rules
* in any of the apps.
*
* Remember how we said that ext_example_settings is actually the controller file:
*/
Yii::app()->controllerMap['ext_example_settings'] = array(
// remember the ext-example path alias?
'class' => 'ext-example.backend.controllers.Ext_example_settingsController',

// pass the extension instance as a variable to the controller
'extension' => $this,
);
}

/**
* Now we can continue only if the extension is enabled from its settings:
*/

if ($this->getOption('enabled', 'no') != 'yes') {
return;
}

/**
* Hook into customers area
*/
if ($this->isAppName('customer')) {



$notify = Yii::app()->notify;
//$notify->clearError();
//$notify->clearSuccess();
//$notify->clearError()->clearSuccess()->addSuccess(Yii::t('app', 'The Hook is set which will run after each send'));
//$notify->clearSuccess();

Yii::app()->hooks->addAction('console_command_send_campaigns_after_send_to_subscriber', function(
$campaign, $subscriber, $customer, $server, $sent, $response, $status
){
//$this->addLog('within the hook');
//error_log('here');
// say the field tag is CAMPAIGNSEGMENT

$field = ListField::model()->findByAttributes(array(
'list_id' => $subscriber->list_id,
'tag' => 'CAMPAIGNSEGMENT'
));

// now set a new value:
$fieldValue = ListFieldValue::model()->findByAttributes(array(
'field_id' => $field->field_id,
'subscriber_id' => $subscriber->subscriber_id,
));
$notify->clearSuccess();
$notify->addSuccess(Yii::t('app', 'In the hook now'));

//$notify->addSuccess(Yii::t('app', $fieldValue->value));
////$notify->clearSuccess();

$fieldValue->value = '1_2';
$fieldValue->save();

//$notify->addError(Yii::t('app', 'Action Runs'));
//$notify->clearError();
//return 1;

});
}



}

/**
* This is an inherit method where we define the url to our settings page in backed.
* Remember that we can click on an extension title to view the extension settings.
* This method generates that link.
*/
public function getPageUrl()
{
return Yii::app()->createUrl('ext_example_settings/index');
}

/**
* Code to run before enabling the extension.
* Make sure to call the parent implementation
*
* Please note that if you return false here
* the extension will not be enabled.
*/
public function beforeEnable()
{
// your code here

// call parent
return parent::beforeEnable();
}

/**
* Code to run after enabling the extension.
* Make sure to call the parent implementation
*/
public function afterEnable()
{
// your code here


// call parent
parent::afterEnable();
}

/**
* Code to run before disable the extension.
* Make sure to call the parent implementation
*
* Please note that if you return false here
* the extension will not be disabled.
*/
public function beforeDisable()
{
// your code here

// call parent
return parent::beforeDisable();
}

/**
* Code to run after disable the extension.
* Make sure to call the parent implementation
*/
public function afterDisable()
{
// your code here

// call parent
parent::afterDisable();
}

/**
* Code to run before delete the extension.
* Make sure to call the parent implementation
*
* Please note that if you return false here
* the extension will not be deleted.
*/
public function beforeDelete()
{
// your code here

// call parent
return parent::beforeDelete();
}

/**
* Code to run after delete the extension.
* Make sure to call the parent implementation
*/
public function afterDelete()
{
// your code here

// remove the custom option
$this->removeOption('myCustomvariable');

// or remove all options.
$this->removeAllOptions();

// call parent
parent::afterDelete();
}

/**
* This method is called to check if an extension needs update
*/
public function checkUpdate()
{

}

/**
* This is called when the extension is actually updated
* So update logic goes here.
*/
public function update()
{

}
}
 
@Colt405 - First thing, public $allowedApps = array('backend', 'customer'); should also contain the console app, so public $allowedApps = array('backend', 'customer', 'console');
Second, you set the hook for the console app inside the if ($this->isAppName('customer')) { , which is not correct. you should check like:
Code:
if ( $this->isAppName('console') ) {
    Yii::app()->hooks->addAction('console_command_send_campaigns_after_send_to_subscriber', function(....
}
Think that the code inside if ($this->isAppName('customer')) { never runs in command line, but the one in if ( $this->isAppName('console') ) {... does.

Makes sense now ?
 
@twisted1919 , Thank you very much for your reply.

But my point is, though the action hook represents the console command which will execute after campaign is send to customer, but when I am sending the campaign, I am not using any console to send the campaign, I am just clicking a button or schedule the campaign to fire automatically. So how the console section will run? Is Mailwizz sending the campaigns internally using console?
 
So how the console section will run? Is Mailwizz sending the campaigns internally using console?
You just schedule it then. The cron job that runs each minute will pickup scheduled campaigns and process them. This all happens in console.
 
Back
Top