setono / sylius-analytics-plugin
Sylius 的 Google Analytics 插件
Requires
- php: >=7.4
- ext-json: *
- doctrine/collections: ^1.8
- doctrine/doctrine-bundle: ^2.7
- doctrine/orm: ^2.14
- knplabs/knp-menu: ^3.3
- psr/event-dispatcher: ^1.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- setono/composite-compiler-pass: ^1.1
- setono/google-analytics-bundle: ^2.0@alpha
- setono/google-analytics-events: ^1.0@rc
- setono/google-analytics-measurement-protocol: ^2.0@beta
- sylius/channel: ^1.0
- sylius/channel-bundle: ^1.0
- sylius/core: ^1.8
- sylius/grid-bundle: ^1.10
- sylius/locale: ^1.0
- sylius/order: ^1.0
- sylius/product: ^1.0
- sylius/resource-bundle: ^1.8
- sylius/taxonomy: ^1.0
- sylius/ui-bundle: ^1.0
- symfony/config: ^5.4 || ^6.0
- symfony/dependency-injection: ^5.4 || ^6.0
- symfony/event-dispatcher: ^5.4 || ^6.0
- symfony/form: ^5.4 || ^6.0
- symfony/http-foundation: ^5.4 || ^6.0
- symfony/http-kernel: ^5.4 || ^6.0
- symfony/messenger: ^5.4 || ^6.0
- webmozart/assert: ^1.11
Requires (Dev)
- api-platform/core: ^2.7.13
- behat/behat: ^3.13
- icanhazstring/composer-unused: ^0.8.10
- matthiasnoback/symfony-config-test: ^4.3
- matthiasnoback/symfony-dependency-injection-test: ^4.3
- php-http/message-factory: ^1.1
- phpspec/prophecy-phpunit: ^2.0.2
- phpunit/phpunit: ^9.6.10
- psalm/plugin-phpunit: ^0.18
- setono/code-quality-pack: ^2.4
- sylius/sylius: ~1.10.14
- symfony/debug-bundle: ^5.4 || ^6.0
- symfony/dotenv: ^5.4 || ^6.0
- symfony/intl: ^5.4 || ^6.0
- symfony/property-info: ^5.4 || ^6.0
- symfony/serializer: ^5.4 || ^6.0
- symfony/web-profiler-bundle: ^5.4 || ^6.0
- symfony/webpack-encore-bundle: ^1.17.1
- 4.x-dev
- v4.0.0-beta.3
- v4.0.0-beta.2
- v4.0.0-beta
- v4.0.0-alpha.11
- v4.0.0-alpha.10
- v4.0.0-alpha.9
- v4.0.0-alpha.8
- v4.0.0-alpha.7
- v4.0.0-alpha.6
- v4.0.0-alpha.5
- v4.0.0-alpha.4
- v4.0.0-alpha.3
- v4.0.0-alpha.2
- v4.0.0-alpha.1
- v4.0.0-alpha
- 3.x-dev
- v3.0.1
- v3.0.0
- v3.0.0-alpha.5
- v3.0.0-alpha.3
- v3.0.0-alpha.2
- v3.0.0-alpha
- 2.0.x-dev
- v2.0.4
- v2.0.3
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-alpha.3
- v2.0.0-alpha.2
- v2.0.0-alpha
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- v1.0.0-beta.2
- v1.0.0-beta
This package is auto-updated.
Last update: 2024-09-23 06:28:17 UTC
README
此插件为您的商店添加了 Google Analytics 跟踪功能。您可以选择使用 gtag 或标签管理器的集成。gtag 集成将在跟踪事件时输出传统的 gtag()
函数,而标签管理器集成将使用事件数据填充 dataLayer
。
安装
步骤 1:下载插件
此插件使用 TagBagBundle 向您的页面注入脚本。在安装此插件之前,请阅读并遵循该包的安装说明。
composer require setono/sylius-analytics-plugin:"^4.0@alpha"
注意:此插件的 v4 版本仍在 alpha 版本中,因此可能会发生破坏性更改。
步骤 2:启用插件
然后,通过将其添加到项目中的 config/bundles.php
文件中注册的插件/包列表中,在 SyliusGridBundle
之前启用插件。
<?php $bundles = [ Setono\SyliusAnalyticsPlugin\SetonoSyliusAnalyticsPlugin::class => ['all' => true], Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true], ];
步骤 3:配置插件
# config/packages/setono_sylius_analytics.yaml imports: - { resource: "@SetonoSyliusAnalyticsPlugin/Resources/config/app/config.yaml" } setono_google_analytics: gtag: ~ # If you want to use tag manager instead of gtag, just comment the line above and remove the comment below # tag_manager: ~
步骤 4:导入路由
# config/routes/setono_sylius_analytics.yaml setono_sylius_analytics: resource: "@SetonoSyliusAnalyticsPlugin/Resources/config/routes.yaml"
步骤 5:更新您的数据库模式
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
步骤 6:创建属性/容器
点击您的后端中的 Google Analytics 链接并创建一个新的属性/容器。
步骤 7:您已准备好!
您的 Sylius 商店现在开始跟踪。
可用的事件包括
- add_payment_info
- add_shipping_info
- add_to_cart
- begin_checkout
- purchase
- view_cart
- view_item_list
- view_item
并可以在 EventSubscriber 文件夹 中找到。
如果您想用更多数据丰富事件,请继续阅读。
用更多数据丰富事件
当我们想在 Sylius 应用程序中跟踪事件时,将触发 Setono\GoogleAnalyticsBundle\Event\ClientSideEvent
。
此事件包含实际的 Google Analytics 事件,例如 Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\PurchaseEvent
。这样,如果您订阅了 ClientSideEvent
,您可以在事件渲染(并发送到 Google)之前操纵事件的所有内容。
但还有其他更改数据丰富的方式。这可以通过解析器来完成。插件使用解析器从产品中解析品牌、从产品中解析类别等。继续阅读以了解如何使用此功能。
品牌解析器
一个项目有一个品牌属性(item_brand
),但插件不知道您如何在应用程序中添加品牌数据。因此,您需要实现自己的 BrandResolver
。以下是一个示例
<?php use Setono\SyliusAnalyticsPlugin\Resolver\Brand\BrandResolverInterface; use Sylius\Component\Core\Model\ProductInterface; use Sylius\Component\Core\Model\ProductVariantInterface; final class BrandResolver implements BrandResolverInterface { public function resolveFromProduct(ProductInterface $product): ?string { return $product->getBrand(); // here we assume the getBrand() method will return a brand name or null (if not set) } public function resolveFromProductVariant(ProductVariantInterface $productVariant): ?string { return $this->resolveFromProduct($productVariant->getProduct()); } }
当您实现 BrandResolverInterface
并将您的类注册为服务时,它将自动标记为 setono_sylius_analytics.brand_resolver
并在跟踪时使用。
类别解析器
一个项目有类别属性(item_category
、item_category2
等),默认情况下,插件将根据产品的主要分类或(如果没有设置主要分类)分类的集合中的第一个分类解析这些属性。
您可以在Setono\SyliusAnalyticsPlugin\Resolver\Category\CategoryResolver
类中查看实现。
项目解析器
项目解析器的任务是返回一个Setono\GoogleAnalyticsMeasurementProtocol\Request\Body\Event\Item\Item
对象,该对象可以从订单项或产品中获取。此解析器还提供了一个默认实现,您可以在Setono\SyliusAnalyticsPlugin\Resolver\Item\ItemResolver
类中查看。
项目解析器
项目解析器必须返回一个项目数组,输入为订单。默认实现可以在Setono\SyliusAnalyticsPlugin\Resolver\Items\ItemsResolver
类中找到。
变体解析器
项目具有一个变体属性(item_variant
)。您如何在分析用户界面中查看变体数据由您自己决定,然而,该插件提供了两个默认解析器,即Setono\SyliusAnalyticsPlugin\Resolver\Variant\NameBasedVariantResolver
和Setono\SyliusAnalyticsPlugin\Resolver\Variant\OptionBasedVariantResolver
。
基于选项的版本具有最高的优先级,因此将被首先尝试。它尝试根据变体上的选项(如果有)创建一个变体字符串。
基于名称的版本返回\Sylius\Component\Product\Model\ProductVariantInterface::getName()
。
要实现自己的解析器,只需实现Setono\SyliusAnalyticsPlugin\Resolver\Variant\VariantResolverInterface
。以下是一个示例
<?php use Setono\SyliusAnalyticsPlugin\Resolver\Variant\VariantResolverInterface; use Sylius\Component\Core\Model\ProductVariantInterface; final class ProductNameBasedVariantResolver implements VariantResolverInterface { public function resolve(ProductVariantInterface $productVariant): ?string { return $productVariant->getProduct()->getName(); } }
当您实现VariantResolverInterface
并将您的类注册为服务时,它将自动标记为setono_sylius_analytics.variant_resolver
并在跟踪时使用。请注意,两个现有的变体解析器将作为后备继续存在。
贡献
您可以贡献的方式
- 将messages和validators翻译成您的母语
- 创建新的事件订阅者以处理尚未实现的分析事件
谢谢!