GA Analytics
docs

Environment Variables

Configure the required environment variables for the Google Analytics API connection.

Required Variables

.env.local
# .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_IDRequired

The numeric GA4 Property ID. Find it in Google Analytics → Admin → Property Settings.

Example: 505668883

This 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_EMAILRequired

The client_email field from your service account JSON key file.

Example: sanity-analytics@my-project.iam.gserviceaccount.com
GA_PRIVATE_KEYRequired

The private_key field from your service account JSON key file. Must be wrapped in double quotes and include the \n characters.

Format: "-----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:

Field mapping
// 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_ID

Deployment

For production deployments, add these environment variables to your hosting platform:

VercelSettings → Environment Variables → add all three variables
NetlifySite settings → Build & deploy → Environment → add variables
Railway / RenderService → Variables → add all three variables

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.