Ferhat Gölge
Computer Worker
Google Analytics (GA4) Integration
Laravel Integration with Google Analytics (GA4): Improving Dashboard Performance with DB Cache and Scheduler
Meta Description:
A step-by-step guide to securely connecting Google Analytics (GA4) data to Laravel projects, caching the data in the database, and automatically updating it using a scheduler on a Linux server.
Keywords:
Laravel Google Analytics, GA4 Laravel integration, Laravel scheduler, Laravel analytics dashboard, GA4 API Laravel, Laravel analytics cache
Laravel Integration with Google Analytics (GA4)
In modern web dashboards, Google Analytics 4 (GA4) data is crucial for analyzing user behavior. However, in many projects, dashboard performance can decrease because GA data is pulled directly via API.
In this article:
- Connecting Laravel with Google Analytics GA4
- Caching API data in the database
- Improving dashboard performance
- Automatic data updates with Laravel Scheduler
- cron installation on a Linux server
We will explain the steps.
The structure used in this guide is explained using the FerhatGolge panel architecture , but the same method can be applied to all Laravel projects.
1. Establishing the Google Analytics (GA4) Connection
Creating a GA4 Service Account
To enable Laravel applications to access the GA4 API, a Service Account must be created.
Steps:
- Open Google Cloud Console
- Go to IAM & Admin → Service Accounts
- Create a new service account
- Generate and download JSON keys.
- On the GA4 panel
Go to the section and add the service account as a Viewer .
2. Adding the JSON Key to the Laravel Project
For security reasons, the JSON key file should not be placed in the public folder .
Suggested folder:
Laravel can read this file directly.
3. GA4 Property ID Identification
The Property ID value found in the Google Analytics panel should be stored in the application's settings section.
Example:
This value will be used in API queries.
4. JSON Path Setting
The system must read the path to the JSON file from the settings.
Example setting:
Important safety note:
The JSON file should not be located in the public or uploads folder.
5. Testing the Connection
The following artisan command can be used to test that the GA4 connector is working correctly.
This command verifies the system connection by retrieving GA data.
6. Storing GA Data with DB Cache
Storing data in the database instead of making an API call every time the dashboard page is opened significantly improves performance.
Advantages:
- API limits are reduced.
- Dashboard opens faster.
- API errors do not affect the user.
Creating Migration
Example table structure:
| column description | |
| date_range_days | data range (7, 30 etc.) |
| summary | general statistics |
| countries | country data |
| devices | device data |
| pages | page performance |
| fetched_at | data retrieval time |
Model Creation
It is necessary to cast the JSON fields within the model.
7. Artisan Command that Retrieves GA Data
In Laravel, we can create an Artisan Command to retrieve GA data and write it to the database.
Creating commands:
Example data payload:
8. Installing Laravel Scheduler
Laravel Scheduler can be used to regularly update GA data.
This command will run automatically every hour.
9. Reading Data from the Dashboard
Dashboard will now read data from the DB cache instead of directly from the API.
This method significantly improves dashboard performance.
10. Autorunning on Ubuntu/Linux Server
Deploy Commands
Defining Cron Jobs
For Laravel scheduler to work, a cron job must be added.
To add it to the server:
File Permissions
Troubleshooting
GA data is coming back empty.
- Is the Property ID correct?
- Is the service account authorized within GA4?
The JSON file cannot be read.
- Is the file inside the storage?
- Are the file permissions correct?
API access error
Is the Analytics Data API active in Google Cloud?
Time difference problem
Make sure the server clock is correct.
Conclusion
Using DB caching for Google Analytics data in Laravel, instead of pulling data directly from the API , significantly improves dashboard performance.
Thanks to this architecture:
- GA API calls decrease.
- Dashboard loads faster.
- Data is updated regularly.
- The system operates more stably.