# 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)

1. Go to [Google Analytics](https://analytics.google.com)
2. Sign in to your Google account
3. Click "Admin" (gear icon) in the bottom left
4. Click "Create" → "Property"
5. Enter your property name (e.g., "My Business Website")
6. Select your reporting time zone and currency
7. Click "Next" and complete the setup

#### 1.2 Set Up Data Stream

1. After creating the property, you'll be prompted to set up a data stream
2. Select "Web" for website integration
3. Enter your website URL
4. Give your stream a name
5. Click "Create stream"

### Step 2: Get Your Measurement ID

1. In your GA4 property, go to "Admin" → "Data Streams"
2. Click on your web data stream
3. Your **Measurement ID** will be displayed at the top (format: `G-XXXXXXXXX`)
4. Copy this value for <mark style="color:red;">**Measurement Id**</mark>

### Step 3: Get Your Property ID

1. In Google Analytics 4, select your property
2. Click the gear icon (⚙️) at the bottom left to open Admin settings
3. In the Property column (middle section), click "Property Settings"
4. Your **Property ID** will be displayed at the top in numeric format (e.g., `123456789`)
5. Copy this value for <mark style="color:red;">**Property ID**</mark>

### Step 4: Set Up Google Cloud Project and Service Account

#### 4.1 Create Google Cloud Project

1. Go to [Google Cloud Console](https://console.cloud.google.com)
2. Sign in with your Google account
3. Create a new project or select an existing one

#### 4.2 Enable Google Analytics Data API

1. In Google Cloud Console, go to "APIs and services" → "Library"
2. Search for "Google Analytics Data API"
3. Click on it and click "Enable"
4. Wait for the API to be enabled

#### 4.3 Create Service Account

1. Go to "IAM & Admin" → "Service Accounts"
2. Click "Create Service Account"
3. Enter a name (e.g., "analytics-api-service")
4. Add a description (optional)
5. Click "Create and Continue"
6. Skip role assignment for now (click "Continue")
7. Click "Done"

#### 4.4 Generate Service Account Key

1. Find your newly created service account in the list
2. Click on the service account email
3. Go to the "Keys" tab
4. Click "Add Key" → "Create new key"
5. Select "JSON" format
6. Click "Create"
7. 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 <mark style="color:red;">**Client Email**</mark>
* `private_key` → This is your <mark style="color:red;">**Private Key**</mark>

**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

1. Go back to [Google Analytics](https://analytics.google.com)
2. Select your GA4 property
3. Click "Admin" (gear icon)
4. In the Account column, click "Account Access Management"
5. Click the "+" (plus) icon to add a user
6. Enter your service account email (from the JSON file: `your-service@project-id.iam.gserviceaccount.com`)
7. Select permissions:
   * **Viewer**: For read-only access.
8. Click "Add"

## **Step 6: Final Setup Completion**

⏱️ <mark style="color:orange;">**Important**</mark>**:** 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.
