generated from Setono/SyliusPluginSkeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from Setono/adding-events
Add events and event subscribers related to the conversion processor
- Loading branch information
Showing
9 changed files
with
161 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\Event; | ||
|
||
use Setono\SyliusGoogleAdsPlugin\Model\ConversionInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
final class PreSetClickConversionDataEvent extends Event | ||
{ | ||
public function __construct( | ||
public readonly ConversionInterface $conversion, | ||
public array $data, | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\Event; | ||
|
||
use Google\Ads\GoogleAds\V13\Common\UserIdentifier; | ||
use Setono\SyliusGoogleAdsPlugin\Model\ConversionInterface; | ||
use Symfony\Contracts\EventDispatcher\Event; | ||
|
||
final class PreSetClickConversionUserIdentifiersEvent extends Event | ||
{ | ||
/** @var list<UserIdentifier> */ | ||
public array $userIdentifiers = []; | ||
|
||
public function __construct( | ||
public readonly ConversionInterface $conversion, | ||
) { | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/EventSubscriber/ConversionProcessing/AddEmailUserIdentifierSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\EventSubscriber\ConversionProcessing; | ||
|
||
use Google\Ads\GoogleAds\V13\Common\UserIdentifier; | ||
use Google\Ads\GoogleAds\V13\Enums\UserIdentifierSourceEnum\UserIdentifierSource; | ||
use Setono\SyliusGoogleAdsPlugin\Event\PreSetClickConversionUserIdentifiersEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class AddEmailUserIdentifierSubscriber implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PreSetClickConversionUserIdentifiersEvent::class => 'add', | ||
]; | ||
} | ||
|
||
public function add(PreSetClickConversionUserIdentifiersEvent $event): void | ||
{ | ||
$email = $event->conversion->getOrder()?->getCustomer()?->getEmailCanonical(); | ||
if (null === $email) { | ||
return; | ||
} | ||
|
||
$email = strtolower(trim($email)); | ||
$emailParts = explode('@', $email); | ||
if (count($emailParts) !== 2) { | ||
return; | ||
} | ||
|
||
// Google Ads requires removal of any '.' characters preceding "gmail.com" or "googlemail.com". | ||
if (preg_match('/^(gmail|googlemail)\.com\s*/', $emailParts[1])) { | ||
$emailParts[0] = str_replace('.', '', $emailParts[0]); | ||
$email = sprintf('%s@%s', $emailParts[0], $emailParts[1]); | ||
} | ||
|
||
$event->userIdentifiers[] = new UserIdentifier([ | ||
'hashed_email' => hash('sha256', $email), | ||
'user_identifier_source' => UserIdentifierSource::FIRST_PARTY, | ||
]); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/EventSubscriber/ConversionProcessing/AddPhoneNumberUserIdentifierSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Setono\SyliusGoogleAdsPlugin\EventSubscriber\ConversionProcessing; | ||
|
||
use Brick\PhoneNumber\PhoneNumber; | ||
use Brick\PhoneNumber\PhoneNumberFormat; | ||
use Google\Ads\GoogleAds\V13\Common\UserIdentifier; | ||
use Google\Ads\GoogleAds\V13\Enums\UserIdentifierSourceEnum\UserIdentifierSource; | ||
use Setono\SyliusGoogleAdsPlugin\Event\PreSetClickConversionUserIdentifiersEvent; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
final class AddPhoneNumberUserIdentifierSubscriber implements EventSubscriberInterface | ||
{ | ||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
PreSetClickConversionUserIdentifiersEvent::class => 'add', | ||
]; | ||
} | ||
|
||
public function add(PreSetClickConversionUserIdentifiersEvent $event): void | ||
{ | ||
$phoneNumber = $event->conversion->getOrder()?->getCustomer()?->getPhoneNumber() ?? $event->conversion->getOrder()?->getBillingAddress()?->getPhoneNumber(); | ||
if (null === $phoneNumber) { | ||
return; | ||
} | ||
|
||
$countryCode = $event->conversion->getOrder()?->getBillingAddress()?->getCountryCode(); | ||
if (null !== $countryCode) { | ||
return; | ||
} | ||
|
||
try { | ||
$phoneNumberParsed = PhoneNumber::parse($phoneNumber, $countryCode); | ||
} catch (\Throwable) { | ||
return; | ||
} | ||
|
||
$event->userIdentifiers[] = new UserIdentifier([ | ||
'hashed_phone_number' => hash('sha256', $phoneNumberParsed->format(PhoneNumberFormat::E164)), | ||
'user_identifier_source' => UserIdentifierSource::FIRST_PARTY, | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters