Skip to content

Commit

Permalink
Add the pageview event
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Dec 12, 2023
1 parent 3f6f438 commit a70825e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.revenue.js')
->defaultValue('https://plausible.io/js/script.manual.revenue.js')
->cannotBeEmpty()
->end()
->end()
Expand Down
5 changes: 5 additions & 0 deletions src/Event/Plausible/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ final class Events

final public const BEGIN_CHECKOUT = 'Begin Checkout';

/**
* This is a special event in Plausible, hence the lowercase name
*/
final public const PAGEVIEW = 'pageview';

final public const PURCHASE = 'Purchase';

final public const SELECT_SHIPPING_METHOD = 'Select Shipping Method';
Expand Down
33 changes: 33 additions & 0 deletions src/EventSubscriber/PageviewSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPlausiblePlugin\EventSubscriber;

use Setono\SyliusPlausiblePlugin\Event\Plausible\Event;
use Setono\SyliusPlausiblePlugin\Event\Plausible\Events;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;

final class PageviewSubscriber extends AbstractEventSubscriber
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'track',
];
}

public function track(RequestEvent $requestEvent): void
{
try {
if (!$requestEvent->isMainRequest()) {
return;
}

$this->eventBus->dispatch(new Event(Events::PAGEVIEW));
} catch (\Throwable $e) {
$this->log(Events::PAGEVIEW, $e);
}
}
}
7 changes: 7 additions & 0 deletions src/Resources/config/services/event_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<tag name="kernel.event_subscriber"/>
</service>

<service id="setono_sylius_plausible.event_subscriber.pageview"
class="Setono\SyliusPlausiblePlugin\EventSubscriber\PageviewSubscriber">
<argument type="service" id="setono_sylius_plausible.event_bus"/>

<tag name="kernel.event_subscriber"/>
</service>

<service id="setono_sylius_plausible.event_subscriber.purchase"
class="Setono\SyliusPlausiblePlugin\EventSubscriber\PurchaseSubscriber">
<argument type="service" id="setono_sylius_plausible.event_bus"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
setono_sylius_plausible:
client_side:
script: 'https://plausible.io/js/script.local.revenue.js'
domain: 'wattoo.dk'
script: 'https://plausible.io/js/script.local.manual.revenue.js'
domain: 'setono.com'

0 comments on commit a70825e

Please sign in to comment.