fix: adds action to response meta (#2381)

This commit is contained in:
Anshuman Pandey
2024-04-03 19:05:08 +05:30
committed by GitHub
parent 2463ed8500
commit d94e003244
5 changed files with 12 additions and 6 deletions
@@ -10,7 +10,7 @@ import { createResponse } from "@formbricks/lib/response/service";
import { getSurvey } from "@formbricks/lib/survey/service";
import { ZId } from "@formbricks/types/environment";
import { InvalidInputError } from "@formbricks/types/errors";
import { TResponse, ZResponseInput } from "@formbricks/types/responses";
import { TResponse, TResponseInput, ZResponseInput } from "@formbricks/types/responses";
interface Context {
params: {
@@ -77,7 +77,7 @@ export async function POST(request: Request, context: Context): Promise<Response
let response: TResponse;
try {
const meta = {
const meta: TResponseInput["meta"] = {
source: responseInput?.meta?.source,
url: responseInput?.meta?.url,
userAgent: {
@@ -86,6 +86,7 @@ export async function POST(request: Request, context: Context): Promise<Response
os: agent?.os.name,
},
country: country,
action: responseInput?.meta?.action,
};
response = await createResponse({
+1 -1
View File
@@ -83,7 +83,7 @@ export const trackAction = async (name: string): Promise<Result<void, NetworkErr
for (const survey of activeSurveys) {
for (const trigger of survey.triggers) {
if (trigger === name) {
await triggerSurvey(survey);
await triggerSurvey(survey, name);
}
}
}
+4 -3
View File
@@ -30,7 +30,7 @@ const shouldDisplayBasedOnPercentage = (displayPercentage: number) => {
return randomNum <= displayPercentage;
};
export const triggerSurvey = async (survey: TSurvey): Promise<void> => {
export const triggerSurvey = async (survey: TSurvey, action?: string): Promise<void> => {
// Check if the survey should be displayed based on displayPercentage
if (survey.displayPercentage) {
const shouldDisplaySurvey = shouldDisplayBasedOnPercentage(survey.displayPercentage);
@@ -39,10 +39,10 @@ export const triggerSurvey = async (survey: TSurvey): Promise<void> => {
return; // skip displaying the survey
}
}
await renderWidget(survey);
await renderWidget(survey, action);
};
const renderWidget = async (survey: TSurvey) => {
const renderWidget = async (survey: TSurvey, action?: string) => {
if (isSurveyRunning) {
logger.debug("A survey is already running. Skipping.");
return;
@@ -201,6 +201,7 @@ const renderWidget = async (survey: TSurvey) => {
language: languageCode === "default" ? getDefaultLanguageCode(survey) : languageCode,
meta: {
url: window.location.href,
action,
},
});
},
+3
View File
@@ -200,6 +200,7 @@ export const ZResponseMeta = z.object({
})
.optional(),
country: z.string().optional(),
action: z.string().optional(),
});
export type TResponseMeta = z.infer<typeof ZResponseMeta>;
@@ -244,6 +245,7 @@ export const ZResponseInput = z.object({
})
.optional(),
country: z.string().optional(),
action: z.string().optional(),
})
.optional(),
});
@@ -280,6 +282,7 @@ export const ZResponseUpdate = z.object({
.object({
url: z.string().optional(),
source: z.string().optional(),
action: z.string().optional(),
})
.optional(),
});
+1
View File
@@ -221,6 +221,7 @@ export default function SingleResponseCard({
</p>
)}
{response.meta.url && <p>URL: {response.meta.url}</p>}
{response.meta.action && <p>Action: {response.meta.action}</p>}
{response.meta.source && <p>Source: {response.meta.source}</p>}
{response.meta.country && <p>Country: {response.meta.country}</p>}
</div>