-
Notifications
You must be signed in to change notification settings - Fork 15
Channel-aware GTM container resolution (draft, addresses #151) #152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| services: | ||
|
|
||
| sylius.google_tag_manager.listener.channels: | ||
| class: GtmPlugin\EventListener\ChannelGtmListener | ||
| arguments: | ||
| - "@google_tag_manager" | ||
| - "@sylius.context.channel" | ||
| - "%gtm.channels%" | ||
| tags: | ||
| - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,18 @@ | ||
| imports: | ||
| - { resource: features/*.yaml } | ||
|
|
||
| services: | ||
|
|
||
| sylius.google_tag_manager.resolver.channel_feature: | ||
| class: GtmPlugin\Resolver\ChannelFeatureResolver | ||
| arguments: | ||
| - "@sylius.context.channel" | ||
| - "%gtm.features%" | ||
| - "%gtm.channels%" | ||
|
|
||
| sylius.google_tag_manager.twig.channel: | ||
| class: GtmPlugin\Twig\GtmChannelExtension | ||
| arguments: | ||
| - "@sylius.google_tag_manager.resolver.channel_feature" | ||
| tags: | ||
| - { name: twig.extension } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,9 @@ public function load(array $configs, ContainerBuilder $container): void | |
| $container->setParameter($parameter, $setting); | ||
| } | ||
|
|
||
| $container->setParameter('gtm.features', $config['features']); | ||
| $container->setParameter('gtm.channels', $this->normaliseChannels($config['channels'] ?? [])); | ||
|
|
||
| $loader->load('services.yaml'); | ||
| } | ||
|
|
||
|
|
@@ -32,6 +35,24 @@ public function prepend(ContainerBuilder $container): void | |
| $this->prependSyliusTwigHooks($container); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, array{id?: ?string, enabled?: ?bool, features?: array<string, bool>}> $channels | ||
| * | ||
| * @return array<string, array{id: ?string, enabled: ?bool, features: array<string, bool>}> | ||
| */ | ||
| private function normaliseChannels(array $channels): array | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this can be written different, still performant, in a way more readable way so for future maintenance it's easier to grasp. Rule of thumb is that if I need to look at it longer than 3 seconds to see what all these ?? etc. do, it's too complex ;-) |
||
| { | ||
| foreach ($channels as $code => $channel) { | ||
| $channels[$code] = [ | ||
| 'id' => $channel['id'] ?? null, | ||
| 'enabled' => $channel['enabled'] ?? (($channel['id'] ?? null) !== null ? true : null), | ||
| 'features' => $channel['features'] ?? [], | ||
| ]; | ||
| } | ||
|
|
||
| return $channels; | ||
| } | ||
|
|
||
| protected function prependSyliusTwigHooks(ContainerBuilder $container): void | ||
| { | ||
| if (!$container->hasExtension('sylius_twig_hooks')) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| namespace GtmPlugin\EventListener; | ||
|
|
||
| use GtmPlugin\Resolver\ChannelFeatureResolver; | ||
| use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
| use Xynnn\GoogleTagManagerBundle\Service\GoogleTagManagerInterface; | ||
|
|
||
|
|
@@ -12,12 +13,17 @@ final class AddRouteListener | |
| public function __construct( | ||
| private readonly bool $enabled, | ||
| private readonly GoogleTagManagerInterface $googleTagManager, | ||
| private readonly ?ChannelFeatureResolver $featureResolver = null, | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a deprecation immediately that on the next major version this will be made required?
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (applies to multiple locations & then the bool $enabled will also change probably) |
||
| ) { | ||
| if ($this->featureResolver === null) { | ||
| trigger_error('Not passing a ChannelFeatureResolver to AddRouteListener is deprecated and it will be required in the next major version.', \E_USER_DEPRECATED); | ||
| } | ||
| } | ||
|
|
||
| public function onKernelRequest(RequestEvent $event): void | ||
| { | ||
| if (!$this->enabled) { | ||
| $enabled = $this->featureResolver?->isEnabled('route') ?? $this->enabled; | ||
| if (!$enabled) { | ||
| return; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace GtmPlugin\EventListener; | ||
|
|
||
| use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
| use Sylius\Component\Channel\Context\ChannelNotFoundException; | ||
| use Symfony\Component\HttpKernel\Event\RequestEvent; | ||
| use Xynnn\GoogleTagManagerBundle\Service\GoogleTagManagerInterface; | ||
|
|
||
| final class ChannelGtmListener | ||
| { | ||
| /** | ||
| * @param array<string, array{id: ?string, enabled: ?bool}> $channels | ||
| * Pre-normalised by GtmExtension::load(). | ||
| */ | ||
| public function __construct( | ||
| private readonly GoogleTagManagerInterface $googleTagManager, | ||
| private readonly ChannelContextInterface $channelContext, | ||
| private readonly array $channels, | ||
| ) { | ||
| } | ||
|
|
||
| public function onKernelRequest(RequestEvent $event): void | ||
| { | ||
| if (!$event->isMainRequest()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $code = $this->channelContext->getChannel()->getCode(); | ||
| } catch (ChannelNotFoundException) { | ||
| return; | ||
| } | ||
|
|
||
| $config = $this->channels[$code] ?? null; | ||
| if ($config === null) { | ||
| return; | ||
| } | ||
|
|
||
| if ($config['id'] !== null) { | ||
| $this->googleTagManager->setId($config['id']); | ||
| } | ||
|
|
||
| if ($config['enabled'] !== null) { | ||
| $config['enabled'] ? $this->googleTagManager->enable() : $this->googleTagManager->disable(); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace GtmPlugin\Resolver; | ||
|
|
||
| use Sylius\Component\Channel\Context\ChannelContextInterface; | ||
| use Sylius\Component\Channel\Context\ChannelNotFoundException; | ||
|
|
||
| final class ChannelFeatureResolver | ||
| { | ||
| /** | ||
| * @param array<string, bool> $globalFeatures | ||
| * @param array<string, array{id: ?string, enabled: ?bool, features: array<string, bool>}> $channels | ||
| */ | ||
| public function __construct( | ||
| private readonly ChannelContextInterface $channelContext, | ||
| private readonly array $globalFeatures, | ||
| private readonly array $channels, | ||
| ) { | ||
| } | ||
|
|
||
| public function isEnabled(string $feature): bool | ||
| { | ||
| try { | ||
| $code = $this->channelContext->getChannel()->getCode(); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we return here if the code can be found? So within the |
||
|
|
||
| return $this->channels[$code]['features'][$feature] ?? $this->globalFeatures[$feature] ?? false; | ||
| } catch (ChannelNotFoundException) { | ||
| return $this->globalFeatures[$feature] ?? false; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace GtmPlugin\Twig; | ||
|
|
||
| use GtmPlugin\Resolver\ChannelFeatureResolver; | ||
| use Twig\Extension\AbstractExtension; | ||
| use Twig\TwigFunction; | ||
|
|
||
| final class GtmChannelExtension extends AbstractExtension | ||
| { | ||
| public function __construct( | ||
| private readonly ChannelFeatureResolver $featureResolver, | ||
| ) { | ||
| } | ||
|
|
||
| public function getFunctions(): array | ||
| { | ||
| return [ | ||
| new TwigFunction('gtm_channel_allows', $this->gtmChannelAllows(...)), | ||
| ]; | ||
| } | ||
|
|
||
| public function gtmChannelAllows(string $feature): bool | ||
| { | ||
| return $this->featureResolver->isEnabled($feature); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| {% if gtm_channel_allows('events') %} | ||
| <script src="{{ asset('bundles/gtmplugin/prototype.events.js') }}" {{ sylius_test_html_attribute('gtm-events-javascript') }}></script> | ||
| {% endif %} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,3 +5,8 @@ gtm: | |
| route: true | ||
| context: true | ||
| events: true | ||
| channels: | ||
| FASHION_WEB: | ||
| id: "GTM-FASHION" | ||
| BUSINESS_WEB: | ||
| enabled: false | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unneeded newline
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(this applies at multiple locations)