djdmg/transparent-pixel-bundle
Composer 安装命令:
composer require djdmg/transparent-pixel-bundle
包简介
Symfony bundle that provides a 1×1 transparent tracking pixel for pages and emails; logs IP/UA/OS/browser/device and more.
README 文档
README
Package: djdmg/transparent-pixel-bundle
A tiny Symfony bundle that adds a 1×1 transparent tracking pixel for pages and emails. Each hit is stored with rich request details (IP, User-Agent, OS, browser, device, mobile/bot flags, headers, query params, cookies, referer, method, timestamp).
Requirements
- PHP 8.2+
- Symfony 7.x (
framework-bundle,twig-bundle) - Doctrine ORM 3.x + DoctrineBundle
Install
composer require djdmg/transparent-pixel-bundle
If you allow contrib recipes (recommended):
composer config extra.symfony.allow-contrib true
If Flex recipe isn’t available, wire manually:
<?php // config/bundles.php return [ Djdmg\TransparentPixelBundle\TransparentPixelBundle::class => ['all' => true], ];
# config/routes/transparent_pixel.yaml transparent_pixel_bundle: resource: '@TransparentPixelBundle/Controller/' type: attribute
Database
Generate and run migrations in your app:
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate -n
Tables created: tp_pixel, tp_pixel_hit.
Usage
1) Create or fetch a Pixel
use Djdmg\TransparentPixelBundle\Service\PixelManager; public function show(PixelManager $pixels) { // Creates on first call, returns existing afterwards $pixel = $pixels->ensurePixel('newsletter-aug-2025', ['campaign' => 'summer25']); return $this->render('page.html.twig', ['pixel' => $pixel]); }
2) Render in Twig (page)
{# Adds ?uid=… to the URL and logs it under "query" JSON #} {{ transparent_pixel_tag(pixel, { uid: app.user ? app.user.id : 'guest' }) }}
Or just the URL:
<img src="{{ transparent_pixel_url(pixel, { source: 'landing' }) }}" width="1" height="1" style="display:none" alt="" loading="eager">
3) Use in emails (Symfony Mailer)
use Symfony\Bridge\Twig\Mime\TemplatedEmail; $pixel = $pixels->ensurePixel('reset-password'); $email = (new TemplatedEmail()) ->to($user->getEmail()) ->subject('Reset your password') ->htmlTemplate('emails/reset.html.twig') ->context([ 'pixel' => $pixel, 'uid' => (string)($user->getId() ?? 'guest'), ]); $this->mailer->send($email);
{# templates/emails/reset.html.twig #} {{ transparent_pixel_tag(pixel, { uid: uid }) }}
Tip (CLI/cron/workers): set a base URL if you generate links outside HTTP requests:
# config/packages/framework.yaml framework: router: default_uri: 'https://app.example.com'
4) Read access details (code)
// All hits (limit 500) or only for a specific pixel $all = $pixels->getAccessDetails(null, 500); $perPixel = $pixels->getAccessDetails($pixel, 200);
Each item includes:
[ 'at' => '2025-08-15T10:00:23+00:00', 'ip' => '203.0.113.10', 'os' => 'iOS 17', 'browser' => 'Mobile Safari 17.4', 'device' => 'iPhone', 'isMobile' => true, 'isBot' => false, 'method' => 'GET', 'referer' => 'https://example.com/page', 'headers' => [...], 'query' => ['uid' => '123'], 'cookies' => [...], 'ua' => 'Mozilla/5.0 (...)', 'token' => 'ab12…', 'pixel' => ['id' => 1, 'name' => 'newsletter-aug-2025'], ]
Production notes
- No-cache headers ensure every view triggers a hit.
- Trusted proxies for real client IPs:
framework: trusted_proxies: '%env(TRUSTED_PROXIES)%' trusted_headers: ['x-forwarded-for','x-forwarded-proto','x-forwarded-host','x-forwarded-port']
# .env TRUSTED_PROXIES=127.0.0.1,REMOTE_ADDR
- Privacy/GDPR: inform users, consider anonymizing IPs (e.g., truncate /24) and add a retention policy.
Troubleshooting
- Routes not found → ensure routes are imported with
@TransparentPixelBundle/Controller/. - Relative URLs in emails → set
framework.router.default_uri. - No tables → run migrations (
diff+migrate).
License
MIT © djdmg
djdmg/transparent-pixel-bundle 适用场景与选型建议
djdmg/transparent-pixel-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 427 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「twig」 「email」 「bundle」 「analytics」 「tracking」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 djdmg/transparent-pixel-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 djdmg/transparent-pixel-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 djdmg/transparent-pixel-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
The bundle for easy using json-rpc api on your project
Twig extensions for Tracy Debugger
统计信息
- 总下载量: 427
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-15