Analytics
Complete Guide: Google Analytics 4 API Integration
This comprehensive guide will walk you through setting up Google Analytics 4 (GA4) API integration for your directory, including obtaining all necessary credentials .
Required Environment Variables
You'll need to configure these four variables:
Measurement ID
Client Email
Private Key
Property ID
Step 1: Set Up Google Analytics 4 Property
1.1 Create GA4 Property (if not already done)
Go to Google Analytics
Sign in to your Google account
Click "Admin" (gear icon) in the bottom left
Click "Create" → "Property"
Enter your property name (e.g., "My Business Website")
Select your reporting time zone and currency
Click "Next" and complete the setup
1.2 Set Up Data Stream
After creating the property, you'll be prompted to set up a data stream
Select "Web" for website integration
Enter your website URL
Give your stream a name
Click "Create stream"
Step 2: Get Your Measurement ID
In your GA4 property, go to "Admin" → "Data Streams"
Click on your web data stream
Your Measurement ID will be displayed at the top (format:
G-XXXXXXXXX
)Copy this value for Measurement Id
Step 3: Get Your Property ID
In Google Analytics 4, select your property
Click the gear icon (⚙️) at the bottom left to open Admin settings
In the Property column (middle section), click "Property Settings"
Your Property ID will be displayed at the top in numeric format (e.g.,
123456789
)Copy this value for Property ID
Step 4: Set Up Google Cloud Project and Service Account
4.1 Create Google Cloud Project
Go to Google Cloud Console
Sign in with your Google account
Create a new project or select an existing one
4.2 Enable Google Analytics Data API
In Google Cloud Console, go to "APIs and services" → "Library"
Search for "Google Analytics Data API"
Click on it and click "Enable"
Wait for the API to be enabled
4.3 Create Service Account
Go to "IAM & Admin" → "Service Accounts"
Click "Create Service Account"
Enter a name (e.g., "analytics-api-service")
Add a description (optional)
Click "Create and Continue"
Skip role assignment for now (click "Continue")
Click "Done"
4.4 Generate Service Account Key
Find your newly created service account in the list
Click on the service account email
Go to the "Keys" tab
Click "Add Key" → "Create new key"
Select "JSON" format
Click "Create"
A JSON file will download automatically - keep this file secure!
4.5 Extract Credentials from JSON File
Open the downloaded JSON file and locate these values:
client_email
→ This is your Client Emailprivate_key
→ This is your Private Key
Important: When using the private key in your environment variables, make sure to:
Keep the
\n
characters as literal\n
(don't convert to actual line breaks)Include the full key including
-----BEGIN PRIVATE KEY-----
and-----END PRIVATE KEY-----
Step 5: Grant Service Account Access to GA4
Go back to Google Analytics
Select your GA4 property
Click "Admin" (gear icon)
In the Account column, click "Account Access Management"
Click the "+" (plus) icon to add a user
Enter your service account email (from the JSON file:
your-service@project-id.iam.gserviceaccount.com
)Select permissions:
Viewer: For read-only access.
Click "Add"
Step 6: Final Setup Completion
⏱️ Important: Wait for configuration to propagate. - Google Analytics permissions and API access can take a few minutes to fully activate. - If you keep getting the same error even after waiting, contact us and we will help you fix it.
Common mistake:
1 - Make sure you use the PROPERTY ID, not the ACCOUNT ID.
Last updated