Skip to content

Commit fd5c207

Browse files
authored
Analytics Page PHP client v2 compatibility (#967)
1 parent 3515724 commit fd5c207

File tree

5 files changed

+54
-16
lines changed

5 files changed

+54
-16
lines changed

Helper/AnalyticsHelper.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Algolia\AlgoliaSearch\Helper;
44

55
use Algolia\AlgoliaSearch\AnalyticsClient;
6+
use Algolia\AlgoliaSearch\Config\AnalyticsConfig;
67
use Algolia\AlgoliaSearch\DataProvider\Analytics\IndexEntityDataProvider;
7-
use AlgoliaSearch\Analytics;
8+
use Algolia\AlgoliaSearch\RequestOptions\RequestOptionsFactory;
89

910
class AnalyticsHelper
1011
{
@@ -48,8 +49,11 @@ class AnalyticsHelper
4849
private $analyticsClient;
4950

5051
/**
51-
* AnalyticsHelper constructor.
52-
*
52+
* @var AnalyticsConfig
53+
*/
54+
private $analyticsConfig;
55+
56+
/**
5357
* @param AlgoliaHelper $algoliaHelper
5458
* @param ConfigHelper $configHelper
5559
* @param IndexEntityDataProvider $entityHelper
@@ -82,6 +86,11 @@ private function setupAnalyticsClient()
8286
$this->configHelper->getApplicationID(),
8387
$this->configHelper->getAPIKey()
8488
);
89+
90+
$this->analyticsConfig = AnalyticsConfig::create(
91+
$this->configHelper->getApplicationID(),
92+
$this->configHelper->getAPIKey()
93+
);
8594
}
8695

8796
/**
@@ -350,9 +359,16 @@ private function fetch($path, array $params)
350359
throw new \Magento\Framework\Exception\LocalizedException($msg);
351360
}
352361

353-
$response = $this->analyticsClient->custom('GET', $path, $params);
362+
$this->setupAnalyticsClient();
363+
364+
$requestOptions = new RequestOptionsFactory($this->analyticsConfig);
365+
$requestOptions = $requestOptions->create([]);
366+
367+
$requestOptions->addQueryParameters($params);
368+
369+
$response = $this->analyticsClient->custom('GET', $path, $requestOptions);
354370
} catch (\Exception $e) {
355-
$this->errors[] = $e->getMessage();
371+
$this->errors[] = $e->getMessage() . ': ' . $path;
356372
$this->logger->log($e->getMessage());
357373

358374
$this->fetchError = true;

ViewModel/Adminhtml/Analytics/Overview.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ public function __construct(
4444
$this->backendView = $backendView;
4545
$this->analyticsHelper = $analyticsHelper;
4646
$this->indexEntityDataProvider = $indexEntityDataProvider;
47-
48-
$this->getTotalCountOfSearches();
4947
}
5048

5149
/**
@@ -56,6 +54,14 @@ public function getBackendView()
5654
return $this->backendView;
5755
}
5856

57+
public function getTimeZone()
58+
{
59+
return $this->backendView->getDateTime()->getConfigTimezone(
60+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
61+
$this->getStore()->getId()
62+
);
63+
}
64+
5965
/**
6066
* @throws NoSuchEntityException
6167
*
@@ -80,13 +86,13 @@ public function getAnalyticsParams($additional = [])
8086
if (empty($this->analyticsParams)) {
8187
$params = ['index' => $this->getIndexName()];
8288
if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) {
89+
$dateTime = $this->getBackendView()->getDateTime();
90+
$timeZone = $this->getTimeZone();
8391
if (isset($formData['from']) && $formData['from'] !== '') {
84-
$params['startDate'] = date('Y-m-d', $this->getBackendView()->getDateTime()
85-
->date($formData['from'])->getTimestamp());
92+
$params['startDate'] = $dateTime->date($formData['from'], $timeZone, true, false)->format('Y-m-d');
8693
}
8794
if (isset($formData['to']) && $formData['to'] !== '') {
88-
$params['endDate'] = date('Y-m-d', $this->getBackendView()->getDateTime()
89-
->date($formData['to'])->getTimestamp());
95+
$params['endDate'] = $dateTime->date($formData['to'], $timeZone, true, false)->format('Y-m-d');
9096
}
9197
}
9298

@@ -296,14 +302,18 @@ public function getNoResultSearches()
296302
}
297303

298304
/**
305+
* @throws NoSuchEntityException
306+
*
299307
* @return bool
300308
*/
301309
public function checkIsValidDateRange()
302310
{
303311
if ($formData = $this->getBackendView()->getBackendSession()->getAlgoliaAnalyticsFormData()) {
304312
if (isset($formData['from']) && !empty($formData['from'])) {
305-
$startDate = $this->getBackendView()->getDateTime()->date($formData['from']);
306-
$diff = date_diff($startDate, $this->getBackendView()->getDateTime()->date());
313+
$dateTime = $this->getBackendView()->getDateTime();
314+
$timeZone = $this->getTimeZone();
315+
$startDate = $dateTime->date($formData['from'], $timeZone, true, false);
316+
$diff = date_diff($startDate, $dateTime->date(null, $timeZone, true, null));
307317

308318
if ($diff->days > $this->getAnalyticRetentionDays()) {
309319
return false;

view/adminhtml/layout/algolia_algoliasearch_analytics_index.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
</block>
2222
</referenceContainer>
2323
<referenceContainer name="content">
24-
<block class="Magento\Backend\Block\Template" name="analytics_overview" template="Algolia_AlgoliaSearch::analytics/overview.phtml">
24+
<block class="Magento\Backend\Block\Template" name="analytics.overview" template="Algolia_AlgoliaSearch::analytics/overview.phtml">
2525
<arguments>
2626
<argument name="view_model" xsi:type="object">Algolia\AlgoliaSearch\ViewModel\Adminhtml\Analytics\Overview</argument>
2727
</arguments>

view/adminhtml/layout/default.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
</head>
66

77
<referenceBlock name="before.body.end">
8-
<block class="Magento\Backend\Block\Template" name="algolia_tracking" template="Algolia_AlgoliaSearch::tracking.phtml" />
8+
<block class="Magento\Backend\Block\Template" name="algolia.tracking" template="Algolia_AlgoliaSearch::tracking.phtml">
9+
<arguments>
10+
<argument name="view_model" xsi:type="object">Algolia\AlgoliaSearch\ViewModel\Adminhtml\Common</argument>
11+
</arguments>
12+
</block>
913
</referenceBlock>
1014
</page>

view/adminhtml/templates/tracking.phtml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
<?php
2+
/** @var \Magento\Backend\Block\Template $block */
3+
/** @var \Algolia\AlgoliaSearch\ViewModel\Adminhtml\Common $viewModel */
4+
$view = $block->getViewModel();
5+
$applicationId = $view->getApplicationId();
6+
?>
7+
18
<script>
9+
var algoliaAppId = '<?php echo $block->escapeJs($applicationId) ?>';
210
var algoliaEventsTracking = {
311

412
eventsToTrack: [
@@ -359,7 +367,7 @@ var algoliaEventsTracking = {
359367
data = data || {};
360368

361369
const postData = JSON.stringify({
362-
appId: applicationId,
370+
appId: algoliaAppId,
363371
eventName: eventName,
364372
data: data
365373
});

0 commit comments

Comments
 (0)