Spatie Laravel Analytics is a package that integrates Google Analytics into your Laravel applications. This package allows you to retrieve analytics data easily and provides a fluent interface for working with Google Analytics data in your Laravel application.
Key Features of Spatie Laravel Analytics:
- Easy Integration: Simple setup process for connecting to Google Analytics.
- Fluent API: Provides a fluent interface for querying Google Analytics data.
- Multiple Views: Supports multiple Google Analytics views for managing analytics across different sections of your application.
- Data Types: Access a variety of data types, including sessions, users, page views, and more.
- Caching: Caches results to improve performance and reduce the number of API requests.
Installation
To get started with Spatie Laravel Analytics, follow these steps:
- Require the Package: Install the package via Composer by running:
1composer require spatie/laravel-analytics - Publish the Configuration: Publish the configuration file using the following command:
1php artisan vendor:publish --provider="Spatie\Analytics\AnalyticsServiceProvider" - Set Up the Configuration: The configuration file will be located at
config/analytics.php
. In this file, you need to specify your Google Analytics view ID and the path to your service account credentials JSON file. Here’s an example configuration:
1234return ['view_id' => env('GOOGLE_ANALYTICS_VIEW_ID'),'service_account_credentials' => storage_path('app/google-analytics-service-account.json'),]; - Add Google Analytics Credentials: Create a Google Cloud project and enable the Google Analytics API. Generate service account credentials and download the JSON file. Place this file in the specified directory (e.g.,
storage/app/google-analytics-service-account.json
). - Set Environment Variables: In your
.env
file, add the following environment variable:
1GOOGLE_ANALYTICS_VIEW_ID=YOUR_VIEW_ID
Querying Analytics Data
Once the package is installed and configured, you can start querying Google Analytics data. Here’s how to get started:
- Setting Up a Controller: Create a controller to fetch and display analytics data:
1php artisan make:controller AnalyticsController - Fetching Analytics Data: In the
AnalyticsController
, use theAnalytics
facade to retrieve data:
1234567891011121314151617181920212223namespace App\Http\Controllers;use Spatie\Analytics\Analytics;use Spatie\Analytics\AnalyticsClient;use Illuminate\Http\Request;class AnalyticsController extends Controller{protected $analytics;public function __construct(Analytics $analytics){$this->analytics = $analytics;}public function index(){// Fetching analytics data$analyticsData = $this->analytics->fetchTotalVisitorsAndPageViews(30);return view('analytics.index', compact('analyticsData'));}} - Creating a View: Create a view file at
resources/views/analytics/index.blade.php
to display the data:
12345678910111213141516171819202122232425@extends('layouts.app')@section('content')<div class="container"><h1>Analytics Data</h1><table class="table"><thead><tr><th>Date</th><th>Visitors</th><th>Page Views</th></tr></thead><tbody>@foreach ($analyticsData as $date => $data)<tr><td>{{ $date }}</td><td>{{ $data['visitors'] }}</td><td>{{ $data['pageViews'] }}</td></tr>@endforeach</tbody></table></div>@endsection - Defining Routes: Add a route in
routes/web.php
to access the analytics page:
123use App\Http\Controllers\AnalyticsController;Route::get('/analytics', [AnalyticsController::class, 'index'])->name('analytics.index');
Accessing Other Data Types
The Spatie Laravel Analytics package provides various methods for accessing different types of data, such as:
- Total Visitors and Page Views:
1$totalVisitorsAndPageViews = $this->analytics->fetchTotalVisitorsAndPageViews(30); - Most Visited Pages:
1$mostVisitedPages = $this->analytics->fetchMostVisitedPages(30); - User Demographics:
1$demographics = $this->analytics->fetchUserDemographics();
Conclusion
Spatie Laravel Analytics is an excellent tool for integrating Google Analytics into your Laravel applications. With its fluent API and simple setup, you can quickly access and display your analytics data, helping you make informed decisions based on user behavior.
Additional Considerations
- Documentation: For more detailed information, configuration options, and advanced usage, refer to the official Spatie Laravel Analytics documentation.
- Caching: The package provides built-in caching options to help manage API requests efficiently.