Pixelairport's avatar

Looking for package to create js tracking code in controller

Hi. I setup tracking for a website and want to add event tracking. I know I can do listeners in laravel, but can I also say in the listener something like ...

JSEventTrack('trackEvent', 'Contact', 'Email Link Click', '[email protected]')

... so that i can put something like @push('events') in my blade layout what transform the code to this

_paq.push(['trackEvent', 'Contact', 'Email Link Click', '[email protected]']);

Is there a package or something else? I want to use matomo. I know there is a tag manager which would be better, but then I need cookie consent, because we are in EU.

0 likes
1 reply
LaryAI's avatar
Level 58

Yes, there is a package available for this called "matomo-tracker". You can use this package to track events in your Laravel application using Matomo. Here's how you can use it:

  1. Install the package using composer:
composer require matomo/matomo-tracker
  1. In your controller, you can create a new Matomo tracker object and track the event like this:
use Matomo\Tracker;

$tracker = new Tracker($idSite = 1, $apiUrl = 'http://your-matomo-url.com/matomo.php');
$tracker->doTrackEvent('Contact', 'Email Link Click', '[email protected]');
  1. You can then pass this data to your view using the view method in your controller:
return view('your-view')->with('tracker', $tracker);
  1. In your blade layout, you can then use the @push directive to push the tracking code to the page:
@push('scripts')
    <script type="text/javascript">
        _paq.push(['trackEvent', '{{ $tracker->getEventCategory() }}', '{{ $tracker->getEventAction() }}', '{{ $tracker->getEventName() }}']);
    </script>
@endpush

This will push the tracking code to the page, using the data that you tracked in your controller.

Please or to participate in this conversation.