Knowing user behavior is crucial for optimizing any web app. While Running a Laravel-powered site like a e-commerce store, SaaS solution, or business platform integrating Google Analytics 4 (GA4) makes it easy to track traffic, user engagement, and conversions. In this tutorial, we’ll discuss how to integrate Google Analytics 4 in Laravel with an easy setup.
With this integration, you’ll be able to track user activities, analyze key metrics, and make informed decisions about performance and growth.
Use Cases of Google Analytics 4 in Laravel
- Track user traffic and engagement across different devices
- Measure conversion rates for forms, checkouts, or signups
- Understand popular pages and navigation paths
- Monitor events like button clicks or downloads
- Optimize marketing campaigns using audience insights
Creating Google Analytics Property
Before connecting Google Analytics 4 with your Laravel app, you need to create a GA4 property in your Google Analytics account. Start by signing in to analytics.google.com using your Google account and open the Admin panel from the bottom-left corner of the dashboard.
Next, under the Property column, click Create Property. Enter details for your website or application like name, select your reporting time zone, and choose the correct currency.
Make sure to pick Google Analytics 4 (GA4) as the property type before continuing.
Once the property is created, add a Data Stream by selecting Web as the platform and entering your Laravel app’s domain.
After setup, Google Analytics will generate a Measurement ID (for example, G-XXXXXXXXXX) like below.

Configuring GA4 Property in Laravel
Once your google analytics measurement ID is created, we can move forward with Laravel setup. To connect Laravel with your GA4 property, start by adding your Measurement ID to the .env file. It helps you manage different environments securely.
Open your .env file and add a variable such as:
GA_MEASUREMENT_ID=G-XXXXXXXXXX
Next, include this variable inside your Laravel service configuration so it can be accessed easily across views and will be cached automatically by framework itself. In config/services.php, add:
'ga_measurement_id' => env('GA_MEASUREMENT_ID'),
With this approach it ensures your GA4 configuration remains secure and easy to manage without exposing sensitive information.
Separate Blade File for GA4 Code
It’s a good practice to keep your tracking code in a seperate blade file for better maintenance and cleaner templates. Create a new file inside resources/views/includes/ and name it ga4.blade.php.
Inside this file, paste your GA4 tracking snippet and replace the Measurement ID dynamically:
@if(config('services.ga_measurement_id'))
<script async src="https://www.googletagmanager.com/gtag/js?id={{ config('services.ga_measurement_id') }}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '{{ config('services.ga_measurement_id') }}');
</script>
@endif
Adding the Blade File into the Master Layout
The last thing you need to do is include this blade file into any relevant layouts like user, guest or even admin. Most of the cases, you just need to include this into master or app layout which has all the child layout. However, you can implement it into selected pages as per your need.
Open your resources/views/layouts/app.blade.php and add the include statement just before the closing tag:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ config('app.name', 'Laravel') }}</title>
{{-- Include GA4 tracking --}}
@if (app()->environment('production'))
@include('includes.ga')
@endif
@vite(['resources/css/app.css', 'resources/js/app.js'])
</head>
<body>
<div id="app">
@yield('content')
</div>
</body>
</html>
As you can see, we have just included google analytics blade file into our layout file. Additionally, we have added condition to load that only if Laravel is running into production environment. The google analytics does not work properly with localhost so it’s a additional check for that.
Conclusion
Using Google Analytics 4 in Laravel empowers you to make informed decisions based on data also know how users are engaging with your Laravel application. Whether you are optimizing for conversions or launching new features, Laravel GA4 integration gives you the ability to always have real-time insights that inform your decisions. With easy setup and clean installation, it can help to initiate tracking meaningful user action and increase your app’s growth.
Check out Laravel Google Login Using Socialite to learn how to add Google Sign-In to your Laravel app with ease.
