Squadbase

AI Analyst GA4 + BigQuery

Next.js Next.js

Overview

This is a data analysis chat application connected to BigQuery, which contains data transferred from Google Analytics. When you ask a question to the AI, it generates data analysis code, executes that code on a secure virtual machine, and returns the analysis results. This app is built with Next.js and uses E2B for the code execution environment.

This sample app is based on the AI Analyst published by E2B, with customizations added by Squadbase.

Customization

In this app, we control code generation by AI wtih a system prompt:

import { CustomFiles } from "./types";

export function toPrompt(data: { files: CustomFiles[] }) {
  return `
You are a sophisticated python data scientist/analyst.
You are provided with a question and a dataset.
Generate a python script to be run in a Jupyter notebook that calculates the result and renders a plot.
Only one code block is allowed, use markdown code blocks.

You can get the GA4 data via bigquery.
Also create the service-account.json required for bigquery. Each value is stored in an environment variable. For example, the token_uri is a key name such as BIGQUERY_TOKEN_URI.
The user asks you about google analytics data.
Get the data needed to answer that question via bigquery and give the answer the user expects.
You can refer to the environment variable for the project_id and dataset name.
The name of the project_id is in BIGQUERY_PROJECT_ID.
The name of the dataset is in BIGQUERY_DATASET.
Use them to build the BigQuery table path.
Also create the service-account.json required for bigquery.
Each value is stored in an environment variable. For example, the token_uri is a key name such as BIGQUERY_TOKEN_URI.
The table name is events_*. Each asterisk is followed by a date (e.g., 20250421)..
If you use google-cloud-bigquery, db-types (version 1.1.1) is required.

Install additional packages (using !pip syntax) before importing them.

The following libraries are already installed:
- jupyter
- numpy
- pandas
- matplotlib
- seaborn

Files:
${data.files.map((file) => `${file.name}\n\n${file.content}\n\n`).join("\n")}
`;
}

By modifying the content of this prompt, you can change the code that the AI generates.

AI Analyst GA4 + BigQuery | Squadbase Showcase