Skip to content

Repository files navigation

Sylius plugin integrating Plausible Analytics

Latest Stable Version Total Downloads License PHP Version Require build codecov Mutation testing badge

Use Plausible Analytics to track visitors and events in your Sylius store.

Installation

Step 1: Install and enable the plugin

composer require setono/sylius-plausible-plugin

Step 2: Register the tag bag bundle

The plugin outputs its JavaScript through setono/tag-bag-bundle. If the bundle isn't registered already (Symfony Flex does this for you), add it to config/bundles.php:

# config/bundles.php
return [
    // ...
    Setono\TagBagBundle\SetonoTagBagBundle::class => ['all' => true],
];

Step 3: Render the tag bag in your shop layout

Nothing is rendered until you call the tag bag's Twig functions. Add them to your shop layout:

{# templates/bundles/SyliusShopBundle/layout.html.twig #}
<head>
    {# ... #}
    {{ setono_tag_bag_render_head() }}
</head>
<body>
{{ setono_tag_bag_render_body_begin() }}
    {# ... #}
{{ setono_tag_bag_render_body_end() }}
{{ setono_tag_bag_render_all() }}
</body>

Important

If you skip this step the plugin will appear to work, but no tracking code is ever written to the page.

Step 4: Add the Plausible script identifier trait to your Channel entity

<?php

declare(strict_types=1);

namespace App\Entity\Channel;

use Doctrine\ORM\Mapping as ORM;
use Setono\SyliusPlausiblePlugin\Model\ChannelInterface as PlausibleChannelInterface;
use Setono\SyliusPlausiblePlugin\Model\ChannelTrait as PlausibleChannelTrait;
use Sylius\Component\Core\Model\Channel as BaseChannel;

#[ORM\Entity]
#[ORM\Table(name: 'sylius_channel')]
class Channel extends BaseChannel implements PlausibleChannelInterface
{
    use PlausibleChannelTrait;
}

Make sure the channel resource points at your own class:

# config/packages/sylius_channel.yaml
sylius_channel:
    resources:
        channel:
            classes:
                model: App\Entity\Channel\Channel

Step 5: Import routes

# config/routes/setono_sylius_plausible.yaml
setono_sylius_plausible:
    resource: "@SetonoSyliusPlausiblePlugin/Resources/config/routes.yaml"

Step 6: Update your database schema

bin/console doctrine:migrations:diff
bin/console doctrine:migrations:migrate

Usage

Configure Plausible per channel

Navigate to Marketing > Plausible in the admin panel to configure the Plausible script for each channel.

You can enter the Plausible script in any of the following formats:

  • Identifier only: pa-hb0WlWkUb5U3qhSS-vd-a
  • Full URL: https://plausible.io/js/pa-hb0WlWkUb5U3qhSS-vd-a.js
  • HTML snippet: <script async src="https://plausible.io/js/pa-hb0WlWkUb5U3qhSS-vd-a.js"></script>

The plugin will normalize any of these formats and output the correct script tag on your storefront.

Disabling tracking

Tracking is enabled by default. Turn it off in environments that shouldn't report to Plausible, for instance staging:

# config/packages/setono_sylius_plausible.yaml
setono_sylius_plausible:
    enabled: false

While disabled, none of the tracking services are registered at all, so no listener runs and no work is done. The admin UI stays available, so you can still configure your channels.

Self-hosted Plausible

If you run Plausible Community Edition, point the plugin at your own host:

# config/packages/setono_sylius_plausible.yaml
setono_sylius_plausible:
    script_host: 'https://analytics.example.com'

The script is then loaded from https://analytics.example.com/js/<identifier>.js. Defaults to https://plausible.io.

About

Use Plausible Analytics to track visitors and events in your Sylius store

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages