feat: send free limit reached to PostHog (#2014)

Co-authored-by: Matti Nannt <mail@matthiasnannt.com>
This commit is contained in:
Shubham Palriwala
2024-02-06 15:25:09 +05:30
committed by GitHub
parent 9c90c9faf1
commit 6a9968bd97
3 changed files with 33 additions and 0 deletions

View File

@@ -1,3 +1,4 @@
import { sendFreeLimitReachedEventToPosthogBiWeekly } from "@/app/api/v1/client/[environmentId]/in-app/sync/lib/posthog";
import { responses } from "@/app/lib/api/response";
import { transformErrorToDetails } from "@/app/lib/api/validator";
import { NextResponse } from "next/server";
@@ -95,6 +96,7 @@ export async function GET(
person = await createPerson(environmentId, userId);
}
} else {
sendFreeLimitReachedEventToPosthogBiWeekly(environmentId, "userTargeting");
const errorMessage = `Monthly Active Users limit in the current plan is reached in ${environmentId}`;
if (!person) {
// if it's a new person and MAU limit is reached, throw an error
@@ -107,6 +109,9 @@ export async function GET(
}
}
}
if (isInAppSurveyLimitReached) {
sendFreeLimitReachedEventToPosthogBiWeekly(environmentId, "inAppSurvey");
}
const [surveys, noCodeActionClasses, product] = await Promise.all([
getSyncSurveys(environmentId, person),

View File

@@ -0,0 +1,24 @@
import { unstable_cache } from "next/cache";
import { capturePosthogEvent } from "@formbricks/lib/posthogServer";
import { getTeamDetails } from "@formbricks/lib/teamDetail/service";
export const sendFreeLimitReachedEventToPosthogBiWeekly = async (
environmentId: string,
plan: "inAppSurvey" | "userTargeting"
) => {
await unstable_cache(
async () => {
const teamDetails = await getTeamDetails(environmentId);
if (teamDetails?.teamOwnerId) {
return await capturePosthogEvent(teamDetails.teamOwnerId, "free limit reached", teamDetails.teamId, {
plan,
});
}
},
[`posthog-${plan}-limitReached-${environmentId}`],
{
revalidate: 60 * 60 * 24 * 15, // 15 days
}
)();
};

View File

@@ -1,3 +1,4 @@
import { sendFreeLimitReachedEventToPosthogBiWeekly } from "@/app/api/v1/client/[environmentId]/in-app/sync/lib/posthog";
import { responses } from "@/app/lib/api/response";
import { transformErrorToDetails } from "@/app/lib/api/validator";
import { NextRequest, NextResponse } from "next/server";
@@ -56,6 +57,9 @@ export async function GET(
const currentResponseCount = await getMonthlyTeamResponseCount(team.id);
isInAppSurveyLimitReached =
!hasInAppSurveySubscription && currentResponseCount >= PRICING_APPSURVEYS_FREE_RESPONSES;
if (isInAppSurveyLimitReached) {
sendFreeLimitReachedEventToPosthogBiWeekly(environmentId, "inAppSurvey");
}
}
if (!environment?.widgetSetupCompleted) {