> For the complete documentation index, see [llms.txt](https://zakariaes-organization.gitbook.io/directoryeasy-nocode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zakariaes-organization.gitbook.io/directoryeasy-nocode/analytics.md).

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


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://zakariaes-organization.gitbook.io/directoryeasy-nocode/analytics.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
