Skip to content

Usage via Facade or Helper Class

Toni Suárez Rios edited this page Mar 17, 2024 · 1 revision

The Laravel UTM-Parameters package offers flexible options for integrating and utilizing UTM parameters within your Laravel application. You can access UTM parameters either through the provided Facade or via Helper functions, depending on your preference and coding style.

Using the Facade

The Facade provides a convenient way to interact with the UTM parameters throughout your application. By leveraging the Facade, you can easily retrieve, check, and utilize UTM parameters in your controllers, views, middleware, and other parts of your Laravel application.

Here's an example of how you can use the Facade to check for the presence of a specific UTM parameter and perform actions accordingly:

use Suarez\UtmParameter\Facades\UtmParameter;

if (UtmParameter::has('campaign', 'special-sale')) {
    return redirect('/special-offer');
}

In this example, the UtmParameter Facade is used to check if the UTM parameter campaign has the value special-sale. If it does, the user is redirected to a special offer page.

Using the Helper Class

Alternatively, you can utilize the Helper functions provided by the Laravel UTM-Parameters package to work with UTM parameters. These Helper functions offer a more concise syntax and can be directly used within your views, controllers, routes, and other parts of your Laravel application.

Here's how you can use the Helper functions to achieve the same result as the previous example:

if (has_utm('campaign', 'special-sale')) {
    return redirect('/special-offer');
}

In this code snippet, the has_utm Helper function is used to check if the UTM parameter campaign has the value special-sale, and if true, a redirect to a special offer page is performed.

Conclusion

Whether you prefer using the Facade or the Helper functions, the Laravel UTM-Parameters package offers convenient methods for working with UTM parameters in your Laravel application. Choose the approach that best suits your coding style and requirements, and leverage UTM parameters to enhance the functionality and customization of your application.