Dashboard Analytics Setup
This guide will help you configure Google Analytics to display dashboard charts in your application.
Prerequisites
- A Google account
- Access to Google Cloud Console
- Access to Google Analytics
Step 1: Create a Google Cloud Project
- Go to Google Cloud Console
- Click Select a project → New Project
- Enter a project name (e.g., "My Analytics Dashboard")
- Click Create
Step 2: Enable Google Analytics Data API
- In your Google Cloud project, go to APIs & Services → Library
- Search for "Google Analytics Data API"
- Click on Google Analytics Data API
- Click Enable
Step 3: Create Service Account
- Go to APIs & Services → Credentials
- Click Create Credentials → Service Account
- Fill in the service account details:
- Service account name: e.g., "analytics-dashboard"
- Service account ID: will be auto-generated
- Description: Optional description
- Click Create and Continue
- Skip the optional steps (Grant users access to this service account)
- Click Done
Step 4: Create and Download Service Account Key
- In Credentials page, find your newly created service account
- Click on the service account email
- Go to the Keys tab
- Click Add Key → Create new key
- Select JSON as the key type
- Click Create
- The JSON file will be automatically downloaded to your computer
[!IMPORTANT] Keep this JSON file secure! It contains credentials that grant access to your Google Analytics data.
Step 5: Upload Service Account Credentials
- Rename the downloaded JSON file to
service-account-credentials.json - Place it in your Laravel application at:
storage/app/analytics/service-account-credentials.json - Make sure the directory exists:
mkdir -p storage/app/analytics
Step 6: Get Google Analytics Property ID
Method 1: Using Google Analytics Admin
- Go to Google Analytics
- Click Admin (gear icon in the bottom left)
- In the Property column, select your property
- Click Property Settings
- Copy the Property ID (starts with numbers, e.g.,
123456789)
Method 2: Using Google Analytics Data Stream
- In Google Analytics Admin, go to Data Streams
- Select your data stream (Web, iOS, or Android)
- You'll see the Measurement ID (e.g.,
G-XXXXXXXXXX) - The Property ID is shown at the top of the page
[!NOTE] The Property ID is different from the Measurement ID. Make sure you copy the correct Property ID (numeric format).
Step 7: Grant Service Account Access to Google Analytics
- In Google Analytics Admin, go to Property Access Management
- Click the + icon to add users
- Enter the service account email (find it in the downloaded JSON file, under
client_email)- Example:
analytics-dashboard@your-project.iam.gserviceaccount.com
- Example:
- Select role: Viewer (minimum required) or Analyst
- Click Add
Step 8: Configure Laravel Application
Option 1: Using Environment Variables (Recommended)
Add to your .env file:
ANALYTICS_PROPERTY_ID=123456789
The service account credentials path is already configured in config/analytics.php:
'service_account_credentials_json' => storage_path('app/analytics/service-account-credentials.json'),
Option 2: Using Array Credentials
Instead of a file path, you can pass credentials as an array. Edit config/analytics.php:
'service_account_credentials_json' => [
'type' => 'service_account',
'project_id' => 'your-project-id',
'private_key_id' => 'your-private-key-id',
'private_key' => '-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n',
'client_email' => 'your-service-account@your-project.iam.gserviceaccount.com',
'client_id' => 'your-client-id',
'auth_uri' => 'https://accounts.google.com/o/oauth2/auth',
'token_uri' => 'https://oauth2.googleapis.com/token',
'auth_provider_x509_cert_url' => 'https://www.googleapis.com/oauth2/v1/certs',
'client_x509_cert_url' => 'your-cert-url',
],
[!WARNING] If using array credentials, make sure to keep your
.envfile secure and never commit it to version control.
Step 9: Verify Configuration
Check File Permissions
Ensure the JSON file is readable by your web server:
chmod 644 storage/app/analytics/service-account-credentials.json
Test the Connection
Create a test route or command to verify the setup:
use Spatie\Analytics\Analytics;
use Spatie\Analytics\Period;
$analytics = app(Analytics::class);
$analyticsData = $analytics->fetchTotalVisitorsAndPageViews(Period::days(7));
dd($analyticsData);
If configured correctly, you should see analytics data returned.
Troubleshooting
Error: "Service account credentials file not found"
- Verify the file path is correct
- Check file permissions
- Ensure the directory exists
Error: "User does not have sufficient permissions"
- Make sure you granted the service account access in Google Analytics (Step 7)
- Wait a few minutes for permissions to propagate
- Verify you're using the correct Property ID
Error: "API has not been used in project"
- Enable the Google Analytics Data API in your Google Cloud project (Step 2)
- Wait a few minutes for the API to be fully enabled
Error: "Invalid property ID"
- Double-check the Property ID in Google Analytics
- Ensure you're using the Property ID, not the Measurement ID
- Property IDs are numeric (e.g.,
123456789), not alphanumeric (e.g.,G-XXXXXXXXXX)
Cache Configuration
Analytics responses are cached by default for 24 hours. You can adjust this in config/analytics.php:
'cache_lifetime_in_minutes' => 60 * 24, // 24 hours
To disable caching:
'cache_lifetime_in_minutes' => 0,
Security Best Practices
-
Never commit credentials to version control
- Add
storage/app/analytics/*.jsonto.gitignore
- Add
-
Restrict file permissions
chmod 600 storage/app/analytics/service-account-credentials.json -
Use environment variables
- Store sensitive configuration in
.envfile
- Store sensitive configuration in
-
Grant minimal permissions
- Service account only needs Viewer role in Google Analytics
-
Rotate keys regularly
- Create new service account keys periodically
- Delete old keys from Google Cloud Console