<?php
namespace App\Utils\Uniqskills\MixPanel;
use App\Entity\Gos\OrderPart;
use App\Entity\Gos\OrderProductVariant;
use App\Entity\Gos\Orders;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\User\UserInterface;
class MixPanelService
{
private ?Request $request;
public function __construct(RequestStack $requestStack)
{
$this->request = $requestStack->getCurrentRequest();
}
public function sendSearchQueryEvent(string $query, UserInterface $user = null): void
{
$mp = $this->initialize();
$this->setPeople($mp, $user);
$this->sendEvent($mp, MixPanelEvent::SEARCH, [
'query' => $query,
'distinct_id' => $this->setDistinctId($user),
]);
}
public function sendCartRenderEvent(array $parameters, UserInterface $user = null): void
{
$mp = $this->initialize();
$this->setPeople($mp, $user);
$this->sendEvent($mp, MixPanelEvent::CART, array_merge($parameters, [
'distinct_id' => $this->setDistinctId($user)
]));
}
public function initialize(): \Mixpanel
{
return \Mixpanel::getInstance('64a0f4f4e3e2f61d17fa2dbcfa5cb8e2', [
'host' => 'api-eu.mixpanel.com',
]);
}
public function sendEvent(\Mixpanel $mp, string $event, array $parameters = []): void
{
$mp->track(
$event,
array_merge($parameters)
);
}
public function setPeople(\Mixpanel $mp, UserInterface $user = null): void
{
if ($user instanceof UserInterface)
{
$username = $user->getUsername();
$mp->people->set($username, [
'$email' => $username,
'$name' => strtok($username, '@'),
], $ip = 0, $ignore_time = true);
}
else
{
$session = $this->request->getSession();
$mp->people->set($session->getId(), [
'tempUser' => $session->get('tempUser'),
'registerFrom' => $session->get('registerFrom'),
'myCountryCode' => $session->get('myCountryCode'),
]);
}
}
public function setDistinctId(UserInterface $user = null): string
{
if ($user !== null)
{
return $user->getUsername();
}
return $this->request->getSession()->getId();
}
public function prepareOrderData(Orders $order): array
{
$orderParts = [];
$orderProductVariants = [];
/** @var OrderPart $orderPart */
foreach ($order->getOrderPart() as $orderPart)
{
/** @var OrderProductVariant $orderProductVariant */
foreach ($orderPart->getOrderProductVariant() as $orderProductVariant)
{
array_push($orderProductVariants, [
'productVariantNoComplete' => $orderProductVariant->getProductVariantNoComplete(),
'title' => $orderProductVariant->getTitle(),
'priceNet' => $orderProductVariant->getPriceNet(),
'priceGross' => $orderProductVariant->getPriceGross(),
'quantity' => $orderProductVariant->getQuantity(),
]);
}
array_push($orderParts, [
'ordTran' => $orderPart->getOrdTran(),
'totalPriceNet' => $orderPart->getTotalPriceNet(),
'totalPriceGross' => $orderPart->getTotalPriceGross(),
'orderProductVariants' => $orderProductVariants,
]);
}
return [
'ordTran' => $order->getOrdTran(),
'totalPriceNet' => $order->getTotalPriceNet(),
'totalPriceGross' => $order->getTotalPriceGross(),
'actionNumber' => $order->getActionNumber(),
'fromSource' => $order->getFromSource(),
'paymentSystem' => $order->getPaymentSystem() ? $order->getPaymentSystem()->getName() : null,
'orderParts' => $orderParts,
];
}
}