Skip to content

Commit 875ba31

Browse files
authored
Merge pull request #1498 from algolia/feature/MAGE-780
Feature/mage 780 - Revenue analytics
2 parents 19b364a + d1b6e08 commit 875ba31

38 files changed

+3231
-2031
lines changed

Api/Insights/EventsInterface.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Api\Insights;
4+
5+
use Algolia\AlgoliaSearch\Api\InsightsClient;
6+
use Algolia\AlgoliaSearch\Exceptions\AlgoliaException;
7+
use Magento\Framework\Exception\LocalizedException;
8+
use Magento\Quote\Model\Quote\Item;
9+
use Magento\Sales\Model\Order;
10+
use Magento\Store\Model\StoreManagerInterface;
11+
12+
interface EventsInterface
13+
{
14+
/** @var string */
15+
public const EVENT_KEY_SUBTYPE = 'eventSubtype';
16+
/** @var string */
17+
public const EVENT_KEY_OBJECT_IDS = 'objectIDs';
18+
/** @var string */
19+
public const EVENT_KEY_OBJECT_DATA = 'objectData';
20+
/** @var string */
21+
public const EVENT_KEY_CURRENCY = 'currency';
22+
/** @var string */
23+
public const EVENT_KEY_VALUE = 'value';
24+
/** @var string */
25+
public const EVENT_KEY_QUERY_ID = 'queryID';
26+
27+
/** @var string */
28+
public const EVENT_SUBTYPE_CART = 'addToCart';
29+
/** @var string */
30+
public const EVENT_SUBTYPE_PURCHASE = 'purchase';
31+
32+
// https://www.algolia.com/doc/rest-api/insights/#method-param-objectids
33+
/** @var int */
34+
public const MAX_OBJECT_IDS_PER_EVENT = 20;
35+
36+
// https://www.algolia.com/doc/rest-api/insights/#events-endpoints
37+
/** @var int */
38+
public const MAX_EVENTS_PER_REQUEST = 1000;
39+
40+
public function setInsightsClient(InsightsClient $client): EventsInterface;
41+
42+
public function setAuthenticatedUserToken(string $token): EventsInterface;
43+
44+
public function setAnonymousUserToken(string $token): EventsInterface;
45+
46+
public function setStoreManager(StoreManagerInterface $storeManager): EventsInterface;
47+
48+
/**
49+
* @param string $eventName
50+
* @param string $indexName
51+
* @param array $objectIDs
52+
* @param string $queryID
53+
* @param array $requestOptions
54+
* @return array<string, mixed> API response
55+
* @throws AlgoliaException
56+
*/
57+
public function convertedObjectIDsAfterSearch(
58+
string $eventName,
59+
string $indexName,
60+
array $objectIDs,
61+
string $queryID,
62+
array $requestOptions = []
63+
): array;
64+
65+
/**
66+
* @param string $eventName
67+
* @param string $indexName
68+
* @param array $objectIDs
69+
* @param array $requestOptions
70+
* @return array<string, mixed> API response
71+
* @throws AlgoliaException
72+
*/
73+
public function convertedObjectIDs(
74+
string $eventName,
75+
string $indexName,
76+
array $objectIDs,
77+
array $requestOptions = []
78+
): array;
79+
80+
/**
81+
* Track conversion for add to cart operation
82+
* @param string $eventName
83+
* @param string $indexName
84+
* @param Item $item
85+
* @param string|null $queryID specify if conversion is result of a search
86+
* @return array<string, mixed> API response
87+
* @throws AlgoliaException
88+
* @throws LocalizedException
89+
*/
90+
public function convertAddToCart(
91+
string $eventName,
92+
string $indexName,
93+
Item $item,
94+
string $queryID = null
95+
): array;
96+
97+
/**
98+
* Track purchase conversion for all items on an order in as few batches as possible
99+
* @param string $eventName
100+
* @param string $indexName
101+
* @param Order $order
102+
* @return array<array<string, mixed>> An array of API responses for all batches processed
103+
* @throws AlgoliaException
104+
* @throws LocalizedException
105+
*/
106+
public function convertPurchase(
107+
string $eventName,
108+
string $indexName,
109+
Order $order
110+
): array;
111+
112+
/**
113+
* Track purchase conversion event for an arbitrary group of items
114+
* @param string $eventName
115+
* @param string $indexName
116+
* @param Order\Item[] $items
117+
* @param string|null $queryID
118+
* @return array<string, mixed> API response
119+
* @throws AlgoliaException
120+
* @throws LocalizedException
121+
*/
122+
public function convertPurchaseForItems(
123+
string $eventName,
124+
string $indexName,
125+
array $items,
126+
string $queryID = null
127+
): array;
128+
129+
}

Block/Checkout/Conversion.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Block\Checkout;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\InsightsHelper;
67
use Magento\Checkout\Model\Session;
78
use Magento\Framework\View\Element\Template;
89
use Magento\Framework\View\Element\Template\Context;
@@ -52,8 +53,8 @@ public function getOrderItemsConversionJson()
5253

