- How do I install spatie/laravel-analytics in a Laravel app?
- Run composer require spatie/laravel-analytics. Laravel package discovery registers the service provider automatically. Optionally publish the config file with php artisan vendor:publish --tag=analytics-config, then set your GA4 property ID and service-account credentials.
- Does spatie/laravel-analytics work with Google Analytics 4?
- Yes. The package is built around the GA4 Data API and its convenience methods wrap GA4 runReport calls, returning results as Laravel collections. It is designed for GA4 properties, and the configured property_id is a GA4 property ID.
- How do I configure the Google service account and property ID for spatie/laravel-analytics?
- Publish the config file, set property_id to your GA4 property, and point the service-account JSON path at a key file stored outside the repository. In Google Cloud, enable the Analytics Data API and grant the service account's client_email the Analyst role on the GA4 property. These GCP provisioning steps are manual and usually owned by whoever manages the Google account.
- Can I test Laravel analytics code without calling Google with spatie/laravel-analytics?
- Yes. Analytics::fake() lets you swap the client for a fake, optionally with fixture arrays, so feature tests can assert analytics screens without network access. This avoids the usual pain of mocking Google SDKs in CI.
- Does spatie/laravel-analytics support multiple GA4 properties or multi-tenant apps?
- Not natively. property_id is a single global config value, so multi-tenant or multi-property use requires per-request config mutation or binding a second client instance. That is a design smell and generally means wrapping the package in your own service layer.
- How does caching work in spatie/laravel-analytics?
- The config file exposes a cache lifetime and cache store, and the package caches API results accordingly. Tune the lifetime to your data freshness SLA; a 24-hour cache is fine for some dashboards but not for near-real-time reporting.
- What are the limitations of spatie/laravel-analytics for historical data and reporting?
- The GA4 Data API returns aggregated data, not raw events. GA4 retention windows (14 months standard) and cardinality thresholding, where high-entropy dimensions collapse into (other), limit what the package can answer. For multi-year history, joins, or reproducibility, materialize GA4 data into your own warehouse and treat this package as an extraction tool.
- Will spatie/laravel-analytics conflict with other Google Cloud or protobuf packages?
- The package pulls google/analytics-data, which brings protobuf/gRPC and Guzzle. If your app also uses Google Cloud packages such as BigQuery, Cloud Storage, or Vertex, protobuf version pinning can cause composer update breakage. Test upgrades in a branch and pin compatible versions.
- Which Laravel versions does spatie/laravel-analytics support?
- The package is a Laravel package installed via Composer, but exact Laravel and PHP version constraints live in its composer.json. Check the require section on Packagist or the GitHub releases page before upgrading Laravel. That is the authoritative source for framework compatibility.
- Is spatie/laravel-analytics maintained and production-ready?
- It has strong real-world signals: about 3.2k GitHub stars, a mature README, CHANGELOG, and releases. It is a focused utility with community-only support and no vendor SLA, and advanced use sits on Google's beta Analytics Data API surface. Many teams run it in production as a presentation adapter, but wrap it if analytics data touches business logic.