djdmg/transparent-pixel-bundle
Installation
composer require djdmg/transparent-pixel-bundle
Add to config/app.php under providers:
Djdmg\TransparentPixelBundle\TransparentPixelBundle::class,
Configuration Publish the config file:
php artisan vendor:publish --provider="Djdmg\TransparentPixelBundle\TransparentPixelBundle" --tag="config"
Edit config/transparent_pixel.php to set:
pixel_url (e.g., https://yourdomain.com/transparent-pixel)pixel_width (default: 1x1)pixel_color (default: #FFFFFF)First Use Case Inject the pixel into an email template (e.g., Blade or SwiftMailer):
// In a Blade template
{!! app('transparent_pixel')->render() !!}
// In SwiftMailer
$message->addPart(app('transparent_pixel')->render(), 'text/html');
Blade Templates
Use the render() method directly in your email Blade files:
<table>
<!-- Email content -->
<tr>
<td>
{!! app('transparent_pixel')->render() !!}
</td>
</tr>
</table>
Tip: Place the pixel in the last row of your email to avoid layout shifts.
SwiftMailer Integration Attach the pixel as an HTML part:
$message = (new \Swift_Message('Hello'))
->setFrom(['sender@example.com' => 'Sender'])
->setTo(['recipient@example.com'])
->addPart(app('transparent_pixel')->render(), 'text/html');
Override pixel attributes per email:
$pixel = app('transparent_pixel')
->setWidth(2)
->setHeight(2)
->setColor('#000000')
->render();
Extend the pixel URL with tracking parameters:
$pixel = app('transparent_pixel')
->setPixelUrl('https://yourdomain.com/track?campaign=welcome')
->render();
Bind a custom instance in AppServiceProvider@boot():
$this->app->bind('transparent_pixel', function () {
return new \Djdmg\TransparentPixelBundle\TransparentPixel(
config('transparent_pixel.custom_url'),
config('transparent_pixel.custom_width')
);
});
Mock the pixel in PHPUnit:
$this->app->instance('transparent_pixel', $mockPixel);
$mockPixel->shouldReceive('render')->andReturn('<img src="mock-pixel" />');
Pixel Blocking
Caching Issues
?campaign=...) may not update.Email Client Compatibility
<table> with explicit dimensions:
<table border="0" cellpadding="0" cellspacing="0" width="1"><tr><td><img src="pixel-url" width="1" height="1" /></td></tr></table>
License Compliance
Verify Pixel URL Check if the pixel URL is being generated correctly by inspecting the rendered HTML or logs:
\Log::debug('Transparent Pixel URL:', ['url' => app('transparent_pixel')->getPixelUrl()]);
Test in Email Clients Use tools like Litmus or Email on Acid to test rendering across clients.
Custom Pixel Generator
Extend the TransparentPixel class to support SVG or other formats:
class CustomTransparentPixel extends \Djdmg\TransparentPixelBundle\TransparentPixel {
public function render(): string {
return '<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1" viewBox="0 0 1 1"></svg>';
}
}
Event-Based Tracking Dispatch events when the pixel is rendered (e.g., for analytics):
use Illuminate\Support\Facades\Event;
Event::listen('transparent_pixel.rendered', function ($pixel) {
\Log::info('Pixel rendered for campaign: ' . $pixel->getCampaign());
});
Configuration Overrides Override config values dynamically:
config(['transparent_pixel.pixel_color' => '#' . str_random(6)]);
Preload the Pixel
Add the pixel URL to the <head> of your email template to preload it:
<link rel="preload" href="pixel-url" as="image">
Note: Preload support varies by email client.
Use a CDN Host the pixel on a CDN (e.g., Cloudflare) to reduce latency and improve deliverability.
How can I help you explore Laravel packages today?