Environment Variables
Configure the required environment variables for the Google Analytics API connection.
Required Variables
# .env.local
# Your GA4 Property ID (numeric, NOT the Measurement ID like G-XXXXXXX)
GA_PROPERTY_ID=123456789
# Service account email from your Google Cloud JSON key
GA_SERVICE_ACCOUNT_EMAIL=sanity-analytics@your-project.iam.gserviceaccount.com
# Private key from your Google Cloud JSON key (keep the quotes and \n characters)
GA_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBg...\n-----END PRIVATE KEY-----\n"Variable Reference
GA_PROPERTY_IDRequiredThe numeric GA4 Property ID. Find it in Google Analytics → Admin → Property Settings.
505668883This is the Property ID (numeric), not the Measurement ID (like G-XXXXXXXXX). The Measurement ID is used for frontend tracking scripts — the Property ID is what the Data API needs.
GA_SERVICE_ACCOUNT_EMAILRequiredThe client_email field from your service account JSON key file.
sanity-analytics@my-project.iam.gserviceaccount.comGA_PRIVATE_KEYRequiredThe private_key field from your service account JSON key file. Must be wrapped in double quotes and include the \n characters.
"-----BEGIN PRIVATE KEY-----\nMIIEv...\n-----END PRIVATE KEY-----\n"Where to Get the Values
Open the JSON key file you downloaded during Google Cloud Setup. Map the fields as follows:
// From your downloaded JSON key file:
{
"client_email": "...", → GA_SERVICE_ACCOUNT_EMAIL
"private_key": "...", → GA_PRIVATE_KEY
}
// From Google Analytics Admin:
// Property Settings → Property ID → GA_PROPERTY_IDDeployment
For production deployments, add these environment variables to your hosting platform:
Security: Never commit your .env.local file to version control. Make sure it's listed in your .gitignore. The private key is a sensitive credential that grants access to your analytics data.