mirror of
https://github.com/formbricks/formbricks.git
synced 2026-04-27 07:34:47 -05:00
feat: group events by environmentId in posthog (#2036)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
This commit is contained in:
committed by
GitHub
parent
07f28d0971
commit
14b162aabc
@@ -5,10 +5,9 @@ const enabled =
|
||||
process.env.NEXT_PUBLIC_POSTHOG_API_HOST &&
|
||||
process.env.NEXT_PUBLIC_POSTHOG_API_KEY;
|
||||
|
||||
export const capturePosthogEvent = async (
|
||||
userId: string,
|
||||
export const capturePosthogEnvironmentEvent = async (
|
||||
environmentId: string,
|
||||
eventName: string,
|
||||
teamId?: string,
|
||||
properties: any = {}
|
||||
) => {
|
||||
if (
|
||||
@@ -23,12 +22,11 @@ export const capturePosthogEvent = async (
|
||||
host: process.env.NEXT_PUBLIC_POSTHOG_API_HOST,
|
||||
});
|
||||
client.capture({
|
||||
distinctId: environmentId,
|
||||
event: eventName,
|
||||
distinctId: userId,
|
||||
groups: teamId ? { company: teamId } : {},
|
||||
groups: { environment: environmentId },
|
||||
properties,
|
||||
});
|
||||
|
||||
await client.shutdownAsync();
|
||||
} catch (error) {
|
||||
console.error("error sending posthog event:", error);
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import "server-only";
|
||||
|
||||
import { Prisma } from "@prisma/client";
|
||||
import { unstable_cache } from "next/cache";
|
||||
|
||||
import { prisma } from "@formbricks/database";
|
||||
import { ZId } from "@formbricks/types/environment";
|
||||
import { DatabaseError, ResourceNotFoundError } from "@formbricks/types/errors";
|
||||
|
||||
import { SERVICES_REVALIDATION_INTERVAL } from "../constants";
|
||||
import { teamCache } from "../team/cache";
|
||||
import { validateInputs } from "../utils/validate";
|
||||
|
||||
export const getTeamDetails = async (
|
||||
environmentId: string
|
||||
): Promise<{ teamId: string; teamOwnerId: string | undefined }> =>
|
||||
unstable_cache(
|
||||
async () => {
|
||||
validateInputs([environmentId, ZId]);
|
||||
|
||||
try {
|
||||
const environment = await prisma.environment.findUnique({
|
||||
where: {
|
||||
id: environmentId,
|
||||
},
|
||||
select: {
|
||||
product: {
|
||||
select: {
|
||||
team: {
|
||||
select: {
|
||||
id: true,
|
||||
memberships: {
|
||||
select: {
|
||||
userId: true,
|
||||
role: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!environment) {
|
||||
throw new ResourceNotFoundError("Environment", environmentId);
|
||||
}
|
||||
|
||||
const teamId: string = environment.product.team.id;
|
||||
// find team owner
|
||||
const teamOwnerId: string | undefined = environment.product.team.memberships.find(
|
||||
(m) => m.role === "owner"
|
||||
)?.userId;
|
||||
|
||||
return {
|
||||
teamId: teamId,
|
||||
teamOwnerId: teamOwnerId,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
throw new DatabaseError(error.message);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
[`getTeamDetails-${environmentId}`],
|
||||
{
|
||||
tags: [teamCache.tag.byEnvironmentId(environmentId)],
|
||||
revalidate: SERVICES_REVALIDATION_INTERVAL,
|
||||
}
|
||||
)();
|
||||
Reference in New Issue
Block a user