Skip to content

Commit

Permalink
Merged PR 53524: Ensure CartInsight collection is created at dummy da…
Browse files Browse the repository at this point in the history
…ta push

## What's being changed

The dummy records data upload for cart insight.

## Why it's being changed

This process happens whenever account credentials are saved, and is designed to ensure that any connected Dotdigital account can use the abandoned cart block immediately after connection.

We [migrated to V3 to send this data recently](914d39d), but overlooked that the PUT `/insightData/v3/contacts/{identifier}/{value}/{collectionName}/{recordId}` method to add contact insight data to a named collection won't actually create the collection if it doesn't exist already. For this we need the PUT `/insightData/v3/import`.

Note we have intentionally reverted some of the changes made previously because `/insightData/v3/import` takes a different schema.

## How to review / test this change

- In your connected DD account, delete the CartInsight collection (you might want to do this on a nightly env because it can involve deleting a lot of connected pieces in DD)
- Go to Dotdigital > Accounts and save your credentials (any scope is fine)
- Confirm that a new contact insight data collection 'CartInsight' is added with 1 record
- Check the data via the contact you added it to

Related work items: #250410
  • Loading branch information
sta1r committed Apr 17, 2024
1 parent 80c7fd2 commit 0f96b6b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 47 deletions.
79 changes: 40 additions & 39 deletions Model/DummyRecordsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Dotdigitalgroup\Email\Helper\Data;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Dotdigitalgroup\Email\Logger\Logger;
use Dotdigitalgroup\Email\Model\Apiconnector\Account;
use Magento\Framework\UrlInterface;
use Dotdigitalgroup\Email\Model\Connector\AccountHandler;
Expand All @@ -29,11 +28,6 @@ class DummyRecordsData
*/
public $email = [];

/**
* @var Logger
*/
private $logger;

/**
* @var Account
*/
Expand All @@ -49,20 +43,17 @@ class DummyRecordsData
*
* @param Data $helper
* @param StoreManagerInterface $storeManager
* @param Logger $logger
* @param Account $account
* @param AccountHandler $accountHandler
*/
public function __construct(
Data $helper,
StoreManagerInterface $storeManager,
Logger $logger,
Account $account,
AccountHandler $accountHandler
) {
$this->helper = $helper;
$this->storeManager = $storeManager;
$this->logger = $logger;
$this->account = $account;
$this->accountHandler = $accountHandler;
}
Expand Down Expand Up @@ -97,7 +88,20 @@ public function getContactInsightData($websiteId): array
{
/** @var Store $store */
$store = $this->getStore($websiteId);
return $this->getStaticData($store);
return $this->getStaticData($store, $websiteId);
}

/**
* Get account email.
*
* @param string|int $websiteId
* @return string
* @throws \Exception
*/
public function getEmailFromAccountInfo($websiteId)
{
$accountInfo = $this->helper->getWebsiteApiClient($websiteId)->getAccountInfo();
return $this->account->getAccountOwnerEmail($accountInfo);
}

/**
Expand All @@ -121,23 +125,33 @@ private function getStore($websiteId)
* Get static data.
*
* @param Store $store
* @param int $websiteId
*
* @return array
* @throws \Exception
*/
private function getStaticData($store)
private function getStaticData($store, $websiteId)
{
$data = [
'cartId' => '1',
'cartUrl' => $store->getBaseUrl(
UrlInterface::URL_TYPE_WEB,
$store->isCurrentlySecure()
).'connector/email/getbasket/quote_id/1/',
'createdDate' => date(\DateTime::ATOM, time()),
'modifiedDate' => date(\DateTime::ATOM, time()),
'currency' => 'USD',
'subTotal' => round(52, 2),
'taxAmount' => (float) 0,
'shipping' => (float) 0,
'grandTotal' => round(52, 2)
'key' => '1',
'contactIdentity' => [
'identifier' => 'email',
'value' => $this->getEmailFromAccountInfo($websiteId)
],
'json' => [
'cartId' => '1',
'cartUrl' => $store->getBaseUrl(
UrlInterface::URL_TYPE_WEB,
$store->isCurrentlySecure()
).'connector/email/getbasket/quote_id/1/',
'createdDate' => date(\DateTime::ATOM, time()),
'modifiedDate' => date(\DateTime::ATOM, time()),
'currency' => 'USD',
'subTotal' => round(52, 2),
'taxAmount' => (float) 0,
'shipping' => (float) 0,
'grandTotal' => round(52, 2)
]
];

$lineItems[] = [
Expand All @@ -154,9 +168,9 @@ private function getStaticData($store)
'totalPrice' => round(52, 2)
];

$data['discountAmount'] = 0;
$data['lineItems'] = $lineItems;
$data['cartPhase'] = 'ORDER_PENDING';
$data['json']['discountAmount'] = 0;
$data['json']['lineItems'] = $lineItems;
$data['json']['cartPhase'] = 'ORDER_PENDING';

return $data;
}
Expand All @@ -172,17 +186,4 @@ private function getImageUrl()
.'magento/magento2-sample-data/'
.'2.3/pub/media/catalog/product/m/h/mh01-black_main.jpg';
}

/**
* Get account email.
*
* @param string|int $websiteId
* @return string
* @throws \Exception
*/
public function getEmailFromAccountInfo($websiteId)
{
$accountInfo = $this->helper->getWebsiteApiClient($websiteId)->getAccountInfo();
return $this->account->getAccountOwnerEmail($accountInfo);
}
}
18 changes: 10 additions & 8 deletions Model/Sync/DummyRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Dotdigital\Exception\ResponseValidationException;
use Dotdigital\V3\Models\ContactFactory as DotdigitalContactFactory;
use Dotdigital\V3\Models\InsightData;
use Dotdigitalgroup\Email\Logger\Logger;
use Dotdigitalgroup\Email\Model\Apiconnector\V3\ClientFactory;
use Dotdigitalgroup\Email\Model\DummyRecordsData;
Expand Down Expand Up @@ -85,7 +86,6 @@ public function syncForWebsite($websiteId)
private function postContactAndCartInsightData($websiteId = 0)
{
$identifier = $this->dummyData->getEmailFromAccountInfo($websiteId);
$cartInsightData = $this->dummyData->getContactInsightData($websiteId);
$client = $this->clientFactory->create(
['data' => ['websiteId' => $websiteId]]
);
Expand All @@ -109,13 +109,15 @@ private function postContactAndCartInsightData($websiteId = 0)
}

try {
$client->insightData->createOrUpdateContactCollectionRecord(
'CartInsight',
'1',
'email',
$this->dummyData->getEmailFromAccountInfo($websiteId),
$cartInsightData
);
$cartInsightData = new InsightData([
'collectionName' => 'CartInsight',
'collectionScope' => 'contact',
'collectionType' => 'cartInsight'
]);
$cartInsightData->setRecords([
$this->dummyData->getContactInsightData($websiteId)
]);
$client->insightData->import($cartInsightData);
} catch (ResponseValidationException $e) {
$this->logger->debug(
sprintf(
Expand Down

0 comments on commit 0f96b6b

Please sign in to comment.