pixelopen/magento-plausible

此包最新版本(100.3.0)没有提供许可证信息。

将 Plausible 分析添加到 Magento

安装: 1,015

依赖项: 0

建议者: 0

安全: 0

星标: 5

关注者: 3

分支: 0

开放问题: 0

类型:magento2-module

100.3.0 2024-06-10 07:43 UTC

This package is auto-updated.

Last update: 2024-09-10 08:11:54 UTC


README

Minimum PHP Version Minimum Magento Version GitHub release Quality Gate Status

演示

将 Plausible 分析添加到 Magento。

  • 保存访问过的页面
  • 使用 Plausible 共享链接在 Magento 管理员中访问分析
  • 管理特定操作的目标(联系、注册、结账、订单...)
  • 允许使用您自己的 Plausible 实例
  • 与多个网站兼容
  • 与 Magento OpenSource、Mage-OS 和 Adobe Commerce 兼容
  • 与 Hyvä 主题兼容

Plausible Analytics in Magento admin

要求

  • Magento >= 2.4.4
  • PHP >= 8.0.0

安装

composer require pixelopen/magento-plausible

配置

商店 > 配置 > 服务 > Plausible

跟踪

注意:对于带有 Magento CSP 模块的定制域名,请记住在 CSP 白名单中添加您的实例 URL 以支持 frame-srcconnect-srcscript-src

管理员

目标

当模块配置中启用时,模块包含目标事件。

  • 发送联系消息
  • 账户注册
  • 客户已登录
  • 查看购物车
  • 结账
  • 订单完成

您需要在 Plausible 网站配置中添加目标事件

Plausible Goals

Plausible 目标名称必须与模块配置中的名称相同。

默认目标名称为

  • contact
  • register
  • login
  • cart
  • checkout
  • order

收入跟踪

使用“订单”目标时,会发送“收入跟踪”。此功能仅在 Plausible 商业计划中可用。

添加“订单”目标时,您需要启用“收入跟踪”

Plausible Revenue Tracking

自定义目标

在模块中,在 config.xml 文件中声明新的目标

<?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>

从任何地方(如控制器操作),添加一个新的 Plausible 目标

<?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;
    }
}

当达到此控制器并显示页面时,名为 "page_view_goal" 的目标(根据 "my_page_view_action" 的配置)将被发送到 Plausible。

send 方法允许发送目标。目标将一直持续到当前会话中对 send 方法的请求。

如有需要,添加自定义属性(仅限商业计划)

$this->goals->add('my_page_view_action', ['foo' => 'bar'])->send();

要在任何操作后发送全缓存页面的目标,请将操作添加到位于 etc/frontend 目录中的 sections.xml 文件。

<?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>

要始终在完整缓存页面上发送目标,请向本机块 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>

要从自定义 RequireJS 脚本发送目标,请请求重新加载 plausible 部分

define(
    [
        'uiComponent',
        'Magento_Customer/js/customer-data'
    ],
    function (Component, customerData) {
        'use strict';

        return Component.extend({

            initialize: function () {
                this._super();
                
                customerData.reload(['plausible'], false);
            }

        });
    }
);

要简单地使用 JavaScript 发送目标,请使用 plausible 方法

plausible('goalName', {'props': {'foo': 'bar'}});