Consent Studio is a great consent management platform for Laravel β and this is how to use it
Find us on Github
This is the link to our Github repository: https://github.com/vallonic/consent-studio-laravel. Here you will find detailed information. The below tutorial is meant to get you started quickly, but does not cover all the functionality.
0. Prerequisites
Make sure that your Laravel application runs on at least Laravel 10.
1. Make sure that you have an account with Consent Studio
Log in at https://consent.studio. If you do not yet have an account, you can create one here:
Also, you must have registered your domain.
2. Install the Consent Studio CMP Laravel package using Composer
Run the following command in your terminal:
composer require consent-studio/laravel
3. Configuration
3.1 Publish the configuration file
Run the following command in your terminal:
php artisan vendor:publish --tag=consent-studio-config
3.2 Modify the configuration, including Google Consent Mode
In the consent-studio.php config file that you have just published, you will find an array of settings which enable you to load Consent Studio. Including with Google Consent Mode v2.
You can find the instructions for all parameters of this file in the Github repository readme file.
This is the part in your Laravel configuration where you can configure the parameters for the Google Consent Mode v2 default command:
'google_consent_mode' => [
'enabled' => true,
'wait_for_update' => 500, // Milliseconds to wait for consent update
'ads_data_redaction' => true,
'url_passthrough' => false,
'defaults' => [
[
'ad_storage' => 'denied',
'ad_user_data' => 'denied',
'ad_personalization' => 'denied',
'analytics_storage' => 'denied',
'functionality_storage' => 'granted',
'personalization_storage' => 'granted',
'security_storage' => 'granted',
],
],
],
4. Include Consent Studio in your front-end (Blade) layouts
It is important that you include both the Consent Studio banner.js script, as well as the window.bakery configuration for our CMP. This can be achieved at once using the @consentstudio Blade directive:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Laravel App</title>
@consentstudio
<!-- Your other scripts here -->
</head>
<body>
@yield('content')
</body>
</html>
5. Wrap content that requires consent in Laravel Blade
Below is an example from our Github repository readme. We have a smart RegEx based @consent directive that can be used to intelligently add a cs-require parameter to certain types of content and will modify scripts from being parsed or external sources from being loaded when consent is missing.
{{-- Marketing scripts --}}
@consent('marketing')
<script>
fbq('track', 'PageView');
</script>
@endconsent
{{-- Analytics scripts --}}
@consent('analytics')
<script src="https://www.google-analytics.com/analytics.js"></script> @endconsent
{{-- YouTube embeds --}}
@consent('marketing')
<iframe src="https://www.youtube.com/embed/dQw4w9WgXcQ" width="560" height="315"></iframe>
@endconsent
The @consent directive supports the following categories:
functional- Essential functionality (usually not blocked)analytics- Analytics and statistics trackingmarketing- Marketing, advertising, and social medianeutral- Neutral category (not assigned to any specific consent type)
The directive accepts only a single category string, not an array.
More information about how to block inline scripts and how to block external sources, can be found here:
6. Implement consent logic in your controllers using the facade
Below, you will find an example of how you can implement Consent Studio logic in the request lifecycle:
use ConsentStudio\Laravel\Facades\ConsentStudio;
// Check if user has seen the banner
if (ConsentStudio::seenBanner()) {
// Banner was shown to the user
}
// Get the consent ID
$consentId = ConsentStudio::id(); // Returns string|null
// Check if a specific consent category is granted if (ConsentStudio::granted('marketing')) {
// Marketing consent is granted
}
// Alternative method (alias)
if (ConsentStudio::has('analytics')) {
// Analytics consent is granted
}
// Check if any of multiple categories are granted
if (ConsentStudio::any(['marketing', 'analytics'])) {
// At least one is granted
}
7. Ensure that Consent Studio has access to your domain
This can be achieved by whitelisting our IP addresses, for example. These can be found in this article.
Questions & support for the Consent Studio Laravel package
When you have concerns, issues, feedback and/or questions, please get in touch with Consent Studio using the contact methods as laid out in this article: