Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Api/NewCustomerResolverInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Tagging\GTM\Api;

use Magento\Sales\Api\Data\OrderInterface;

interface NewCustomerResolverInterface
{
/**
* Determine whether the given order belongs to a new customer for the
* purpose of the `new_customer` field in the GTM data layer and webhook.
*
* Merchants can override this via a DI preference to replace the default
* (guest = new customer) heuristic with e.g. a prior-order-count check.
*
* @param OrderInterface $order
* @return bool
*/
public function isNewCustomer(OrderInterface $order): bool;
}
8 changes: 6 additions & 2 deletions DataLayer/Event/Purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Magento\Sales\Api\Data\OrderInterface;
use Tagging\GTM\Api\Data\EventInterface;
use Tagging\GTM\Api\NewCustomerResolverInterface;
use Tagging\GTM\Config\Config;
use Tagging\GTM\DataLayer\Tag\Order\OrderItems;
use Tagging\GTM\Util\PriceFormatter;
Expand All @@ -16,15 +17,18 @@ class Purchase implements EventInterface
private OrderItems $orderItems;
private Config $config;
private PriceFormatter $priceFormatter;
private NewCustomerResolverInterface $newCustomerResolver;

public function __construct(
OrderItems $orderItems,
Config $config,
PriceFormatter $priceFormatter
PriceFormatter $priceFormatter,
NewCustomerResolverInterface $newCustomerResolver
) {
$this->orderItems = $orderItems;
$this->config = $config;
$this->priceFormatter = $priceFormatter;
$this->newCustomerResolver = $newCustomerResolver;
}

/**
Expand Down Expand Up @@ -81,7 +85,7 @@ private function getUserData(OrderInterface $order): array
'email' => $order->getCustomerEmail() ?? '',
'first_name' => $order->getCustomerFirstname() ?? '',
'last_name' => $order->getCustomerLastname() ?? '',
'new_customer' => $order->getCustomerIsGuest() ? 'true' : 'false'
'new_customer' => $this->newCustomerResolver->isNewCustomer($order) ? 'true' : 'false'
];
}

Expand Down
8 changes: 6 additions & 2 deletions DataLayer/Event/PurchaseWebhookEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Magento\Framework\HTTP\ClientFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Tagging\GTM\Api\NewCustomerResolverInterface;
use Tagging\GTM\DataLayer\Tag\Order\OrderItems;
use Magento\Sales\Api\Data\OrderInterface;
use Tagging\GTM\Util\PriceFormatter;
Expand All @@ -22,6 +23,7 @@ class PurchaseWebhookEvent
private $priceFormatter;
private LoggerInterface $logger;
private Debugger $debugger;
private NewCustomerResolverInterface $newCustomerResolver;

public function __construct(
Json $json,
Expand All @@ -30,7 +32,8 @@ public function __construct(
Config $config,
PriceFormatter $priceFormatter,
LoggerInterface $logger,
Debugger $debugger
Debugger $debugger,
NewCustomerResolverInterface $newCustomerResolver
) {
$this->json = $json;
$this->clientFactory = $clientFactory;
Expand All @@ -39,6 +42,7 @@ public function __construct(
$this->priceFormatter = $priceFormatter;
$this->logger = $logger;
$this->debugger = $debugger;
$this->newCustomerResolver = $newCustomerResolver;
}

public function purchase(OrderInterface $order)
Expand Down Expand Up @@ -122,7 +126,7 @@ public function purchase(OrderInterface $order)
"email" => $order->getCustomerEmail() ?? '',
"first_name" => $order->getCustomerFirstname() ?? '',
"last_name" => $order->getCustomerLastname() ?? '',
"new_customer" => (string)($order->getCustomerIsGuest() ? "true" : "false")
"new_customer" => (string)($this->newCustomerResolver->isNewCustomer($order) ? "true" : "false")
];
} catch (\Exception $e) {
$this->debugger->debug($e->getMessage());
Expand Down
8 changes: 6 additions & 2 deletions DataLayer/Tag/Order/UserData.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Tagging\GTM\Api\Data\TagInterface;
use Tagging\GTM\Api\NewCustomerResolverInterface;

class UserData implements TagInterface
{
private CheckoutSession $checkoutSession;
private OrderRepositoryInterface $orderRepository;
private NewCustomerResolverInterface $newCustomerResolver;

public function __construct(
CheckoutSession $checkoutSession,
OrderRepositoryInterface $orderRepository
OrderRepositoryInterface $orderRepository,
NewCustomerResolverInterface $newCustomerResolver
) {
$this->checkoutSession = $checkoutSession;
$this->orderRepository = $orderRepository;
$this->newCustomerResolver = $newCustomerResolver;
}

/**
Expand Down Expand Up @@ -54,7 +58,7 @@ public function get(): array
'email' => $order->getCustomerEmail() ?? '',
'first_name' => $order->getCustomerFirstname() ?? '',
'last_name' => $order->getCustomerLastname() ?? '',
'new_customer' => $order->getCustomerIsGuest() ? 'true' : 'false'
'new_customer' => $this->newCustomerResolver->isNewCustomer($order) ? 'true' : 'false'
];
}

Expand Down
20 changes: 20 additions & 0 deletions Model/NewCustomerResolver/IsGuestResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Tagging\GTM\Model\NewCustomerResolver;

use Magento\Sales\Api\Data\OrderInterface;
use Tagging\GTM\Api\NewCustomerResolverInterface;

/**
* Default resolver: treats guest orders as new customers.
*
* Preserves the historical behavior of the module. Override via a DI
* preference in your own module to implement a different heuristic.
*/
class IsGuestResolver implements NewCustomerResolverInterface
{
public function isNewCustomer(OrderInterface $order): bool
{
return (bool)$order->getCustomerIsGuest();
}
}
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Tagging\GTM\Api\CheckoutSessionDataProviderInterface" type="Tagging\GTM\SessionDataProvider\CheckoutSessionDataProvider"/>
<preference for="Tagging\GTM\Api\CustomerSessionDataProviderInterface" type="Tagging\GTM\SessionDataProvider\CustomerSessionDataProvider"/>
<preference for="Tagging\GTM\Api\NewCustomerResolverInterface" type="Tagging\GTM\Model\NewCustomerResolver\IsGuestResolver"/>

<type name="Tagging\GTM\Logger\Debugger">
<arguments>
Expand Down
Loading