Skip to content

Commit d6203c8

Browse files
committed
updates POC, adds integration_key
1 parent 6572138 commit d6203c8

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

Block/Catalog/Product/ViewedProduct.php

+11-12
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Klaviyo\Reclaim\Block\Catalog\Product;
44

5-
use Klaviyo\Reclaim\Helper\Logger;
65
use Klaviyo\Reclaim\Helper\ScopeSetting;
6+
use Klaviyo\Reclaim\Helper\Data;
77
use Magento\Catalog\Helper\Image;
88
use Magento\Framework\Registry;
99
use Magento\Framework\View\Element\Template;
@@ -16,12 +16,7 @@ class ViewedProduct extends Template
1616
protected $imageUrl = null;
1717
protected $categories = [];
1818
protected $price = 0;
19-
20-
/**
21-
* Klaviyo Logger
22-
* @var Logger
23-
*/
24-
protected $_klaviyoLogger;
19+
const INTEGRATION_KEY = 'magento_two';
2520

2621
/**
2722
* @var Magento\Catalog\Helper\Image
@@ -32,25 +27,24 @@ class ViewedProduct extends Template
3227
* ViewedProduct constructor.
3328
* @param Context $context
3429
* @param ScopeSetting $klaviyoScopeSetting
35-
* @param Logger $klaviyoLogger
3630
* @param Registry $registry
3731
* @param Image $imageHelper
32+
* @param Data $dataHelper
3833
* @param array $data
3934
*/
4035
public function __construct(
4136
Context $context,
4237
ScopeSetting $klaviyoScopeSetting,
43-
Logger $klaviyoLogger,
4438
Registry $registry,
4539
Image $imageHelper,
40+
Data $dataHelper,
4641
array $data = []
4742
) {
48-
// $this->_storeManager = $context->getStoreManager();
4943
parent::__construct($context, $data);
50-
$this->_klaviyoLogger = $klaviyoLogger;
5144
$this->_klaviyoScopeSetting = $klaviyoScopeSetting;
5245
$this->_registry = $registry;
5346
$this->imageHelper = $imageHelper;
47+
$this->_dataHelper = $dataHelper;
5448
}
5549

5650
/**
@@ -202,11 +196,16 @@ protected function _toHtml()
202196
*/
203197
public function getViewedProductJson()
204198
{
205-
$external_catalog_id = $this->_storeManager->getStore()->getWebsiteId() . '-' . $this->_storeManager->getStore()->getId();
199+
$external_catalog_id = $this->_dataHelper->getExternalCatalogIdForEvent(
200+
$this->_storeManager->getStore()->getWebsiteId(),
201+
$this->_storeManager->getStore()->getId()
202+
);
203+
206204
$_product = $this->getProduct();
207205

208206
$result = [
209207
'external_catalog_id' => $external_catalog_id,
208+
'integration_key' => self::INTEGRATION_KEY,
210209
'ProductID' => $_product->getId(),
211210
'Name' => $_product->getName(),
212211
'SKU' => $_product->getSku(),

Helper/Data.php

+13
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,17 @@ public function klaviyoTrackEvent($event, $customer_properties = [], $properties
185185
$api = new KlaviyoV3Api($this->_klaviyoScopeSetting->getPublicApiKey($storeId), $this->_klaviyoScopeSetting->getPrivateApiKey($storeId), $this->_klaviyoScopeSetting, $this->_klaviyoLogger);
186186
return $api->track($params);
187187
}
188+
189+
/**
190+
* Get the external catalog ID for an event. This is used to link events to a specific scoped catalog in Klaviyo, so that
191+
* profile interest events can be connected to a specific scoped product when building flow audiences.
192+
*
193+
* @param int $website_id
194+
* @param int $store_id
195+
* @return string
196+
*/
197+
public function getExternalCatalogIdForEvent($website_id, $store_id)
198+
{
199+
return $website_id . '-' . $store_id;
200+
}
188201
}

KlaviyoV3Sdk/KlaviyoV3Api.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ class KlaviyoV3Api
6868
const PROFILES_PAYLOAD_KEY = 'profiles';
6969
const CUSTOM_SOURCE_PAYLOAD_KEY = 'custom_source';
7070
const MAGENTO_TWO_PAYLOAD_VALUE = 'Magento Two';
71-
const MAGENTO_TWO_INTEGRATION_SERVICE_KEY = 'magentotwo';
72-
const SERVICE_PAYLOAD_KEY = 'service';
7371

7472
/**
7573
* @var string
@@ -396,8 +394,7 @@ public function buildEventProperties($eventProperties, $time, $metric)
396394
self::DATA_KEY_PAYLOAD => array(
397395
self::TYPE_KEY_PAYLOAD => self::METRIC_KEY_PAYLOAD,
398396
self::ATTRIBUTE_KEY_PAYLOAD => array(
399-
self::NAME_KEY_PAYLOAD => $metric,
400-
self::SERVICE_PAYLOAD_KEY => self::MAGENTO_TWO_INTEGRATION_SERVICE_KEY
397+
self::NAME_KEY_PAYLOAD => $metric
401398
)
402399
)
403400
)

Observer/SalesQuoteSaveAfter.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class SalesQuoteSaveAfter implements ObserverInterface
1414
{
1515
// Character limit for a TEXT datatype field
1616
const PAYLOAD_CHARACTER_LIMIT = 65535;
17+
const INTEGRATION_KEY = 'magento_two';
1718

1819
/**
1920
* Klaviyo Scope setting Helper
@@ -100,8 +101,10 @@ public function execute(Observer $observer)
100101
// Setting StoreId in payload
101102
$klAddedToCartPayload['StoreId'] = $quote->getStoreId();
102103

103-
// Setting external_catalog_id in payload - this connects the event to the right localized product in the klaviyo catalog and should not be changed
104-
$klAddedToCartPayload['external_catalog_id'] = $quote->getStore()->getWebsiteId() . '-' . $quote->getStoreId();
104+
// Setting external_catalog_id in payload - this connects the event to the right localized product in the klaviyo catalog.
105+
$klAddedToCartPayload['external_catalog_id'] = $this->_dataHelper->getExternalCatalogIdForEvent($quote->getStore()->getWebsiteId(), $quote->getStoreId());
106+
107+
$klAddedToCartPayload['integration_key'] = self::INTEGRATION_KEY;
105108

106109
$stringifiedPayload = json_encode($klAddedToCartPayload);
107110

0 commit comments

Comments
 (0)