Skip to content

Commit

Permalink
Merge pull request #5 from Setono/change-default-tracking
Browse files Browse the repository at this point in the history
Change tracking strategy
  • Loading branch information
loevgaard authored Aug 27, 2024
2 parents 116deb4 + 0bcfee2 commit ae71458
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 48 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.manual.revenue.js')
->defaultValue('https://plausible.io/js/script.pageview-props.revenue.js')
->cannotBeEmpty()
->end()
->end()
Expand Down
19 changes: 19 additions & 0 deletions src/Event/LibraryTagCreatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Setono\SyliusPlausiblePlugin\Event;

use Setono\TagBag\Tag\ScriptTag;

/**
* This event is dispatched when the Plausible tracking library tag is created.
* You can listen to this event to alter the tag before it is added to the tag bag.
* Remember that the ScriptTag is a value object, so you need to create a new instance if you want to alter it.
*/
final class LibraryTagCreatedEvent
{
public function __construct(public ScriptTag $tag)
{
}
}
9 changes: 8 additions & 1 deletion src/EventSubscriber/ClientSide/LibrarySubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Setono\SyliusPlausiblePlugin\EventSubscriber\ClientSide;

use Psr\EventDispatcher\EventDispatcherInterface;
use Setono\SyliusPlausiblePlugin\Event\LibraryTagCreatedEvent;
use Setono\TagBag\Tag\InlineScriptTag;
use Setono\TagBag\Tag\ScriptTag;
use Setono\TagBag\Tag\TagInterface;
Expand All @@ -18,6 +20,7 @@ final class LibrarySubscriber implements EventSubscriberInterface
final public const TAG_FINGERPRINT = 'plausible-library';

public function __construct(
private readonly EventDispatcherInterface $eventDispatcher,
private readonly TagBagInterface $tagBag,
private readonly string $script,
private readonly ?string $domain,
Expand Down Expand Up @@ -50,12 +53,16 @@ public function add(RequestEvent $event): void

$domain = $this->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);
}
}
38 changes: 0 additions & 38 deletions src/EventSubscriber/PageviewSubscriber.php

This file was deleted.

1 change: 1 addition & 0 deletions src/Resources/config/services/conditional/client_side.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<service id="setono_sylius_plausible.event_subscriber.client_side.library"
class="Setono\SyliusPlausiblePlugin\EventSubscriber\ClientSide\LibrarySubscriber">
<argument type="service" id="event_dispatcher"/>
<argument type="service" id="setono_tag_bag.tag_bag"/>
<argument>%setono_sylius_plausible.client_side.script%</argument>
<argument>%setono_sylius_plausible.domain%</argument>
Expand Down
7 changes: 0 additions & 7 deletions src/Resources/config/services/event_subscriber.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
<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.manual.revenue.js'
script: 'https://plausible.io/js/script.local.pageview-props.revenue.js'
domain: 'setono.com'

0 comments on commit ae71458

Please sign in to comment.