A lightweight way to handle UTM parameters session-based in your Laravel Application.
@hasUtm('source', 'newsletter')
<p>Special Content for Newsletter-Subscriber.</p>
@endhasUtm
Follow these steps to install the Laravel UTM-Parameters package. Guide for Laravel 10 and below.
Open your terminal and navigate to your Laravel project directory. Then, use Composer to install the package:
$ composer require suarez/laravel-utm-parameter
Once the package is installed, you need to add the UtmParameters middleware to your Laravel application. Open the bootstrap/app.php
file and append the UtmParameters::class
inside the web-group.
# Laravel 11
return Application::configure(basePath: dirname(__DIR__))
...
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
Suarez\UtmParameter\Middleware\UtmParameters::class,
/* ... keep the existing middleware here */
]);
})
...
To enable UTM-Parameters only for certain requests or routes in your application, you can add an alias for the UtmParameters middleware. Open the bootstrap/app.php file and append the UtmParameters::class
inside the web-group.
# Laravel 11
use Suarez\UtmParameter\Middleware\UtmParameters;
->withMiddleware(function (Middleware $middleware) {
$middleware
->alias([
/* ... keep the existing mappings here */
'utm-parameters' => UtmParameters::class,
])
->web(append: [
/* ... keep the existing mappings here */
UtmParameters::class
]);
})
To apply UTM-Parameters to specific routes, use the following middleware: utm-parameters
Route::middleware('utm-parameters')
->get('landing-page/{slug}', 'LandingPageController@show');
To get an array of all UTM parameters, use this helper: get_all_utm()
.
$parameter = get_all_utm();
If you need to retrieve certain UTM parameters, use get_utm('source|medium|campaign|term|content')
.
<p>You came from {{ get_utm('source') }}</p>
// Some Task in your Class
public function someTask()
{
return match(get_utm('source')) {
'bing' => Bing::class,
'google' => Google::class,
'duckduckgo' => DuckDuckGo::class,
'newsletter' => Newsletter::class,
default => Default::class
};
}
// Render a view based on an utm_source
Route::get('/', function () {
return match(get_utm('source')) {
'newsletter' => view('newsletter'),
default => view('welcome')
};
});
Sometimes you want to show or do something, if user might have some or specific utm-parameters.
Simply use:
has_utm('source|medium|campaign|term|content', 'optional-value')
has_not_utm('source|medium|campaign|term|content', 'optional-value')
@hasUtm('source', 'corporate-partner')
<div>Some corporate partner related stuff</div>
@endhasUtm
@hasNotUtm('term')
<p>You have any term.</p>
@endhasNotUtm
if (has_utm('campaign', 'special-sale')) {
redirect('to/special-sale/page');
}
if (has_not_utm('campaign', 'newsletter')) {
session()->flash('Did you know, we have a newsletter?');
}
Explore additional use cases and resources on the wiki pages
- Installation Guide
- Installation Guide (Laravel 8.x to 10.x)
- How it works
- Limitations
- Advanced Usage
- Blade Usage
- Usage via Facade or Helper Class
- Use Case: A/B Testing
- Use Case: Different Styles for Social Media
- Use Case: Lead Attribution
- Use Case: Social Media Tracking
- Use‐Case: Newsletter Redirect on Product Detail Page
- Use‐Case: Offline Marketing Integration
The Laravel UTM-Parameters package is open-sourced software licensed under the MIT license.