* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ namespace PrestaShop\Module\Ps_facebook\Tracker; use Context; use Exception; use PrestaShop\Module\PrestashopFacebook\Adapter\ConfigurationAdapter; use PrestaShop\Module\PrestashopFacebook\Config\Config; use PrestaShop\Module\PrestashopFacebook\Config\Env; use PrestaShop\PsAccountsInstaller\Installer\Facade\PsAccounts; class Segment implements TrackerInterface { /** * @var string */ private $message = ''; /** * @var array */ private $options = []; /** * @var Context */ private $context; /** * @var Env */ private $env; /** * @var ConfigurationAdapter */ private $configurationAdapter; /** * @var string */ private $userId; public function __construct(Context $context, Env $env, ConfigurationAdapter $configurationAdapter, PsAccounts $psAccountsFacade) { $this->context = $context; $this->env = $env; $this->init(); $this->configurationAdapter = $configurationAdapter; try { $this->userId = $psAccountsFacade->getPsAccountsService()->getShopUuidV4(); } catch (Exception $e) { } } /** * Init segment client with the api key */ private function init() { \Segment::init($this->env->get('PSX_FACEBOOK_SEGMENT_API_KEY')); } /** * Track event on segment * * @return bool * * @throws \PrestaShopException */ public function track() { if (empty($this->message)) { throw new \PrestaShopException('Message cannot be empty. Need to set it with setMessage() method.'); } // Dispatch track depending on context shop $this->dispatchTrack(); return true; } private function segmentTrack($domainName) { if (empty($this->userId)) { return; } $userAgent = array_key_exists('HTTP_USER_AGENT', $_SERVER) === true ? $_SERVER['HTTP_USER_AGENT'] : ''; $ip = array_key_exists('REMOTE_ADDR', $_SERVER) === true ? $_SERVER['REMOTE_ADDR'] : ''; $referer = array_key_exists('HTTP_REFERER', $_SERVER) === true ? $_SERVER['HTTP_REFERER'] : ''; $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $externalBusinessId = $this->configurationAdapter->get(Config::PS_FACEBOOK_EXTERNAL_BUSINESS_ID); \Segment::track([ 'userId' => $this->userId, 'event' => $this->message, 'channel' => 'browser', 'context' => [ 'ip' => $ip, 'userAgent' => $userAgent, 'locale' => $this->context->language->iso_code, 'page' => [ 'referrer' => $referer, 'url' => $url, ], 'externalBusinessId' => $externalBusinessId, 'name' => $domainName, ], 'properties' => array_merge([ 'module' => 'ps_facebook', ], $this->options), ]); \Segment::flush(); } /** * Handle tracking differently depending on the shop context * * @return mixed */ private function dispatchTrack() { $dictionary = [ \Shop::CONTEXT_SHOP => function () { return self::trackShop(); }, \Shop::CONTEXT_GROUP => function () { return self::trackShopGroup(); }, \Shop::CONTEXT_ALL => function () { return self::trackAllShops(); }, ]; return call_user_func($dictionary[$this->context->shop->getContext()]); } /** * Send track segment only for the current shop */ private function trackShop() { $userId = $this->context->shop->domain; $this->segmentTrack($userId); } /** * Send track segment for each shop in the current shop group */ private function trackShopGroup() { $shops = $this->context->shop->getShops(true, $this->context->shop->getContextShopGroupID()); foreach ($shops as $shop) { $this->segmentTrack($shop['domain']); } } /** * Send track segment for all shops */ private function trackAllShops() { $shops = $this->context->shop->getShops(); foreach ($shops as $shop) { $this->segmentTrack($shop['domain']); } } /** * @return string */ public function getMessage() { return $this->message; } /** * @param string $message */ public function setMessage($message) { $this->message = $message; } /** * @return array */ public function getOptions() { return $this->options; } /** * @param array $options */ public function setOptions($options) { $this->options = $options; } }