GA Analytics
docs

Google Cloud Setup

Set up a Google Cloud service account to securely connect your GA4 property to the plugin.

Why a service account? The plugin uses a service account (not OAuth) to access GA4 data. This means no user login flow is needed — the plugin authenticates automatically using JWT credentials stored in environment variables.

Step 1: Create a Google Cloud Project

If you don't already have one, create a Google Cloud project:

1Go to the Google Cloud Console (console.cloud.google.com)
2Click "Select a project" → "New Project"
3Enter a project name (e.g., "My Website Analytics")
4Click "Create"

Step 2: Enable the Google Analytics Data API

1In Google Cloud Console, go to "APIs & Services" → "Library"
2Search for "Google Analytics Data API"
3Click on it and press "Enable"

Important: Make sure you enable the Google Analytics Data API (v1beta), not the older “Google Analytics API” or “Google Analytics Reporting API”.

Step 3: Create a Service Account

1Go to "IAM & Admin" → "Service Accounts"
2Click "+ Create Service Account"
3Enter a name (e.g., "sanity-analytics") and description
4Click "Create and Continue" (skip optional role assignment)
5Click "Done"

Step 4: Generate a JSON Key

1Click on the service account you just created
2Go to the "Keys" tab
3Click "Add Key" → "Create new key"
4Select "JSON" and click "Create"
5A JSON file will be downloaded — keep it safe!

The JSON key file contains the credentials you'll need for the environment variables:

service-account-key.json
{
  "type": "service_account",
  "project_id": "your-project-id",
  "private_key_id": "abc123...",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEv...\n-----END PRIVATE KEY-----\n",
  "client_email": "sanity-analytics@your-project.iam.gserviceaccount.com",
  "client_id": "123456789",
  ...
}

Step 5: Add Service Account to GA4 Property

1Go to Google Analytics (analytics.google.com)
2Navigate to Admin (gear icon) → select your property
3Under "Property", click "Property Access Management"
4Click the "+" button to add a new user
5Enter the service account email (e.g., sanity-analytics@your-project.iam.gserviceaccount.com)
6Set the role to "Viewer" (read-only access is sufficient)
7Click "Add"

Step 6: Find Your GA4 Property ID

You need the numeric Property ID (not the Measurement ID like G-XXXXXXX):

1In Google Analytics, go to Admin → Property Settings
2Look for "Property ID" — it's a numeric value (e.g., 123456789)
3Copy this value for your GA_PROPERTY_ID environment variable

Note: The Property ID is a numeric value like 123456789. Don't confuse it with the Measurement ID (like G-XXXXXXXXX) which is used for frontend tracking scripts.

Next step: Now set up the environment variables → using the values from your service account JSON key.