combindma / laravel-facebook-pixel

Meta Pixel integration for Laravel
MIT License
33 stars 9 forks source link

[Bug]: Issue with MetaPixel Data Not Displaying in View #6

Open zhi8617 opened 1 week ago

zhi8617 commented 1 week ago

What happened?

I expected the following to show up:


 <body>
  <script>fbq('track', 'Purchase', {"currency":"USD","value":30});</script>
  <!-- ... -->
</body>
</html>

How to reproduce the bug

I set up MetaPixel as follows:

MetaPixel::track('Purchase', ['currency' => 'USD', 'value' => 30.00]);
MetaPixel::getEventLayer();
return view('welcome');

In testing, getEventLayer has data, but the view does not display any data.

I only see the head section being set up, but nothing is displayed in the body . I even set up the following in body.blade.php:

@php
$metaPixel->track('eventName', ['attribute' => 'value']);
@endphp

Then, the data appears.

Package Version

last

PHP Version

8.2

Laravel Version

11

Which operating systems does with happen with?

No response

Notes

No response

DuniPN commented 1 week ago

The solution I found for this is to specify "singleton" instead of "bind" in the class vendor\combindma\laravel-facebook-pixel\src\FacebookPixelServiceProvider.php. So: public function registeringPackage(): void { $this->app->singleton(MetaPixel::class, function () { return new MetaPixel(); }); } instead of: public function registeringPackage(): void { $this->app->bind(MetaPixel::class, function () { return new MetaPixel(); }); }

Then it works!

zhi8617 commented 1 week ago

Thank .