In the world of digital marketing, understanding your visitor data is crucial. By using the Laravel Analytics package, you can easily tap into your Google Analytics data and generate insightful reports with just a few lines of code. Below, we’ll walk through the steps to install and use this package effectively.
Installation
Before you start, make sure you have Composer installed. You can install the Laravel Analytics package using Composer with the following command:
composer require spatie/laravel-analytics
Next, you can publish the configuration file for this package:
php artisan vendor:publish --tag=analytics-config
This will generate a config file located at config/analytics.php, which you will need to adjust for your project.
How to Obtain Google API Credentials
To access Google Analytics data, you first need to acquire the necessary API credentials. Here are the steps:
- Log in to your Google account and visit the Google API Console.
- Create a new project.
- Go to the API Library, find the Google Analytics Data API, and enable it.
- In the sidebar, click on “Credentials” and create a Service account key.
- Save the downloaded JSON file in the specified path of your config file.
Granting Permissions to Your Analytics Property
Now you need to give permissions to your service account on your Google Analytics property:
- Navigate to your Google Analytics account and go to Property Settings to find your property ID.
- Copy this property ID to the
ANALYTICS_PROPERTY_IDkey in your.envfile. - In the Admin section, go to Property Access Management and add the email from your service account with the Analyst role.
Usage Examples
With everything set up, you’re ready to start pulling analytics data. Here are some simple examples:
- To fetch the most visited pages over the last week:
use SpatieAnalytics\Facades\Analytics;
use SpatieAnalytics\Period;
Analytics::fetchMostVisitedPages(Period::days(7));
Analytics::fetchVisitorsAndPageViews(Period::days(7));
Analogous Explanation of the Code
Think of your analytics data as a library filled with books (the content of your site). Each time a visitor reads a book, it gets recorded in your library log. The Analytics::fetchMostVisitedPages(Period::days(7)); function requests a list of the most-read books (the pages) during the last week, while Analytics::fetchVisitorsAndPageViews(Period::days(7)); checks how many people visited the library and how often they read.
Troubleshooting
If you encounter issues while setting everything up, here are some tips:
- Ensure that the service account has the correct permissions in Google Analytics.
- Check that the property ID is correctly specified in your environment file.
- If the JSON file is not found, double-check the file path in the config.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
Using the Laravel Analytics package makes retrieving Google Analytics data a breeze, allowing you to focus on insights rather than data retrieval. If you have further questions or need assistance, refer to the integration documentation or contact support.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

