Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize MauticFBAdsCustomAudiencesBundle
- Loading branch information
Alexay
committed
Feb 18, 2019
0 parents
commit 1549fda
Showing
9 changed files
with
1,248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* @copyright 2017 Trinoco. All rights reserved | ||
* @author Trinoco | ||
* | ||
* @link http://trinoco.nl | ||
* | ||
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
return [ | ||
'name' => 'Advertising', | ||
'description' => 'Enables integration with Facebook Ads Custom Audiences Syncing your segments.', | ||
'version' => '1.0', | ||
'author' => 'Trinoco', | ||
'services' => [ | ||
'events' => [ | ||
'mautic.plugin.fbadsaudience.lead.subscriber' => [ | ||
'class' => 'MauticPlugin\MauticFBAdsCustomAudiencesBundle\EventListener\LeadListSubscriber', | ||
'arguments' => [ | ||
'mautic.helper.integration', | ||
], | ||
], | ||
'mautic.plugin.fbadsaudience.plugin.subscriber' => [ | ||
'class' => 'MauticPlugin\MauticFBAdsCustomAudiencesBundle\EventListener\PluginSubscriber', | ||
], | ||
], | ||
'integrations' => [ | ||
'mautic.integration.fbadsaudience' => [ | ||
'class' => \MauticPlugin\MauticFBAdsCustomAudiencesBundle\Integration\FBAdsCustomAudiencesIntegration::class, | ||
], | ||
], | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
|
||
/* | ||
* @copyright 2017 Trinoco. All rights reserved | ||
* @author Trinoco | ||
* | ||
* @link http://trinoco.nl | ||
* | ||
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
|
||
namespace MauticPlugin\MauticFBAdsCustomAudiencesBundle\EventListener; | ||
|
||
use FacebookAds\Object\CustomAudienceMultiKey; | ||
use FacebookAds\Object\Fields\CustomAudienceMultikeySchemaFields; | ||
use Mautic\CoreBundle\EventListener\CommonSubscriber; | ||
use Mautic\LeadBundle\Event\LeadListEvent; | ||
use Mautic\LeadBundle\Event\ListChangeEvent; | ||
use Mautic\PluginBundle\Helper\IntegrationHelper; | ||
use Mautic\LeadBundle\LeadEvents; | ||
|
||
use MauticPlugin\MauticFBAdsCustomAudiencesBundle\Helper\FbAdsApiHelper; | ||
|
||
|
||
/** | ||
* Class LeadListsSubscriber. | ||
*/ | ||
class LeadListSubscriber extends CommonSubscriber | ||
{ | ||
/** | ||
* @var \FacebookAds\Api | ||
*/ | ||
protected $fbAPI; | ||
|
||
/** | ||
* @var IntegrationHelper | ||
*/ | ||
protected $helper; | ||
|
||
/** | ||
* LeadSubscriber constructor. | ||
*/ | ||
public function __construct(IntegrationHelper $helper) | ||
{ | ||
$this->helper = $helper; | ||
$this->fbAPI = $this->fbApiInit(); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
LeadEvents::LIST_POST_SAVE => ['onLeadListPostSave', 0], | ||
LeadEvents::LIST_POST_DELETE => ['onLeadListPostDelete', 0], | ||
LeadEvents::LEAD_LIST_BATCH_CHANGE => ['onLeadListBatchChange', 0], | ||
LeadEvents::LEAD_LIST_CHANGE => ['onLeadListChange', 0], | ||
]; | ||
} | ||
|
||
/** | ||
* Initializes the Facebook Ads API. | ||
* | ||
* @return bool|\FacebookAds\Api|null | ||
*/ | ||
protected function fbApiInit() { | ||
$integration = $this->helper->getIntegrationObject('FBAdsCustomAudiences'); | ||
if (!$integration || !$integration->getIntegrationSettings()->isPublished()) { | ||
return FALSE; | ||
} | ||
|
||
return FbAdsApiHelper::init($integration); | ||
} | ||
|
||
/** | ||
* Add list to facebook. | ||
* | ||
* @param ListChangeEvent $event | ||
*/ | ||
public function onLeadListPostSave(LeadListEvent $event) { | ||
if (!$this->fbAPI) { | ||
return; | ||
} | ||
|
||
$list = $event->getList(); | ||
FbAdsApiHelper::addList($list); | ||
} | ||
|
||
/** | ||
* Delete list from facebook. | ||
* | ||
* @param ListChangeEvent $event | ||
*/ | ||
public function onLeadListPostDelete(LeadListEvent $event) | ||
{ | ||
if (!$this->fbAPI) { | ||
return; | ||
} | ||
|
||
$list = $event->getList(); | ||
FbAdsApiHelper::deleteList($list->getName()); | ||
} | ||
|
||
/** | ||
* Add/remove leads from facebook based on batch lead list changes. | ||
* | ||
* @param ListChangeEvent $event | ||
*/ | ||
public function onLeadListBatchChange(ListChangeEvent $event) | ||
{ | ||
if (!$this->fbAPI) { | ||
return; | ||
} | ||
|
||
if ($audience = FbAdsApiHelper::getFBAudience($event->getList()->getName())) { | ||
$users = array(); | ||
foreach ($event->getLeads() as $lead_id) { | ||
$lead = $this->em->getRepository('MauticLeadBundle:Lead')->getEntity($lead_id); | ||
|
||
if ($lead->getEmail()) { | ||
$users[] = array( | ||
$lead->getFirstname(), | ||
$lead->getLastname(), | ||
$lead->getEmail(), | ||
$lead->getMobile(), | ||
$lead->getCountry() | ||
); | ||
} | ||
} | ||
|
||
if ($event->wasAdded()) { | ||
FbAdsApiHelper::addUsers($audience, $users); | ||
} | ||
else { | ||
FbAdsApiHelper::removeUsers($audience, $users); | ||
} | ||
} | ||
|
||
// Save memory with batch processing | ||
unset($event, $users, $audience); | ||
} | ||
|
||
/** | ||
* Add/remove leads from campaigns based on lead list changes. | ||
* | ||
* @param ListChangeEvent $event | ||
*/ | ||
public function onLeadListChange(ListChangeEvent $event) | ||
{ | ||
if (!$this->fbAPI) { | ||
return; | ||
} | ||
|
||
/** @var \Mautic\LeadBundle\Entity\Lead $lead */ | ||
$lead = $event->getLead(); | ||
|
||
if ($audience = FbAdsApiHelper::getFBAudience($event->getList()->getName())) { | ||
$users = array( | ||
array($lead->getFirstname(), $lead->getLastname(), $lead->getEmail(), $lead->getMobile(), $lead->getCountry()) | ||
); | ||
|
||
if ($event->wasAdded()) { | ||
FbAdsApiHelper::addUsers($audience, $users); | ||
} | ||
else { | ||
FbAdsApiHelper::removeUsers($audience, $users); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* @copyright 2017 Trinoco. All rights reserved | ||
* @author Trinoco | ||
* | ||
* @link http://trinoco.nl | ||
* | ||
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html | ||
*/ | ||
|
||
|
||
namespace MauticPlugin\MauticFBAdsCustomAudiencesBundle\EventListener; | ||
|
||
use Mautic\CoreBundle\EventListener\CommonSubscriber; | ||
use Mautic\PluginBundle\Event\PluginIntegrationEvent; | ||
use Mautic\PluginBundle\PluginEvents; | ||
|
||
use MauticPlugin\MauticFBAdsCustomAudiencesBundle\Helper\FbAdsApiHelper; | ||
|
||
/** | ||
* Class PluginSubscriber. | ||
*/ | ||
class PluginSubscriber extends CommonSubscriber | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
PluginEvents::PLUGIN_ON_INTEGRATION_CONFIG_SAVE => ['onIntegrationConfigSave', 0], | ||
]; | ||
} | ||
|
||
public function onIntegrationConfigSave(PluginIntegrationEvent $event) { | ||
if ($event->getIntegrationName() == 'FBAdsCustomAudiences') { | ||
//$integration = $event->getIntegration(); | ||
$changes = $event->getEntity()->getChanges(); | ||
|
||
if (isset($changes['isPublished'])) { | ||
$integration = $event->getIntegration(); | ||
$api = FbAdsApiHelper::init($integration); | ||
|
||
if ($api) { | ||
$lists = $this->em->getRepository('MauticLeadBundle:LeadList')->getLists(); | ||
|
||
if ($changes['isPublished'][1] == 0) { | ||
foreach ($lists as $list) { | ||
FbAdsApiHelper::deleteList($list['name']); | ||
} | ||
} | ||
else { | ||
$listsLeads = $this->em->getRepository('MauticLeadBundle:LeadList')->getLeadsByList($lists); | ||
foreach ($lists as $list) { | ||
$listEntity = $this->em->getRepository('MauticLeadBundle:LeadList')->getEntity($list['id']); | ||
$audience = FbAdsApiHelper::addList($listEntity); | ||
|
||
$leads = $listsLeads[$listEntity->getId()]; | ||
$users = array(); | ||
|
||
foreach ($leads as $lead) { | ||
$users[] = array( | ||
$lead['firstname'], | ||
$lead['lastname'], | ||
$lead['email'], | ||
$lead['mobile'], | ||
$lead['country'], | ||
); | ||
} | ||
|
||
FbAdsApiHelper::addUsers($audience, $users); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.