feat: group events by environmentId in posthog (#2036)

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
This commit is contained in:
Shubham Palriwala
2024-02-12 15:41:05 +05:30
committed by GitHub
parent 07f28d0971
commit 14b162aabc
19 changed files with 36 additions and 993 deletions
+4 -6
View File
@@ -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);
-71
View File
@@ -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,
}
)();