5354
/** @var Item $item */
5455
foreach ($orderItems as $item) {
55-
if ($item->hasData('algoliasearch_query_param')) {
56-
$orderItemsData[$item->getProductId()] = json_decode($item->getData('algoliasearch_query_param'));
56+
if ($item->hasData(InsightsHelper::QUOTE_ITEM_QUERY_PARAM)) {
57+
$orderItemsData[$item->getProductId()] = json_decode($item->getData(InsightsHelper::QUOTE_ITEM_QUERY_PARAM));
5758
}
5859
}
5960

@@ -64,7 +65,7 @@ public function toHtml()
6465
{
6566
$storeId = $this->checkoutSession->getLastRealOrder()->getStoreId();
6667
if ($this->configHelper->isClickConversionAnalyticsEnabled($storeId)
67-
&& $this->configHelper->getConversionAnalyticsMode($storeId) === 'place_order'
68+
&& $this->configHelper->getConversionAnalyticsMode($storeId) === InsightsHelper::CONVERSION_ANALYTICS_MODE_PURCHASE
6869
) {
6970
return parent::toHtml();
7071
}

Block/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Algolia\AlgoliaSearch\Block;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\InsightsHelper;
67
use Magento\Framework\App\Request\Http;
78
use Magento\Framework\Data\CollectionDataSourceInterface;
89
use Magento\Framework\DataObject;
@@ -289,6 +290,7 @@ public function getConfiguration()
289290
'logo' => $this->getViewFileUrl('Algolia_AlgoliaSearch::images/algolia-logo-blue.svg'),
290291
],
291292
'cookieConfiguration' => [
293+
'customerTokenCookie' => InsightsHelper::ALGOLIA_CUSTOMER_USER_TOKEN_COOKIE_NAME,
292294
'consentCookieName' => $config->getDefaultConsentCookieName(),
293295
'cookieAllowButtonSelector' => $config->getAllowCookieButtonSelector(),
294296
'cookieRestrictionModeEnabled' => $config->isCookieRestrictionModeEnabled(),

Helper/AlgoliaHelper.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AlgoliaHelper extends AbstractHelper
3737
*/
3838
protected const ALGOLIA_API_SECURED_KEY_TIMEOUT_SECONDS = 60 * 60 * 24; // TODO: Implement as config
3939

40-
protected SearchClient $client;
40+
protected ?SearchClient $client = null;
4141

4242
protected ConfigHelper $config;
4343

@@ -83,12 +83,14 @@ public function __construct(
8383
$this->config->getNonCastableAttributes()
8484
);
8585

86-
$clientName = $this->client->getClientConfig()->getClientName();
86+
$clientName = $this->client?->getClientConfig()?->getClientName();
8787

88-
AlgoliaAgent::addAlgoliaAgent($clientName, 'Magento2 integration', $this->config->getExtensionVersion());
89-
AlgoliaAgent::addAlgoliaAgent($clientName, 'PHP', phpversion());
90-
AlgoliaAgent::addAlgoliaAgent($clientName, 'Magento', $this->config->getMagentoVersion());
91-
AlgoliaAgent::addAlgoliaAgent($clientName, 'Edition', $this->config->getMagentoEdition());
88+
if ($clientName) {
89+
AlgoliaAgent::addAlgoliaAgent($clientName, 'Magento2 integration', $this->config->getExtensionVersion());
90+
AlgoliaAgent::addAlgoliaAgent($clientName, 'PHP', phpversion());
91+
AlgoliaAgent::addAlgoliaAgent($clientName, 'Magento', $this->config->getMagentoVersion());
92+
AlgoliaAgent::addAlgoliaAgent($clientName, 'Edition', $this->config->getMagentoEdition());
93+
}
9294
}
9395

9496
/**
@@ -319,17 +321,18 @@ public function generateSearchSecuredApiKey(string $key, array $params = []): st
319321

320322
/**
321323
* @param $indexName
322-
* @return void
324+
* @return array<string, mixed>
323325
* @throws \Exception
324326
*/
325-
public function getSettings($indexName)
327+
public function getSettings(string $indexName): array
326328
{
327329
try {
328330
return $this->client->getSettings($indexName);
329-
}catch (\Exception $e) {
331+
} catch (\Exception $e) {
330332
if ($e->getCode() !== 404) {
331333
throw $e;
332334
}
335+
return [];
333336
}
334337
}
335338

@@ -542,6 +545,8 @@ protected function checkClient($methodName): void
542545
*/
543546
public function clearIndex(string $indexName): void
544547
{
548+
$this->checkClient(__FUNCTION__);
549+
545550
$res = $this->client->clearObjects($indexName);
546551

547552
self::setLastOperationInfo($indexName, $res);
@@ -578,7 +583,7 @@ public function waitLastTask(string $lastUsedIndexName = null, int $lastTaskId =
578583
* @return void
579584
* @throws \Exception
580585
*/
581-
protected function prepareRecords(array $objects, string $indexName): void
586+
protected function prepareRecords(array &$objects, string $indexName): void
582587
{
583588
$currentCET = new \DateTime('now', new \DateTimeZone('Europe/Paris'));
584589
$currentCET = $currentCET->format('Y-m-d H:i:s');

0 commit comments

Comments
 (0)