From 3366c6b3b1f5854d383b18ebb395d348ea5274b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 5 Dec 2023 12:21:36 +0100 Subject: [PATCH] Fixes a bug where multiple tracking ids would be sent to google --- .../ConversionProcessor.php | 5 ++-- src/Model/Conversion.php | 24 +++++++++++++++++++ src/Model/ConversionInterface.php | 12 ++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/ConversionProcessor/ConversionProcessor.php b/src/ConversionProcessor/ConversionProcessor.php index 312a129..95bef77 100644 --- a/src/ConversionProcessor/ConversionProcessor.php +++ b/src/ConversionProcessor/ConversionProcessor.php @@ -71,10 +71,9 @@ public function process(ConversionInterface $conversion): void 'conversion_date_time' => $createdAt->format('Y-m-d H:i:sP'), 'currency_code' => $conversion->getCurrencyCode(), 'order_id' => $order->getId(), - 'gclid' => $conversion->getGclid(), - 'gbraid' => $conversion->getGbraid(), - 'wbraid' => $conversion->getWbraid(), + $conversion->getTrackingIdParameter() => $conversion->getTrackingId(), ])); + $this->eventDispatcher->dispatch($preSetClickConversionDataEvent); $clickConversion = new ClickConversion($preSetClickConversionDataEvent->data); diff --git a/src/Model/Conversion.php b/src/Model/Conversion.php index 2dabedc..d526e71 100644 --- a/src/Model/Conversion.php +++ b/src/Model/Conversion.php @@ -7,6 +7,7 @@ use Sylius\Component\Channel\Model\ChannelInterface; use Sylius\Component\Core\Model\OrderInterface; use Sylius\Component\Resource\Model\TimestampableTrait; +use Webmozart\Assert\Assert; class Conversion implements ConversionInterface { @@ -66,6 +67,29 @@ public function setVersion(?int $version): void $this->version = $version; } + public function getTrackingId(): string + { + $val = $this->{$this->getTrackingIdParameter()}; + Assert::nullOrString($val); + + if (null !== $val) { + return $val; + } + + throw new \RuntimeException('This conversion has no tracking id'); + } + + public function getTrackingIdParameter(): string + { + foreach (['gclid', 'gbraid', 'wbraid'] as $parameter) { + if (null !== $this->{$parameter}) { + return $parameter; + } + } + + throw new \RuntimeException('This conversion has no tracking id'); + } + public function getGclid(): ?string { return $this->gclid; diff --git a/src/Model/ConversionInterface.php b/src/Model/ConversionInterface.php index c52943c..83ca9ec 100644 --- a/src/Model/ConversionInterface.php +++ b/src/Model/ConversionInterface.php @@ -26,6 +26,18 @@ interface ConversionInterface extends ResourceInterface, TimestampableInterface, public function getId(): ?int; + /** + * Returns the value of either gclid, gbraid or wbraid (in that order) + * + * @throws \RuntimeException if none of the tracking ids are set + */ + public function getTrackingId(): string; + + /** + * Returns the name of the tracking id parameter (gclid, gbraid or wbraid) + */ + public function getTrackingIdParameter(): string; + public function getGclid(): ?string; public function setGclid(?string $gclid): void;