Open source alternative to Semrush and Ahrefs
TypeScript backlink-analysisgoogle-search-console-mcpkeyword-researchmcpseoseo-agentseo-auditseo-automationseo-skillsseo-toolssite-audit
import { and, eq } from "drizzle-orm";
import { db } from "@/db";
import { account } from "@/db/schema";
import { AppError } from "@/server/lib/errors";
import { createGa4AdminClient } from "@/server/lib/ga4Client";
import { Ga4AdminApiError, Ga4TokenError } from "@/server/lib/ga4Errors";
import { GA4_OAUTH_PROVIDER_ID } from "@/shared/ga4";
import { getGoogleAccessMode } from "@/server/features/google/googleAccess";
import {
GATEWAY_GOOGLE_ACCOUNT_ID,
getGatewayGoogleStatus,
} from "@/server/features/google/gatewayGoogle";
import {
Ga4ConnectionRepository,
type Ga4Connection,
} from "@/server/features/ga4/repositories/Ga4ConnectionRepository";
async function getConnection(projectId: string): Promise<Ga4Connection | null> {
return Ga4ConnectionRepository.getByProjectId(projectId);
}
/** Behind the gateway a member's one account is their gateway connection (gatewayGoogle.ts). */
async function listGrantsForUser(userId: string) {
if ((await getGoogleAccessMode()) === "gateway") {
return (await getGatewayGoogleStatus()).connected
? [
{
id: GATEWAY_GOOGLE_ACCOUNT_ID,
accountId: GATEWAY_GOOGLE_ACCOUNT_ID,
},
]
: [];
}
return db
.select({ id: account.id, accountId: account.accountId })
.from(account)
.where(
and(
eq(account.userId, userId),
eq(account.providerId, GA4_OAUTH_PROVIDER_ID),
),
);
}
async function userHasGrant(userId: string): Promise<boolean> {
const grants = await listGrantsForUser(userId);
return grants.length > 0;
}
function requiresReconnect(error: unknown): boolean {
return (
error instanceof Ga4TokenError ||
(error instanceof Ga4AdminApiError && error.status === 401)
);
}
async function listPropertiesForUserWithGrantStatus(userId: string) {
const grants = await listGrantsForUser(userId);
const accounts = await Promise.all(
grants.map(async (grant) => {
const client = createGa4AdminClient({
userId,
ga4AccountId: grant.accountId,
});
try {
const properties = await client.listProperties();
let email: string | null = null;
try {
email = await client.getUserInfoEmail();
} catch {
email = null;
}
return {
accountId: grant.accountId,
email,
requiresReconnect: false,
propertiesUnavailable: false,
properties,
};
} catch (error) {
const reconnect = requiresReconnect(error);
if (!reconnect) {
console.error("ga4.property_discovery_failed", {
errorName: error instanceof Error ? error.name : "UnknownError",
status:
error instanceof Ga4AdminApiError ? error.status : undefined,
});
}
return {
accountId: grant.accountId,
email: null,
requiresReconnect: reconnect,
propertiesUnavailable: !reconnect,
properties: [],
};
}
}),
);
return { accounts };
}
async function setProperty(input: {
projectId: string;
organizationId: string;
propertyId: string;
accountId: string;
userId: string;
}): Promise<Ga4Connection> {
const grants = await listGrantsForUser(input.userId);
if (!grants.some((grant) => grant.accountId === input.accountId)) {
throw new AppError(
"NOT_FOUND",
"That Google account isn't connected to your OpenSEO account.",
);
}
const client = createGa4AdminClient({
userId: input.userId,
ga4AccountId: input.accountId,
});
const properties = await client.listProperties();
if (
!properties.some((property) => property.propertyId === input.propertyId)
) {
throw new AppError(
"NOT_FOUND",
"That Google Analytics property isn't available on your connected Google account.",
);
}
const property = await client.getProperty(input.propertyId);
let connectedAccountEmail: string | null = null;
try {
connectedAccountEmail = await client.getUserInfoEmail();
} catch {
connectedAccountEmail = null;
}
return Ga4ConnectionRepository.upsert({
projectId: input.projectId,
organizationId: input.organizationId,
propertyId: property.name,
propertyDisplayName: property.displayName,
propertyTimeZone: property.timeZone,
propertyCurrencyCode: property.currencyCode,
connectedByUserId: input.userId,
ga4AccountId: input.accountId,
connectedAccountEmail,
});
}
async function disconnect(input: { projectId: string }): Promise<void> {
await Ga4ConnectionRepository.deleteByProjectId(input.projectId);
}
export const Ga4Service = {
getConnection,
userHasGrant,
listPropertiesForUserWithGrantStatus,
setProperty,
disconnect,
};