Skip to content

A lightweight way to handle UTM parameters session-based in your Laravel Application.

License

Notifications You must be signed in to change notification settings

toni-suarez/laravel-utm-parameter

Repository files navigation

Laravel UTM-Parameters

Latest Version on Packagist StyleCI Test PHP 8.x Packagist Downloads Static Badge Statamic Addon

A lightweight way to handle UTM parameters session-based in your Laravel Application.

@hasUtm('source', 'newsletter')
  <p>Special Content for Newsletter-Subscriber.</p>
@endhasUtm

Installation

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

Middleware Configuration

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 */
    ]);
  })
  ...

Middleware Alias (Optional)

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');

Usage

get_all_utm()

To get an array of all UTM parameters, use this helper: get_all_utm().

$parameter = get_all_utm();

get_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')
    };
});

has_utm()

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?');
 }

Resources

Explore additional use cases and resources on the wiki pages

Inspirations


License

The Laravel UTM-Parameters package is open-sourced software licensed under the MIT license.

About

A lightweight way to handle UTM parameters session-based in your Laravel Application.

Topics

Resources

License

Stars

Watchers

Forks

Languages