Skip to content

Commit

Permalink
Merge pull request #85 from Setono/only-set-one-tracking-parameter
Browse files Browse the repository at this point in the history
Fixes a bug where multiple tracking ids would be sent to google
  • Loading branch information
loevgaard authored Dec 5, 2023
2 parents 279bd11 + 3366c6b commit 152add2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/ConversionProcessor/ConversionProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 24 additions & 0 deletions src/Model/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions src/Model/ConversionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 152add2

Please sign in to comment.