-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from EmicoEcommerce/feat-tw-analytics
Feat: tw analytics
- Loading branch information
Showing
17 changed files
with
626 additions
and
20 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,77 @@ | ||
<?php | ||
|
||
namespace Tweakwise\Magento2Tweakwise\Controller\Ajax; | ||
|
||
use Magento\Framework\App\Action\Action; | ||
use Magento\Framework\App\Action\Context; | ||
use Magento\Framework\Controller\Result\JsonFactory; | ||
use Tweakwise\Magento2Tweakwise\Model\Client; | ||
use Tweakwise\Magento2Tweakwise\Model\PersonalMerchandisingConfig; | ||
use Magento\Framework\Stdlib\Cookie\PublicCookieMetadata; | ||
use Magento\Framework\Stdlib\CookieManagerInterface; | ||
use Tweakwise\Magento2Tweakwise\Model\Client\RequestFactory; | ||
use Magento\Framework\App\ResponseInterface; | ||
use Magento\Framework\Controller\Result\Json; | ||
use Magento\Framework\Controller\ResultInterface; | ||
|
||
class Analytics extends Action | ||
{ | ||
/** | ||
* Constructor. | ||
* | ||
* @param Context $context | ||
* @param JsonFactory $resultJsonFactory | ||
* @param Client $client | ||
* @param PersonalMerchandisingConfig $config | ||
* @param RequestFactory $requestFactory | ||
*/ | ||
public function __construct( | ||
private Context $context, | ||
private JsonFactory $resultJsonFactory, | ||
private Client $client, | ||
private PersonalMerchandisingConfig $config, | ||
private RequestFactory $requestFactory | ||
) { | ||
parent::__construct($context); | ||
} | ||
|
||
/** | ||
* @return ResponseInterface|Json|ResultInterface | ||
*/ | ||
public function execute() | ||
{ | ||
$result = $this->resultJsonFactory->create(); | ||
if ($this->config->isAnalyticsEnabled()) { | ||
$type = $this->getRequest()->getParam('type'); | ||
$profileKey = $this->config->getProfileKey(); | ||
|
||
$tweakwiseRequest = $this->requestFactory->create(); | ||
$tweakwiseRequest->setProfileKey($profileKey); | ||
$value = $this->getRequest()->getParam('value'); | ||
|
||
if ($type === 'product') { | ||
$tweakwiseRequest->setParameter('productKey', $value); | ||
$tweakwiseRequest->setPath('pageview'); | ||
} elseif ($type === 'search') { | ||
$tweakwiseRequest->setParameter('searchTerm', $value); | ||
$tweakwiseRequest->setPath('search'); | ||
} | ||
|
||
if (!empty($tweakwiseRequest->getPath())) { | ||
try { | ||
$this->client->request($tweakwiseRequest); | ||
$result->setData(['success' => true]); | ||
} catch (\Exception $e) { | ||
$result->setData( | ||
[ | ||
'success' => false, | ||
'message' => $e->getMessage() | ||
] | ||
); | ||
} | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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
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
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,69 @@ | ||
<?php | ||
|
||
/** | ||
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved | ||
* | ||
* @copyright Copyright (c) 2017-2022 Tweakwise.com B.V. (https://www.tweakwise.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
namespace Tweakwise\Magento2Tweakwise\Model\Client\Request; | ||
|
||
use Tweakwise\Magento2Tweakwise\Model\Client\Request; | ||
use Tweakwise\Magento2Tweakwise\Model\Client\Response\FacetResponse; | ||
|
||
class AnalyticsRequest extends Request | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $path = ''; | ||
|
||
protected $apiUrl = 'https://navigator-analytics.tweakwise.com/api'; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function isPostRequest() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getApiurl() | ||
{ | ||
return $this->apiUrl; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function setProfileKey(string $profileKey) | ||
{ | ||
$this->setParameter('ProfileKey', $profileKey); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function setPath($path) | ||
{ | ||
$this->path = $path; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getProfileKey() | ||
{ | ||
$profileKey = $this->getCookie($this->config->getPersonalMerchandisingCookieName()); | ||
if (!$profileKey) { | ||
$profileKey = $this->generateProfileKey(); | ||
$this->setCookie('profileKey', $profileKey); | ||
} | ||
|
||
return $profileKey; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
/** | ||
* Tweakwise (https://www.tweakwise.com/) - All Rights Reserved | ||
* | ||
* @copyright Copyright (c) 2017-2022 Tweakwise.com B.V. (https://www.tweakwise.com) | ||
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | ||
*/ | ||
|
||
namespace Tweakwise\Magento2Tweakwise\Model\Client\Request; | ||
|
||
use Tweakwise\Magento2Tweakwise\Model\Client\Request; | ||
use Tweakwise\Magento2Tweakwise\Model\Client\Response\FacetResponse; | ||
|
||
class PurchaseRequest extends Request | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $path = 'purchase'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $productKeys; | ||
|
||
protected $apiUrl = 'https://navigator-analytics.tweakwise.com/api'; | ||
|
||
/** | ||
* @return true | ||
*/ | ||
public function isPostRequest() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* @return mixed|string | ||
*/ | ||
public function getApiurl() | ||
{ | ||
return $this->apiUrl; | ||
} | ||
|
||
/** | ||
* @param string $profileKey | ||
* | ||
* @return void | ||
*/ | ||
public function setProfileKey(string $profileKey) | ||
{ | ||
$this->setParameter('ProfileKey', $profileKey); | ||
} | ||
} |
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
Oops, something went wrong.