From 0bcfee20518223db07cac90cb926f99e94367831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joachim=20L=C3=B8vgaard?= Date: Tue, 27 Aug 2024 09:37:01 +0200 Subject: [PATCH] Change tracking strategy --- src/DependencyInjection/Configuration.php | 2 +- src/Event/LibraryTagCreatedEvent.php | 19 ++++++++++ .../ClientSide/LibrarySubscriber.php | 9 ++++- src/EventSubscriber/PageviewSubscriber.php | 38 ------------------- .../services/conditional/client_side.xml | 1 + .../config/services/event_subscriber.xml | 7 ---- .../packages/setono_sylius_plausible.yaml | 2 +- 7 files changed, 30 insertions(+), 48 deletions(-) create mode 100644 src/Event/LibraryTagCreatedEvent.php delete mode 100644 src/EventSubscriber/PageviewSubscriber.php diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index fa9feb5..39f97c0 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -27,7 +27,7 @@ public function getConfigTreeBuilder(): TreeBuilder ->children() ->scalarNode('script') ->info('The Plausible script to use. See available scripts here: https://plausible.io/docs/script-extensions') - ->defaultValue('https://plausible.io/js/script.manual.revenue.js') + ->defaultValue('https://plausible.io/js/script.pageview-props.revenue.js') ->cannotBeEmpty() ->end() ->end() diff --git a/src/Event/LibraryTagCreatedEvent.php b/src/Event/LibraryTagCreatedEvent.php new file mode 100644 index 0000000..9c6bd09 --- /dev/null +++ b/src/Event/LibraryTagCreatedEvent.php @@ -0,0 +1,19 @@ +domain ?? u($event->getRequest()->getHost())->trimPrefix('www.')->toString(); - $this->tagBag->add( + $tagEvent = new LibraryTagCreatedEvent( ScriptTag::create($this->script) ->defer() ->withSection(TagInterface::SECTION_HEAD) ->withAttribute('data-domain', $domain) ->withFingerprint(self::TAG_FINGERPRINT), ); + + $this->eventDispatcher->dispatch($tagEvent); + + $this->tagBag->add($tagEvent->tag); } } diff --git a/src/EventSubscriber/PageviewSubscriber.php b/src/EventSubscriber/PageviewSubscriber.php deleted file mode 100644 index b94d0aa..0000000 --- a/src/EventSubscriber/PageviewSubscriber.php +++ /dev/null @@ -1,38 +0,0 @@ - 'track', - ]; - } - - public function track(RequestEvent $event): void - { - if (!$event->isMainRequest() || $event->getRequest()->isXmlHttpRequest()) { - return; - } - - $accept = $event->getRequest()->headers->get('Accept'); - if (!is_string($accept) || !str_contains($accept, 'html')) { - return; - } - - try { - $this->eventBus->dispatch(new Event(Events::PAGEVIEW)); - } catch (\Throwable $e) { - $this->log(Events::PAGEVIEW, $e); - } - } -} diff --git a/src/Resources/config/services/conditional/client_side.xml b/src/Resources/config/services/conditional/client_side.xml index c8309b6..f5f4f8e 100644 --- a/src/Resources/config/services/conditional/client_side.xml +++ b/src/Resources/config/services/conditional/client_side.xml @@ -6,6 +6,7 @@ + %setono_sylius_plausible.client_side.script% %setono_sylius_plausible.domain% diff --git a/src/Resources/config/services/event_subscriber.xml b/src/Resources/config/services/event_subscriber.xml index 752e8b4..cec4e86 100644 --- a/src/Resources/config/services/event_subscriber.xml +++ b/src/Resources/config/services/event_subscriber.xml @@ -16,13 +16,6 @@ - - - - - - diff --git a/tests/Application/config/packages/setono_sylius_plausible.yaml b/tests/Application/config/packages/setono_sylius_plausible.yaml index 34a5fc0..a49968a 100644 --- a/tests/Application/config/packages/setono_sylius_plausible.yaml +++ b/tests/Application/config/packages/setono_sylius_plausible.yaml @@ -1,4 +1,4 @@ setono_sylius_plausible: client_side: - script: 'https://plausible.io/js/script.local.manual.revenue.js' + script: 'https://plausible.io/js/script.local.pageview-props.revenue.js' domain: 'setono.com'