Skip to content

Blade Usage

Toni Suárez Rios edited this page May 20, 2025 · 3 revisions

Understanding where your users come from is key to delivering personalized experiences. UTM (Urchin Tracking Module) parameters are your best friends here, and with the Laravel UTM-Parameters package, you can effortlessly tap into this data directly within your Blade templates. This means you can dynamically tailor content, display targeted messages, or even adjust layouts based on referral sources and campaign details.

Let's explore how you can use this package to make your Laravel applications smarter.

Accessing UTM Data: The Helper Functions

The package equips you with a set of handy helper functions to retrieve and work with UTM parameters captured in the user's session.

get_all_utm(): Grab All Parameters

Need a complete picture of all UTM parameters for the current session? The get_all_utm() function is your go-to. It returns an associative array of all captured UTM key-value pairs. This is particularly useful for debugging or when you need to iterate over all available tracking data.

{{-- Display all captured UTM parameters --}}
@foreach (get_all_utm() as $key => $value)
    <p><strong>{{ $key }}:</strong> {{ $value }}</p>
@endforeach

get_utm($key): Fetch a Specific Parameter

To retrieve the value of a single UTM parameter, use get_utm($key). Pass the desired UTM key (e.g., 'source', 'medium', 'campaign'), and the function will return its value. If the parameter isn't found in the session, it gracefully returns null.

{{-- Display a welcome message based on the UTM source --}}
@if (get_utm('utm_source'))
    <p>You arrived via our {{ get_utm('utm_source') }} channel!</p>
@else
    <p>Welcome to our site!</p>
@endif

Conditional Rendering with UTMs: Helpers and Directives

One of the most powerful features is the ability to conditionally render content based on the presence or value of UTM parameters.

Checking for Parameters: has_utm()

The has_utm($key, $value = null) function lets you check if a specific UTM parameter exists. Optionally, you can provide a second argument to verify if the parameter's value matches a specific string. It returns true if the condition is met, and false otherwise.

{{-- Show a special offer if the user came from a newsletter --}}
@if (has_utm('utm_source', 'newsletter'))
    <div class="special-offer">
        <p>🎉 Special Offer for our Newsletter Subscribers! 🎉</p>
    </div>
@endif

For even cleaner syntax in your Blade templates, the package provides a corresponding @hasUtm directive:

@hasUtm('utm_source', 'corporate-partner')
    <div class="partner-content">
        <p>Exclusive content for our valued corporate partners.</p>
    </div>
@endhasUtm

Checking for Absence: has_not_utm()

Conversely, has_not_utm($key, $value = null) checks if a UTM parameter does not exist or does not match the specified value (if provided). This is useful for displaying default content or targeting users who didn't arrive through a specific campaign.

{{-- Display a generic message if not from a social media campaign --}}
@if (has_not_utm('utm_medium', 'social'))
    <p>Explore our latest articles and features.</p>
@endif

Like has_utm, there's a Blade directive for this too, @hasNotUtm:

{{-- Show a message if no utm_term is set --}}
@hasNotUtm('utm_term')
    <p>Looking for something specific? Try our search!</p>
@endhasNotUtm

Why Bother with UTM-Driven Views?

Integrating UTM awareness into your Blade templates opens up a world of possibilities:

  • Personalized Welcome Messages: Greet users differently based on their utm_source or utm_campaign.
  • Targeted Promotions: Show special offers only to users arriving from specific marketing channels (e.g., utm_medium=cpc).
  • Content Customization: Display unique content or calls-to-action for different audience segments identified by UTM tags.
  • A/B Testing Layouts: Experiment with different page layouts or features based on campaign parameters.
  • Data-Driven Insights: Log UTM data alongside user actions to better understand campaign effectiveness.

Wrapping Up

The Laravel UTM-Parameters package provides a simple yet powerful way to make your Blade views more dynamic and responsive to user context. By leveraging its helper functions and Blade directives, you can easily read UTM data and tailor the user experience, leading to higher engagement and more effective marketing campaigns. Start experimenting and unlock the potential of personalized content in your Laravel applications today!

Clone this wiki locally