cherrypulp/laravel-datalayer
Composer 安装命令:
composer require cherrypulp/laravel-datalayer
包简介
Datalayer helper
关键字:
README 文档
README
Package description: Laravel package that generating Google's DataLayer script
Installation
Install via composer
composer require cherrypulp/laravel-datalayer
Register your Google ID
Add this ` GOOGLE_ID=YOUR_GOOGLE_ID` at the end of your .env file
Register Service Provider
Note! This and next step are optional if you use laravel>=5.5 with package auto discovery feature.
Add service provider to config/app.php in providers section
Cherrypulp\DataLayer\ServiceProvider::class,
Register Facade
Register package facade in config/app.php in aliases section
'DataLayer' => Cherrypulp\DataLayer\Facades\DataLayer::class,
Usage
In your Controllers
Push one value in the DataLayer
DataLayer::push('foo', 'bar');
Push an array of data in the DataLayer
DataLayer::pushArray([
'user_name' => 'John Doe',
'age' => '42',
'country' => 'Belgium',
]);
Don't hesitate to check the prototype of the method to view all possibles options
In your View
Publish the DataLayer in the view
Just call this method in your app layout before the closing HEAD tag.
DataLayer::publish();
It will print this entire HTML code in your layout
<script>
dataLayer = dataLayer || [];
</script>
<script>
dataLayer.push({foo:'bar',user_name:'John Doe',age:42,country:'Belgium'});
</script>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','YOUR_GOOGLE_ID');</script>
<!-- End Google Tag Manager -->
Don't forget to call DataLayer::noScript() right after your BODY tag
DataLayer::noScript();
It will print the following :
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=YOUR_GOOGLE_ID"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
You can use an optional array to choose if you do not want to initialise the global JS object or/and the Google's GTM script
DataLayer::publish(['init' => false, 'script' => false]);
It will just print this
<script>
dataLayer.push({foo:'bar',user_name:'John Doe',age:42,country:'Belgium'});
</script>
The DataLayer is reset after each call to the DataLayer::publish() method.
Others methods
Load the data from session
DataLayer::load();
Save the data in the session
DataLayer::save();
Clear the data in the session
DataLayer::clear();
Get the array data
DataLayer::getData();
Print the global JS object in the view
DataLayer::init();
It will print this in the HTML
<script>
window.dataLayer = window.dataLayer || [];
</script>
Print the Google's GTM script in the view
The $google_id parameter is optional. If omitted, it will use the Google ID set in your .env file
DataLayer::script([$google_id = null]);
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','YOUR_GOOGLE_ID');</script>
<!-- End Google Tag Manager -->
Also, don't forget to add the no-script tag with
DataLayer::noScript([$google_id = null]);
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=YOUR_GOOGLE_ID"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
Show the content of the DataLayer (debug purpose)
DataLayer::dd();
Security
If you discover any security related issues, please email anthony@cherrypulp.com instead of using the issue tracker.
Credits
See also
cherrypulp/laravel-datalayer 适用场景与选型建议
cherrypulp/laravel-datalayer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.62k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「datalayer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cherrypulp/laravel-datalayer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cherrypulp/laravel-datalayer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cherrypulp/laravel-datalayer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
GTM integration and dataLayer configuration for Shopware
Class helping to generate datalayer
A simplest abstraction for eloquent models
A helper package that help to generate Google's DataLayer script on pages
PHP DatabaseLayer to work with MVC projects based on DaviW framework
Data layer for integration hub
统计信息
- 总下载量: 2.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-02

