Skip to content
Open
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
10 changes: 10 additions & 0 deletions config/features/channels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unneeded newline

Copy link
Copy Markdown
Owner

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)

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 }
1 change: 1 addition & 0 deletions config/features/context.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ services:
- "@sylius.context.channel"
- "@sylius.context.locale"
- "@sylius.context.currency"
- "@sylius.google_tag_manager.resolver.channel_feature"
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
1 change: 1 addition & 0 deletions config/features/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ services:
- "%gtm.features.environment%"
- "@google_tag_manager"
- "%kernel.environment%"
- "@sylius.google_tag_manager.resolver.channel_feature"
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
1 change: 1 addition & 0 deletions config/features/route.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ services:
arguments:
- "%gtm.features.route%"
- "@google_tag_manager"
- "@sylius.google_tag_manager.resolver.channel_feature"
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
16 changes: 16 additions & 0 deletions config/services.yaml
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 }
23 changes: 23 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ public function getConfigTreeBuilder(): TreeBuilder
->booleanNode('events')->defaultTrue()->end()
->end()
->end()
->arrayNode('channels')
->useAttributeAsKey('code')
->arrayPrototype()
->validate()
->ifTrue(static fn (array $v): bool => ($v['id'] ?? null) === null &&
($v['enabled'] ?? null) === null &&
($v['features'] ?? []) === [])
->thenInvalid('Channel entry must define at least one of "id", "enabled" or a "features" override.')
->end()
->children()
->scalarNode('id')->defaultNull()->end()
->booleanNode('enabled')->defaultNull()->end()
->arrayNode('features')
->children()
->booleanNode('environment')->end()
->booleanNode('route')->end()
->booleanNode('context')->end()
->booleanNode('events')->end()
->end()
->end()
->end()
->end()
->end()
->end()
;

Expand Down
21 changes: 21 additions & 0 deletions src/DependencyInjection/GtmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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')) {
Expand Down
8 changes: 7 additions & 1 deletion src/EventListener/AddRouteListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GtmPlugin\EventListener;

use GtmPlugin\Resolver\ChannelFeatureResolver;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Xynnn\GoogleTagManagerBundle\Service\GoogleTagManagerInterface;

Expand All @@ -12,12 +13,17 @@ final class AddRouteListener
public function __construct(
private readonly bool $enabled,
private readonly GoogleTagManagerInterface $googleTagManager,
private readonly ?ChannelFeatureResolver $featureResolver = null,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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;
}

Expand Down
50 changes: 50 additions & 0 deletions src/EventListener/ChannelGtmListener.php
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();
}
}
}
8 changes: 7 additions & 1 deletion src/EventListener/ContextListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GtmPlugin\EventListener;

use GtmPlugin\Resolver\ChannelFeatureResolver;
use Sylius\Component\Channel\Context\ChannelContextInterface;
use Sylius\Component\Channel\Context\ChannelNotFoundException;
use Sylius\Component\Currency\Context\CurrencyContextInterface;
Expand All @@ -19,12 +20,17 @@ public function __construct(
private readonly ChannelContextInterface $channelContext,
private readonly LocaleContextInterface $localeContext,
private readonly CurrencyContextInterface $currencyContext,
private readonly ?ChannelFeatureResolver $featureResolver = null,
) {
if ($this->featureResolver === null) {
trigger_error('Not passing a ChannelFeatureResolver to ContextListener 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('context') ?? $this->enabled;
if (!$enabled) {
return;
}

Expand Down
8 changes: 7 additions & 1 deletion src/EventListener/EnvironmentListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace GtmPlugin\EventListener;

use GtmPlugin\Resolver\ChannelFeatureResolver;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Xynnn\GoogleTagManagerBundle\Service\GoogleTagManagerInterface;

Expand All @@ -13,12 +14,17 @@ public function __construct(
private readonly bool $enabled,
private readonly GoogleTagManagerInterface $googleTagManager,
private readonly string $environment,
private readonly ?ChannelFeatureResolver $featureResolver = null,
) {
if ($this->featureResolver === null) {
trigger_error('Not passing a ChannelFeatureResolver to EnvironmentListener 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('environment') ?? $this->enabled;
if (!$enabled) {
return;
}

Expand Down
33 changes: 33 additions & 0 deletions src/Resolver/ChannelFeatureResolver.php
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();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 try, and then only have the global return as exceptional state.


return $this->channels[$code]['features'][$feature] ?? $this->globalFeatures[$feature] ?? false;
} catch (ChannelNotFoundException) {
return $this->globalFeatures[$feature] ?? false;
}
}
}
29 changes: 29 additions & 0 deletions src/Twig/GtmChannelExtension.php
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);
}
}
2 changes: 2 additions & 0 deletions templates/events_javascript.html.twig
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 %}
5 changes: 5 additions & 0 deletions tests/Application/config/packages/stefandoorn_sylius_gtm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ gtm:
route: true
context: true
events: true
channels:
FASHION_WEB:
id: "GTM-FASHION"
BUSINESS_WEB:
enabled: false
Loading