Connector for Google Analytics data extraction and analysis
google-analyticsThis package provides a Google Analytics GA4 v4 connector. It exposes a simple lifecycle and typed domain methods for common analytics data.
Go to the root directory of your project.
Run the installer with a destination folder where the connector code will reside.
bash -i <(curl https://registry.514.ai/install.sh) --dest app/connectors/google-analytics google-analytics v4 514-labs typescript default
From your project's root directory:
pnpm install
The connector supports three authentication methods. Choose the one that best fits your use case.
This is the recommended approach for production applications and server-side integrations.
client_email)Create a .env file in the connector directory:
cp .env.example .env
Edit .env and add your credentials from the downloaded JSON file:
GOOGLE_SERVICE_ACCOUNT_EMAIL=your-service-account@your-project.iam.gserviceaccount.com GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nYour private key here\n-----END PRIVATE KEY-----\n" GOOGLE_PROPERTY_ID=123456789
Use this method when you need to access analytics on behalf of a user.
Use the Google OAuth 2.0 Playground or implement the OAuth flow in your application.
GOOGLE_CLIENT_ID=your-client-id.apps.googleusercontent.com GOOGLE_CLIENT_SECRET=your-client-secret GOOGLE_REFRESH_TOKEN=your-refresh-token GOOGLE_PROPERTY_ID=123456789
Note: API keys have limited functionality with Google Analytics Data API. Service Account or OAuth is strongly recommended.
GOOGLE_API_KEY=your-api-key-here GOOGLE_PROPERTY_ID=123456789
import { createConnector } from '@514labs/connector-google-analytics'import dotenv from 'dotenv'dotenv.config()const conn = createConnector()conn.initialize({ baseUrl: 'https://analyticsdata.googleapis.com/v1beta', auth: { type: 'oauth2', oauth2: { grantType: 'jwt', email: process.env.GOOGLE_SERVICE_ACCOUNT_EMAIL!, privateKey: process.env.GOOGLE_PRIVATE_KEY!, tokenUrl: 'https://oauth2.googleapis.com/token', scope: 'https://www.googleapis.com/auth/analytics.readonly' } }, logging: { enabled: true, level: 'info' },})// Fetch reportsfor await (const page of conn.reports.getAll({ pageSize: 100, maxItems: 500})) { console.log(`Received ${page.length} reports`) console.log(page)}import { createConnector } from '@514labs/connector-google-analytics'import dotenv from 'dotenv'dotenv.config()const conn = createConnector()conn.initialize({ baseUrl: 'https://analyticsdata.googleapis.com/v1beta', auth: { type: 'oauth2', oauth2: { grantType: 'refresh_token', clientId: process.env.GOOGLE_CLIENT_ID!, clientSecret: process.env.GOOGLE_CLIENT_SECRET!, refreshToken: process.env.GOOGLE_REFRESH_TOKEN!, tokenUrl: 'https://oauth2.googleapis.com/token' } }, logging: { enabled: true, level: 'info' },})The connector includes a basic example file. Update it with your credentials:
# Copy the example environment file cp .env.example .env # Edit .env with your credentials # Then run the example pnpm tsx examples/basic-usage.ts
Make sure your service account or OAuth user has at least Viewer access to the GA4 property in Google Analytics.
Verify that your GOOGLE_PROPERTY_ID is correct. It should be a numeric ID (e.g., 123456789), not a measurement ID (e.g., G-XXXXXXXXXX).
For service accounts, ensure the private key is properly formatted with escaped newlines (\n). The key should be enclosed in double quotes in the .env file.
Make sure you've enabled the Google Analytics Data API in your Google Cloud project.