lexik/data-layer-bundle
Composer 安装命令:
composer require lexik/data-layer-bundle
包简介
Manipulate the Google Tag Manager Data Layer from your Symfony application.
README 文档
README
Manipulate the Google Tag Manager Data Layer from your Symfony application. This bundle is compatible Symfony 2.7+, 3.0+ and 4.0+ and PHP 5.4+ to 7.0+.
Installation
Add lexik/data-layer-bundle
to your composer.json file:
composer require "lexik/data-layer-bundle"
Register the bundle in app/AppKernel.php:
public function registerBundles() { return array( // ... new Lexik\Bundle\DataLayerBundle\LexikDataLayerBundle(), ); }
Adding informations to the Data Layer
To pass informations to the Data Layer use the lexik_data_layer.manager.data_layer_manager service directly.
Event / Session Data
Use case
Notify an application event that could be used as goal or conversion like a user registration.
Usage
Get the service lexik_data_layer.manager.data_layer_manager and pass an associative array to it's add() method, it will be stored in session until it is passed to a page. Much like a Flash Message.
Using sessions as storage allows you to notify of an event even after a redirect for example.
Example usage from an EventListener to notify a user registration :
<?php namespace AppBundle\Listener; use Lexik\Bundle\DataLayerBundle\Manager\DataLayerManager; /** * UserEventListener */ class UserEventListener { /** * @var DataLayerManager */ protected $manager; /** * @param DataLayerManager $manager */ public function __construct(DataLayerManager $manager) { $this->manager = $manager; } /** * onUserRegistration */ public function onUserRegistration() { $this->manager->add(['registration' => true]); } }
<service id="app.listener.user_event_listener" class="AppBundle\Listener\UserEventListener"> <argument type="service" id="lexik_data_layer.manager.data_layer_manager" /> <tag name="kernel.event_listener" event="fos_user.registration.completed" method="onUserRegistration" /> </service>
Default Data
Use case
Set the user id on every page for example.
Usage example
Create a service implementing the Lexik\Bundle\DataLayerBundle\Collector\CollectorInterface and tag it using the lexik_data_layer.collector tag.
It's handle method will be passed the current Data Layer array, which you can modify by adding or modifying its values.
<?php namespace Lexik\Bundle\DataLayerBundle\Collector; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Symfony\Component\Security\Core\User\UserInterface; /** * UserIdCollector */ class UserIdCollector implements CollectorInterface { /** * @var TokenStorageInterface */ protected $tokenStorage; /** * @param TokenStorageInterface $tokenStorage */ public function __construct(TokenStorageInterface $tokenStorage) { $this->tokenStorage = $tokenStorage; } /** * {@inheritdoc} */ public function handle(&$data) { $token = $this->tokenStorage->getToken(); if ($token->getUser() && $token->getUser() instanceof UserInterface) { $data[] = ['user_id' => md5($token->getUser()->getUsername())]; } } }
<service id="lexik_data_layer.collector.user_id" class="Lexik\Bundle\DataLayerBundle\Collector\UserIdCollector"> <argument type="service" id="security.token_storage" /> <tag name="lexik_data_layer.collector" /> </service>
Adding / Writing Data Layer variables to the page
Use the provided lexik_data_layer() twig function to write the Data Layer value to a page template.
This will automatically reset the Data Layer informations stored in session.
Don't forget to use it BEFORE you insert the Tag Manager tag.
<body> <script> var dataLayer = {{ lexik_data_layer() }}; </script> <!-- Google Tag Manager --> ... <!-- End Google Tag Manager -->
Testing
Setup the test suite using Composer:
$ composer install
Run it using PHPUnit:
$ vendor/bin/phpunit
Contributing
See CONTRIBUTING file.
Credits
- Nicolas Cabot n.cabot@lexik.fr
- Lexik dev@lexik.fr
- All contributors
License
This bundle is under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE
lexik/data-layer-bundle 适用场景与选型建议
lexik/data-layer-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 114.45k 次下载、GitHub Stars 达 14, 最近一次更新时间为 2016 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「bundle」 「analytics」 「google」 「tag manager」 「data layer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lexik/data-layer-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lexik/data-layer-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lexik/data-layer-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
Provide a way to secure accesses to all routes of an symfony application.
DMS Meetup API Bundle, enables Meetup API clients in services
Analytics chooser extensions for site settings.
Bundle Symfony DaplosBundle
统计信息
- 总下载量: 114.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 14
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-02-15