pixelopen / magento-plausible
Add Plausible Analytics to Magento
Installs: 1 301
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 3
Forks: 2
Open Issues: 1
Type:magento2-module
Requires
- php: ^8
- magento/framework: *
- magento/module-backend: *
- magento/module-checkout: *
- magento/module-config: *
- magento/module-contact: *
- magento/module-customer: *
- magento/module-store: *
Requires (Dev)
README
Presentation
Add Plausible Analytics to Magento.
- Save visited pages
- Access Analytics in Magento Admin with Plausible Shared Link
- Manage goals on specific actions (contact, register, checkout, order...)
- Allow use of your own Plausible instance
- Compatible with multiple websites
- Compatible with Magento OpenSource, Mage-OS and Adobe Commerce
- Compatible with Hyvä theme
Requirements
- Magento >= 2.4.4
- PHP >= 8.0.0
Installation
composer require pixelopen/magento-plausible
Configuration
Stores > Configuration > Services > Plausible
Tracking
Note: For custom domain with Magento CSP Module, remember to add your instance URL in CSP whitelist for frame-src, connect-src and script-src.
Admin
Goals
The module includes goal events when enabled in module configuration.
- Contact message sent
- Account registration
- Customer has logged in
- Cart view
- Checkout
- Order complete
You need to add goal events in your Plausible website configuration:
The Plausible goal name must be the same as the name in the module configuration.
Default goal names are:
- contact
- register
- login
- cart
- checkout
- order
Revenue Tracking
With the goal "order", the "revenue tracking" is sent. This feature is only available with Plausible business plan.
When you add the "order" goal, you need to enable the "Revenue Tracking":
Custom Goal
In a module, declare the new goals in the config.xml
file:
<?xml version="1.0"?> <!-- Vendor/MyModule/etc/config.xml --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd"> <default> <pixel_open_plausible> <goals> <my_page_view_action>page_view_goal</my_page_view_action> <my_form_post_action>form_post_goal</my_form_post_action> </goals> </pixel_open_plausible> </default> </config>
From anywhere (like a controller action), add a new Plausible goal:
<?php /* Vendor/MyModule/Controller/Example/Index.php */ declare(strict_types=1); namespace Vendor\MyModule\Controller\Example; use Magento\Framework\App\Action\HttpGetActionInterface; use PixelOpen\Plausible\Session\Goals; class Index implements HttpGetActionInterface { protected Goals $goals; protected ResultFactory $resultFactory; public function __construct( Goals $goals, ResultFactory $resultFactory ) { $this->goals = $goals; $this->resultFactory = $resultFactory; } public function execute(): ResultFactory { $result = $this->resultFactory->create(ResultFactory::TYPE_LAYOUT); $this->goals->add('my_page_view_action')->send(); return $result; } }
When this controller is reached and the page displayed, the goal named "page_view_goal" (according the config for "my_page_view_action") is sent to Plausible.
The send method allows to send goals. The goal will persist until send method is requested for the current session.
Add custom properties if needed (Business Plan only):
$this->goals->add('my_page_view_action', ['foo' => 'bar'])->send();
To send goals on a full cached page after any action, add the action in a sections.xml
file in etc/frontend
directory.
<?xml version="1.0"?> <!-- Vendor/MyModule/etc/frontend/sections.xml --> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd"> <action name="mymodule/example/post"> <section name="plausible" /> </action> </config>
To always send goal on a full cached page, add a child block to the native block pixel_open_plausible_goals
:
<?xml version="1.0"?> <!-- Vendor/MyModule/view/frontend/layout/my_fpc_page.xml --> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="pixel_open_plausible_goals"> <block name="pixel_open_plausible_goals_fpc" class="PixelOpen\Plausible\Block\Goals" template="PixelOpen_Plausible::luma/goals/fpc.phtml" /> </referenceBlock> </body> </page>
To send goals from a custom RequireJS script, ask to reload the plausible section:
define( [ 'uiComponent', 'Magento_Customer/js/customer-data' ], function (Component, customerData) { 'use strict'; return Component.extend({ initialize: function () { this._super(); customerData.reload(['plausible'], false); } }); } );
To simply send a goal in JavaScript, use the plausible method:
plausible('goalName', {'props': {'foo': 'bar'}});