From da3d0283ffee9c592e5b81d2be6a848722df298f Mon Sep 17 00:00:00 2001
From: timothycarambat <rambat1010@gmail.com>
Date: Wed, 13 Nov 2024 12:53:45 -0800
Subject: [PATCH] update onboarding survery endpoint

---
 .../OnboardingFlow/Steps/Survey/index.jsx     | 44 ++++++++++++-------
 frontend/src/utils/constants.js               |  1 +
 2 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/frontend/src/pages/OnboardingFlow/Steps/Survey/index.jsx b/frontend/src/pages/OnboardingFlow/Steps/Survey/index.jsx
index d570d5097..14781381f 100644
--- a/frontend/src/pages/OnboardingFlow/Steps/Survey/index.jsx
+++ b/frontend/src/pages/OnboardingFlow/Steps/Survey/index.jsx
@@ -1,4 +1,7 @@
-import { COMPLETE_QUESTIONNAIRE } from "@/utils/constants";
+import {
+  COMPLETE_QUESTIONNAIRE,
+  ONBOARDING_SURVEY_URL,
+} from "@/utils/constants";
 import paths from "@/utils/paths";
 import { CheckCircle } from "@phosphor-icons/react";
 import React, { useState, useEffect, useRef } from "react";
@@ -13,22 +16,31 @@ async function sendQuestionnaire({ email, useCase, comment }) {
     return;
   }
 
-  return fetch(`https://onboarding-wxich7363q-uc.a.run.app`, {
-    method: "POST",
-    body: JSON.stringify({
-      email,
-      useCase,
-      comment,
-      sourceId: "0VRjqHh6Vukqi0x0Vd0n/m8JuT7k8nOz",
-    }),
-  })
-    .then(() => {
-      window.localStorage.setItem(COMPLETE_QUESTIONNAIRE, true);
-      console.log(`✅ Questionnaire responses sent.`);
+  const data = JSON.stringify({
+    email,
+    useCase,
+    comment,
+    sourceId: "0VRjqHh6Vukqi0x0Vd0n/m8JuT7k8nOz",
+  });
+
+  if (!navigator.sendBeacon) {
+    console.log("navigator.sendBeacon not supported, falling back to fetch");
+    return fetch(ONBOARDING_SURVEY_URL, {
+      method: "POST",
+      body: data,
     })
-    .catch((error) => {
-      console.error(`sendQuestionnaire`, error.message);
-    });
+      .then(() => {
+        window.localStorage.setItem(COMPLETE_QUESTIONNAIRE, true);
+        console.log(`✅ Questionnaire responses sent.`);
+      })
+      .catch((error) => {
+        console.error(`sendQuestionnaire`, error.message);
+      });
+  }
+
+  navigator.sendBeacon(ONBOARDING_SURVEY_URL, data);
+  window.localStorage.setItem(COMPLETE_QUESTIONNAIRE, true);
+  console.log(`✅ Questionnaire responses sent.`);
 }
 
 export default function Survey({ setHeader, setForwardBtn, setBackBtn }) {
diff --git a/frontend/src/utils/constants.js b/frontend/src/utils/constants.js
index 8b9c75629..f15634df3 100644
--- a/frontend/src/utils/constants.js
+++ b/frontend/src/utils/constants.js
@@ -1,4 +1,5 @@
 export const API_BASE = import.meta.env.VITE_API_BASE || "/api";
+export const ONBOARDING_SURVEY_URL = "https://onboarding.anythingllm.com";
 
 export const AUTH_USER = "anythingllm_user";
 export const AUTH_TOKEN = "anythingllm_authToken";