-
-
Notifications
You must be signed in to change notification settings - Fork 3
Blade Usage
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.
The package equips you with a set of handy helper functions to retrieve and work with UTM parameters captured in the user's session.
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
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
One of the most powerful features is the ability to conditionally render content based on the presence or value of UTM parameters.
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
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
Integrating UTM awareness into your Blade templates opens up a world of possibilities:
-
Personalized Welcome Messages: Greet users differently based on their
utm_source
orutm_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.
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!