Laravel Utm Tracking: Built-in Simplicity Beats Third-party Complexity
Last updated on

In the open-source world, it's not just a common practice anymore. It's more like a built-in habit. Whenever we need to implement some functionality, we first look around to see if there’s already a tool available. If there is, we review it, and if it meets our needs, we go ahead and use it. This is how things work — whether it’s WordPress with plugins, Drupal with modules, or Laravel with packages.
But every now and then, there are tasks that are small and easy to handle on your own. They also give you a chance to stay hands-on. A good example is something I recently did on my personal website: https://muhammadhuzaifa.pro. I wanted to forward UTM query string parameters. When a visitor lands on the site with UTM tags, I wanted to track which links they visit during their session, maybe even do more with that data later.
Like always, I first looked for an existing package. I found one by Spatie that seemed related. But then I thought, this is such a small task. Why bring in a whole package for it? The package wasn't under active development and didn’t seem fully stable either. That means I’d need to test it thoroughly before using it. Since it was just for my own site, not for a client, I decided it wasn’t worth the effort. I didn’t want to overcomplicate a simple need.
I had a few options in mind:
- I could pass the UTM parameters manually by using the second argument of Laravel’s
route()
helper and fetch them from the request. But doing that for every route? Not practical. - I could
bind
acustom router class
in aservice provider
and override theroute
generation using amacro
. Definitely possible, but not something I wanted to dive deep into just for this. - The simplest solution was to write a
laravel middleware
. It checks for UTM parameters in the query string. If found, itstores them in the session
. If the URL doesn’t have them but the session does, the middleware rewrites the query and continues. That’s all. No bloat. Works perfectly. The analytics data is clean and useful.
Before I wrap up, I want to share a quick note for computer science students. Using ChatGPT is fine. It’s a great help. But if you depend on it for every little thing, you're limiting your growth. Just like how child psychologists say kids should face challenges to learn problem-solving, students need to struggle a bit too. That’s how you build real skill. Try to understand the problem first and attempt a solution before asking GPT. Once you gain some experience, you’ll know when it makes sense to get help and when it’s better to go solo.
If you found this post helpful, you might also enjoy two of my write-ups where I dive deeper into Laravel and SEO: Technical SEO Configurator for Laravel by Honeystone and Improve Your Laravel Ecommerce Store Technical SEO Using Butschster\LaravelMetaTags. I share hands-on insights and practical examples in both. And if you're looking for an experienced Laravel developer with a proven Upwork track record, feel free to check out my Upwork profile and get in touch.
Thanks for sticking around till the end. Stay happy, stay blessed.