benmacha/mousetracker
Self-hosted mouse/click/scroll tracker for Symfony 5.4–7.x. Records mouse moves, clicks, scroll, keyboard, form-blur values, and DOM snapshots, storing sessions in your own DB for Mouseflow-style heatmaps and replay. No jQuery.
Install the package:
composer require benmacha/mousetracker:^2.0
For Symfony Flex, no additional bundle registration is needed. Otherwise, add to config/bundles.php:
benmacha\mousetracker\TrackerBundle::class => ['all' => true],
Configure routes (config/routes/mouse_tracker.yaml):
mouse_tracker:
resource: '@TrackerBundle/Resources/config/routes.yaml'
prefix: /tracker
Set up the database:
php bin/console doctrine:schema:update --force
(Or use migrations if preferred.)
Publish assets:
php bin/console assets:install --symlink public/
Inject the tracker snippet in your base template (e.g., templates/base.html.twig):
{{ mouse_tracker_service.build()|raw }}
Expose the service as a Twig global (config/packages/twig.yaml):
twig:
globals:
mouse_tracker_service: '@mouse_tracker'
Secure the backend (config/packages/security.yaml):
security:
access_control:
- { path: ^/tracker/back, roles: ROLE_ADMIN }
- { path: ^/tracker, roles: PUBLIC_ACCESS }
Add the tracker snippet to any page where you want to record interactions. The tracker will automatically:
/tracker/createClient and /tracker/addData.Access the backend at /tracker/back to view session replays and heatmaps.
Since mousetracker is a Symfony bundle, you’ll need to adapt it for Laravel. Here’s how:
MouseTrackerServiceProvider):
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use benmacha\mousetracker\TrackerBundle;
class MouseTrackerServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->register(TrackerBundle::class);
}
}
Register the provider in config/app.php:
'providers' => [
// ...
App\Providers\MouseTrackerServiceProvider::class,
],
routes/web.php:
Route::prefix('tracker')->group(function () {
require __DIR__.'/../vendor/benmacha/mousetracker/Resources/config/routes.yaml';
});
php artisan vendor:publish --provider="benmacha\mousetracker\TrackerBundle" --tag="migrations"
php artisan migrate
php artisan vendor:publish --provider="benmacha\mousetracker\TrackerBundle" --tag="assets"
resources/views/layouts/app.blade.php):
<script src="{{ asset('vendor/mousetracker/js/tracker.js') }}"></script>
mouse_tracker_service as a global:
// In your service provider's boot method
$this->app['twig']->addGlobal('mouse_tracker_service', $this->app->make('mouse_tracker'));
@inject or manually render the tracker snippet:
{!! $mouse_tracker_service->build() !!}
config/mouse_tracker.php:
return [
'record_click' => true,
'record_move' => true,
'record_keyboard' => true,
'percentage_recorded' => 100,
'disable_mobile' => false,
'ignore_ips' => [],
];
/tracker/back routes using Laravel middleware (e.g., auth):
Route::prefix('tracker/back')->middleware(['auth'])->group(function () {
// Backend routes
});
Use the percentage_recorded config to sample a subset of visitors (e.g., 10 for 10%):
mouse_tracker:
percentage_recorded: 10
Exclude internal IPs or test environments:
mouse_tracker:
ignore_ips: ['192.168.1.0/24', '127.0.0.1']
Override settings client-side via JavaScript:
<script>
window.UST && (UST.settings.delay = 200); // Adjust batch delay
</script>
Customize the replay UI by overriding the Twig templates in resources/views/vendor/mousetracker/.
GDPR Compliance:
/tracker/createClient and /tracker/addData endpoints are public by default. Ensure you:
tracker.js./tracker/back) with authentication.Mobile Tracking:
disable_mobile: false). Enable it only if needed, as mobile interactions may not translate well to heatmaps.Database Schema:
tracker__client, tracker__page, and tracker__data. Ensure your Laravel migrations handle these correctly, especially if you’re using a custom database connection.Asset Paths:
public/bundles/tracker/js/tracker.js. If you’re using Laravel Mix or Vite, ensure the path is correctly aliased in your build config.Symfony-Specific Features:
AbstractController, #[Route] attributes, and ServiceEntityRepository. In Laravel, you’ll need to manually handle:
Configuration class).Performance:
percentage_recorded to reduce load.Debugging:
/tracker/createClient and /tracker/addData. Look for:
Laravel-Specific Adjustments:
asset() function with Laravel’s asset() helper in Twig templates:
{{ asset('bundles/tracker/js/tracker.js') }}
<script src="{{ asset('vendor/mousetracker/js/tracker.js') }}"></script>
Customizing the Tracker:
tracker.js file by publishing it and modifying it:
php artisan vendor:publish --provider="benmacha\mousetracker\TrackerBundle" --tag="assets"
UST object in your global JS to add custom events or filters.Data Retention:
use App\Models\TrackerClient; // Replace with your entity model
public function handle()
{
TrackerClient::where('created_at', '<=', now()->subMonths(6))->delete();
}
Testing:
How can I help you explore Laravel packages today?