diff --git a/.env.example b/.env.example index 1f957d7445..b2a1549529 100644 --- a/.env.example +++ b/.env.example @@ -137,8 +137,8 @@ NEXT_PUBLIC_SENDGRID_SENDER_NAME= NEXT_PUBLIC_SENTRY_DSN= # Formbricks Experience Management Integration -FORMBRICKS_HOST_URL=https://app.formbricks.com -FORMBRICKS_ENVIRONMENT_ID= +NEXT_PUBLIC_FORMBRICKS_HOST_URL=https://app.formbricks.com +NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID= FORMBRICKS_FEEDBACK_SURVEY_ID= # AvatarAPI diff --git a/packages/features/ee/organizations/pages/components/OtherTeamList.tsx b/packages/features/ee/organizations/pages/components/OtherTeamList.tsx index 8ad43d9e8c..4f72b90c24 100644 --- a/packages/features/ee/organizations/pages/components/OtherTeamList.tsx +++ b/packages/features/ee/organizations/pages/components/OtherTeamList.tsx @@ -1,5 +1,6 @@ import { useState } from "react"; +import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; import type { RouterOutputs } from "@calcom/trpc/react"; import { trpc } from "@calcom/trpc/react"; import { showToast } from "@calcom/ui"; @@ -27,6 +28,7 @@ export default function OtherTeamList(props: Props) { const deleteTeamMutation = trpc.viewer.organizations.deleteTeam.useMutation({ async onSuccess() { await utils.viewer.organizations.listOtherTeams.invalidate(); + trackFormbricksAction("team_disbanded"); }, async onError(err) { showToast(err.message, "error"); diff --git a/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx b/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx index ce17bb8403..f6ea86f628 100644 --- a/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx +++ b/packages/features/ee/organizations/pages/settings/other-team-profile-view.tsx @@ -11,6 +11,7 @@ import { z } from "zod"; import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants"; import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage"; +import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { useParamsWithFallback } from "@calcom/lib/hooks/useParamsWithFallback"; import { md } from "@calcom/lib/markdownIt"; @@ -126,6 +127,7 @@ const OtherTeamProfileView = () => { await utils.viewer.organizations.listOtherTeams.invalidate(); showToast(t("your_team_disbanded_successfully"), "success"); router.push(`${WEBAPP_URL}/teams`); + trackFormbricksAction("team_disbanded"); }, }); @@ -324,7 +326,9 @@ const OtherTeamProfileView = () => { variety="danger" title={t("disband_team")} confirmBtnText={t("confirm_disband_team")} - onConfirm={deleteTeam}> + onConfirm={() => { + deleteTeam(); + }}> {t("disband_team_confirmation_message")} diff --git a/packages/features/ee/teams/components/TeamInviteList.tsx b/packages/features/ee/teams/components/TeamInviteList.tsx index ba4908f149..04976aff55 100644 --- a/packages/features/ee/teams/components/TeamInviteList.tsx +++ b/packages/features/ee/teams/components/TeamInviteList.tsx @@ -1,5 +1,6 @@ import { useState } from "react"; +import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; import type { MembershipRole } from "@calcom/prisma/enums"; import { trpc } from "@calcom/trpc/react"; import { showToast } from "@calcom/ui"; @@ -36,6 +37,7 @@ export default function TeamInviteList(props: Props) { await utils.viewer.teams.list.invalidate(); await utils.viewer.teams.get.invalidate(); await utils.viewer.organizations.listMembers.invalidate(); + trackFormbricksAction("team_disbanded"); }, async onError(err) { showToast(err.message, "error"); diff --git a/packages/features/ee/teams/components/TeamList.tsx b/packages/features/ee/teams/components/TeamList.tsx index 4f31e64ed2..3fb86e34c5 100644 --- a/packages/features/ee/teams/components/TeamList.tsx +++ b/packages/features/ee/teams/components/TeamList.tsx @@ -1,5 +1,6 @@ import { useState } from "react"; +import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { RouterOutputs } from "@calcom/trpc/react"; import { trpc } from "@calcom/trpc/react"; @@ -32,6 +33,7 @@ export default function TeamList(props: Props) { async onSuccess() { await utils.viewer.teams.list.invalidate(); await utils.viewer.teams.hasTeamPlan.invalidate(); + trackFormbricksAction("team_disbanded"); }, async onError(err) { showToast(err.message, "error"); diff --git a/packages/features/ee/teams/pages/team-profile-view.tsx b/packages/features/ee/teams/pages/team-profile-view.tsx index 06dd2c8d55..baacdb347e 100644 --- a/packages/features/ee/teams/pages/team-profile-view.tsx +++ b/packages/features/ee/teams/pages/team-profile-view.tsx @@ -12,6 +12,7 @@ import { z } from "zod"; import SectionBottomActions from "@calcom/features/settings/SectionBottomActions"; import { IS_TEAM_BILLING_ENABLED, WEBAPP_URL } from "@calcom/lib/constants"; import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage"; +import { trackFormbricksAction } from "@calcom/lib/formbricks-client"; import { getTeamUrlSync } from "@calcom/lib/getBookerUrl/client"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { useParamsWithFallback } from "@calcom/lib/hooks/useParamsWithFallback"; @@ -124,6 +125,7 @@ const ProfileView = () => { await utils.viewer.teams.list.invalidate(); showToast(t("your_team_disbanded_successfully"), "success"); router.push(`${WEBAPP_URL}/teams`); + trackFormbricksAction("team_disbanded"); }, }); @@ -217,7 +219,9 @@ const ProfileView = () => { variety="danger" title={t("disband_team")} confirmBtnText={t("confirm_disband_team")} - onConfirm={deleteTeam}> + onConfirm={() => { + deleteTeam(); + }}> {t("disband_team_confirmation_message")} diff --git a/packages/features/shell/Shell.tsx b/packages/features/shell/Shell.tsx index 2764c983b2..32f84d2800 100644 --- a/packages/features/shell/Shell.tsx +++ b/packages/features/shell/Shell.tsx @@ -44,6 +44,7 @@ import { TOP_BANNER_HEIGHT, ENABLE_PROFILE_SWITCHER, } from "@calcom/lib/constants"; +import { useFormbricks } from "@calcom/lib/formbricks-client"; import getBrandColours from "@calcom/lib/getBrandColours"; import { useBookerUrl } from "@calcom/lib/hooks/useBookerUrl"; import { useLocale } from "@calcom/lib/hooks/useLocale"; @@ -237,6 +238,8 @@ const Layout = (props: LayoutProps) => { return (activeBanners?.length ?? 0) * TOP_BANNER_HEIGHT; }, [banners]); + useFormbricks(); + return ( <> {!props.withoutSeo && ( diff --git a/packages/lib/formbricks-client.ts b/packages/lib/formbricks-client.ts new file mode 100644 index 0000000000..9d804022ef --- /dev/null +++ b/packages/lib/formbricks-client.ts @@ -0,0 +1,56 @@ +import formbricks from "@formbricks/js"; +import { useSession } from "next-auth/react"; +import { useEffect } from "react"; + +import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery"; + +export const initFormbricks = ({ + userId, + attributes, +}: { + userId: string; + attributes: { [key: string]: string | null | undefined }; +}) => { + const filteredAttributes: Record = {}; + Object.entries(attributes).forEach(([key, value]) => { + if (value !== null && value !== undefined) { + filteredAttributes[key] = value; + } + }); + + if (process.env.NEXT_PUBLIC_FORMBRICKS_HOST_URL && process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID) { + formbricks.init({ + environmentId: process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID, + apiHost: process.env.NEXT_PUBLIC_FORMBRICKS_HOST_URL, + debug: process.env.NODE_ENV === "development", + userId, + attributes: filteredAttributes, + }); + } +}; + +export const trackFormbricksAction = (eventName: string, properties: Record = {}) => { + if (process.env.NEXT_PUBLIC_FORMBRICKS_HOST_URL && process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID) { + formbricks.track(eventName, properties); + } +}; + +export const useFormbricks = () => { + const { data: user, isLoading } = useMeQuery(); + const { data: session, status } = useSession(); + + useEffect(() => { + if (!isLoading && user && session) { + initFormbricks({ + userId: user.id.toString(), + attributes: { + name: user?.name, + email: user.email, + username: user?.username, + belongsToActiveTeam: session.user.belongsToActiveTeam?.toString(), + isOrganizationAdmin: user.organization?.isOrgAdmin?.toString(), + }, + }); + } + }, [isLoading, user, status, session]); +}; diff --git a/packages/lib/formbricks.ts b/packages/lib/formbricks.ts index b5ed06a546..87f9633622 100644 --- a/packages/lib/formbricks.ts +++ b/packages/lib/formbricks.ts @@ -10,11 +10,11 @@ enum Rating { } export const sendFeedbackFormbricks = async (userId: number, feedback: Feedback) => { - if (!process.env.FORMBRICKS_HOST_URL || !process.env.FORMBRICKS_ENVIRONMENT_ID) + if (!process.env.NEXT_PUBLIC_FORMBRICKS_HOST_URL || !process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID) throw new Error("Missing FORMBRICKS_HOST_URL or FORMBRICKS_ENVIRONMENT_ID env variable"); const api = new FormbricksAPI({ - apiHost: process.env.FORMBRICKS_HOST_URL, - environmentId: process.env.FORMBRICKS_ENVIRONMENT_ID, + apiHost: process.env.NEXT_PUBLIC_FORMBRICKS_HOST_URL, + environmentId: process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID, }); if (process.env.FORMBRICKS_FEEDBACK_SURVEY_ID) { const formbricksUserId = userId.toString(); diff --git a/packages/lib/package.json b/packages/lib/package.json index fae3a15b8f..99c674c371 100644 --- a/packages/lib/package.json +++ b/packages/lib/package.json @@ -15,6 +15,7 @@ "@calcom/config": "*", "@calcom/dayjs": "*", "@formbricks/api": "^1.1.0", + "@formbricks/js": "^1.5.0", "@sendgrid/client": "^7.7.0", "@vercel/og": "^0.5.0", "bcryptjs": "^2.4.3", diff --git a/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts b/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts index e72932778b..0aca667ce6 100644 --- a/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts +++ b/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts @@ -31,7 +31,7 @@ export const submitFeedbackHandler = async ({ ctx, input }: SubmitFeedbackOption comment: comment, }, }); - if (process.env.FORMBRICKS_HOST_URL && process.env.FORMBRICKS_ENVIRONMENT_ID) + if (process.env.NEXT_PUBLIC_FORMBRICKS_HOST_URL && process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID) sendFeedbackFormbricks(ctx.user.id, feedback); if (process.env.SEND_FEEDBACK_EMAIL && comment) sendFeedbackEmail(feedback); diff --git a/turbo.json b/turbo.json index 87e622539e..694ab7f9fb 100644 --- a/turbo.json +++ b/turbo.json @@ -259,8 +259,6 @@ "EMAIL_SERVER_USER", "EMAIL_SERVER", "EXCHANGE_DEFAULT_EWS_URL", - "FORMBRICKS_HOST_URL", - "FORMBRICKS_ENVIRONMENT_ID", "FORMBRICKS_FEEDBACK_SURVEY_ID", "AVATARAPI_USERNAME", "AVATARAPI_PASSWORD", @@ -291,6 +289,8 @@ "NEXT_PUBLIC_DEBUG", "NEXT_PUBLIC_DISABLE_SIGNUP", "NEXT_PUBLIC_EMBED_LIB_URL", + "NEXT_PUBLIC_FORMBRICKS_HOST_URL", + "NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID", "NEXT_PUBLIC_HOSTED_CAL_FEATURES", "NEXT_PUBLIC_MINUTES_TO_BOOK", "NEXT_PUBLIC_SENDER_ID", diff --git a/yarn.lock b/yarn.lock index ec4fb1d489..f2a76606db 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4229,6 +4229,7 @@ __metadata: "@calcom/types": "*" "@faker-js/faker": ^7.3.0 "@formbricks/api": ^1.1.0 + "@formbricks/js": ^1.5.0 "@sendgrid/client": ^7.7.0 "@vercel/og": ^0.5.0 bcryptjs: ^2.4.3 @@ -6307,6 +6308,13 @@ __metadata: languageName: node linkType: hard +"@formbricks/js@npm:^1.5.0": + version: 1.5.0 + resolution: "@formbricks/js@npm:1.5.0" + checksum: b9ba81484f205888f6db81b48b0e5178458b4de8fa3dd76a6f95378656d739204adab502356181fce08fcb65a3b324047ffeb977d8bf556b3e95f652b67aee4d + languageName: node + linkType: hard + "@formkit/auto-animate@npm:1.0.0-beta.5": version: 1.0.0-beta.5 resolution: "@formkit/auto-animate@npm:1.0.0-beta.5"