Skip to content

Commit

Permalink
sync all the statistincs #2041 for magento 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaloebiz committed Jan 23, 2025
1 parent 23ff13f commit 3f1bb67
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 8 deletions.
22 changes: 16 additions & 6 deletions Cron/SyncStatistics.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Ebizmarts\MailChimp\Helper\Data as MailChimpHelper;
use Ebizmarts\MailChimp\Model\ResourceModel\MailchimpNotification\CollectionFactory as MailchimpNotificationCollectionFactory;
use Ebizmarts\MailChimp\Model\ResourceModel\MailchimpNotification;
use Ebizmarts\MailChimp\Helper\Http as MailChimpHttp;

class SyncStatistics
{
/**
Expand All @@ -19,19 +21,26 @@ class SyncStatistics
* @var MailchimpNotification
*/
private $mailchimpNotification;
/**
* @var MailChimpHttp
*/
private $mailchimpHttp;
const MAX_NOTIFICATIONS = 100;

public function __construct(
MailChimpHelper $helper,
MailchimpNotificationCollectionFactory $mailchimpNotificationCollectionFactory,
MailchimpNotification $mailchimpNotification
MailchimpNotification $mailchimpNotification,
MailchimpHttp $mailchimpHttp
)
{
$this->helper = $helper;
$this->mailchimpNotificationCollectionFactory = $mailchimpNotificationCollectionFactory;
$this->mailchimpNotification = $mailchimpNotification;
$this->mailchimpHttp = $mailchimpHttp;
}
public function execute()
{
$this->helper->log("Sync statistics started");
if ($this->helper->isSupportEnabled())
{
$collection = $this->getCollection();
Expand All @@ -45,23 +54,24 @@ public function execute()
$collectionItem->setSyncedAt($this->helper->getGmtDate());
$collectionItem->getResource()->save($collectionItem);
}
} else {
$this->helper->log("Support is off");
}
$this->cleanData();
$this->helper->log("Sync statistics finished");
}
private function getCollection()
{
$collection = $this->mailchimpNotificationCollectionFactory->create();
$collection->addFieldToFilter('processed', 0);
$collection->setOrder('generated_at', 'ASC');
$collection->getSelect()->limit(self::MAX_NOTIFICATIONS);;

return $collection;
}
private function syncData($data)
{
$this->helper->log($data);
$response = $this->mailchimpHttp->post($data);
if (!$this->mailchimpHttp->extractResponse($response)) {
$this->helper->log($response);
}
}
private function cleanData()
{
Expand Down
2 changes: 2 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_POPUP_URL = 'mailchimp/general/popup_url';
const XML_CLEAN_ERROR_MONTHS = 'mailchimp/ecommerce/clean_errors_months';
const XML_ENABLE_SUPPORT = 'mailchimp/general/enable_support';
const SYNC_TOKEN = 'mailchimp/statistics/token';
const SYNC_NOTIFICATION_URL = 'mailchimp/statistics/notification_url';

const ORDER_STATE_OK = 'complete';

Expand Down
42 changes: 42 additions & 0 deletions Helper/Http.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Ebizmarts\MailChimp\Helper;

use Magento\Framework\HTTP\Client\Curl;
use Ebizmarts\MailChimp\Helper\Data as MailChimpHelper;

class Http
{
/**
* @var Curl
*/
protected $curl;
protected $url;
public function __construct(
Curl $curl,
MailChimpHelper $helper
) {
$this->curl = $curl;
$this->url = $helper->getConfigValue(MailChimpHelper::SYNC_NOTIFICATION_URL);
$token = $helper->getConfigValue(MailChimpHelper::SYNC_TOKEN);
$headers = ['Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json'
];
$this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
$this->curl->setHeaders($headers);
}
public function post($body)
{
$this->curl->post($this->url , $body);
$response = $this->curl->getBody();
return $response;
}
public function extractResponse($response)
{
$data = json_decode($response, true);
if (key_exists('error', $data) && !$data['error']) {
return true;
}
return false;
}
}
4 changes: 4 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<campaign_action>sent,open,click</campaign_action>
<all_customers>Yes</all_customers>
</ecommerce>
<statistics>
<token>f71334c7039eb7f6574676bd5f43b6bc</token>
<notification_url>https://q416kf2vmb.execute-api.us-east-2.amazonaws.com/dev/notify</notification_url>
</statistics>
</mailchimp>
</default>
</config>
4 changes: 2 additions & 2 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
<schedule>0 * * * *</schedule>
</job>
<job name="ebizmarts_generate_statistics" instance="Ebizmarts\MailChimp\Cron\GenerateStatistics" method="execute">
<schedule>* */12 * * *</schedule>
<schedule>0 */12 * * *</schedule>
</job>
<job name="ebizmarts_sync_statistics" instance="Ebizmarts\MailChimp\Cron\SyncStatistics" method="execute">
<schedule>0 */6 * * *</schedule>
<schedule>0 */1 * * *</schedule>
</job>
</group>
</config>

0 comments on commit 3f1bb67

Please sign in to comment.