{"version":3,"file":"useTrackingEvents.4WOL1bow.mjs","names":["useState","useMemo","useCallback","useCallback"],"sources":["https:/framerusercontent.com/modules/6UtPpKQ4XWQbVUZ8TCXH/PjHKReg11cggnAzUENNW/useFetch.js","https:/framerusercontent.com/modules/RoyLn3Rsr2xd0OGTHlbC/jVAqWWvFUQPTqCjPxYQ4/tracking_utils.js","https:/framerusercontent.com/modules/dl6NF18IBVaCIEqMYmbC/mDbYt38VeQniejavS81D/useTrackingEvents.js"],"sourcesContent":["import{useCallback,useMemo,useState}from\"react\";export function useFetch({urlPath,method}){const[data,setData]=useState(null);const[status,setStatus]=useState(\"uninitialized\");const rootUrl=useMemo(()=>{if(typeof window===\"undefined\"){return;}const hostname=window.location.hostname;const isProd=hostname.includes(\"www.rocketloans.com\");if(isProd){return\"https://www.rocketloans.com/api\";}return\"https://qa2.rockloans.com/api\";},[]);const trigger=useCallback(async payload=>{const fullUrl=`${rootUrl}${urlPath}`;const body=payload?JSON.stringify(payload):undefined;try{const results=await fetch(fullUrl,{method,headers:{...payload&&{\"Content-Type\":\"application/json\"},Accept:\"application/json\"},body});if(results.ok){const resultsData=await results.json();setData(resultsData);setStatus(\"success\");return resultsData;}throw new Error(`Error when fetching`);}catch(err){console.error(`${err}`);throw new Error(`${err}`);}},[rootUrl,method,urlPath]);return{data,trigger,status};}\nexport const __FramerMetadata__ = {\"exports\":{\"useFetch\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./useFetch.map","/**\n * @description Get properties for a page view event\n */export function getPageViewProperties(){const params=new URLSearchParams(window.location.search);return{page_path:window.location.pathname,page_domain:window.location.hostname,page_url:window.location.href,referrer:document.referrer,utm_campaign:params.get(\"utm_campaign\")||null,utm_content:params.get(\"utm_content\")||null,utm_medium:params.get(\"utm_medium\")||null,utm_source:params.get(\"utm_source\")||null,utm_term:params.get(\"utm_term\")||null,utm_audience:params.get(\"utm_audience\")||null};}/**\n * @description Get the page context object, sent in all event requests in context.page\n */export function getPageContext(){return{pathname:window.location.pathname,referrer:document.referrer,search:window.location.search,title:document.title,url:window.location.href};}/**\n * @description Get a context object for any event\n */export function getEventContext(){const pageContext=getPageContext();return{timestamp:String(new Date().getTime()),page:pageContext};}/**\n * @description Send a click event to FullStory\n */export function sendFullstoryButtonClickEvent(button_name){const buttonName=button_name.toUpperCase();if(window.FS){window.FS(\"trackEvent\",{name:buttonName,properties:{event_name:\"CLICK\",page:{path:window.location.pathname}}});}else{console.log(\"FULLSTORY: SDK is not available.\");}}export const getCookie=name=>{const cookies=document.cookie.split(\"; \");const targetCookie=cookies.find(row=>row.startsWith(`${name}=`));return targetCookie?targetCookie.split(\"=\")[1]:null;};\nexport const __FramerMetadata__ = {\"exports\":{\"getPageViewProperties\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"getPageContext\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"getCookie\":{\"type\":\"variable\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"getEventContext\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"sendFullstoryButtonClickEvent\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./tracking_utils.map","import{useCallback,useEffect}from\"react\";import{useFetch}from\"https://framerusercontent.com/modules/6UtPpKQ4XWQbVUZ8TCXH/PjHKReg11cggnAzUENNW/useFetch.js\";import{getCookie,getEventContext,getPageViewProperties,sendFullstoryButtonClickEvent}from\"https://framerusercontent.com/modules/RoyLn3Rsr2xd0OGTHlbC/jVAqWWvFUQPTqCjPxYQ4/tracking_utils.js\";/**\n * @description A hook used for initializing Amplitude in Framer, and communicates to the Apollo BFF for tracking events.\n *\n * It does the following:\n *\n * 1. Gets the deviceId from the cookie if it's set\n * 2. If not, it calls `api/session-start` to set it\n * 3. Sets up a document framer:click event listener (which comes out of the box with Framer), with a callback for sending button_click events\n * 4. Automatically sends `page_view` events on page load\n * 5. Returns an object that can be used to apply a click event for code components\n */export function useTrackingEvents(){// Send a tracking event\nconst{trigger:triggerEvent}=useFetch({urlPath:\"/events\",method:\"POST\"});// Call 'session/start' to get and set a deviceId\nconst{trigger:triggerSessionStart}=useFetch({urlPath:\"/session/start\",method:\"POST\"});/**\n     * @description Send the event details to the Apollo BFF\n     */const handleTrackingEvent=useCallback(async args=>{const context=getEventContext();const payload={...args,context};try{await triggerEvent(payload);}catch(err){console.error(\"An error occurred when tracking event\",payload.event,err);}},[triggerEvent]);/**\n     * @description Send a click event to both FullStory and Amplitude\n     */const sendClickEvent=useCallback(async button_name=>{sendFullstoryButtonClickEvent(button_name);await handleTrackingEvent({event:\"button_clicked\",properties:{button_name}});},[handleTrackingEvent]);/**\n     * @description Listener event specifically for click events on framer pages\n     *\n     * This callback is added to the document's framer:click event listener. It is called when a Framer Button or Link has a \"Tracking\" value\n     */const buttonClickListener=useCallback(async event=>{const eventDetail=event.detail;const trackingId=eventDetail.trackingId;if(!trackingId){return;}// Framer enforces lower kebab-case for tracking IDs. The RL analytics naming convention for button names is snake_case\nconst buttonId=trackingId.replaceAll(\"-\",\"_\");await sendClickEvent(buttonId);},[sendClickEvent]);/**\n     * @description Send a button click event to both amplitude and FullStory\n     */const sendPageViewEvent=useCallback(async()=>{const pageViewProperties=getPageViewProperties();await handleTrackingEvent({event:\"page_viewed\",properties:pageViewProperties});},[handleTrackingEvent]);/**\n     * @description Tries to first confirm if a deviceId is already stored in a cookie. Otherwise, tries to call /api/session/start, which will set it.\n     */const getOrSetDeviceId=useCallback(async()=>{const deviceIdCookie=getCookie(\"pl_device\");// If a deviceId is already stored in a cookie, return early\nif(deviceIdCookie){return true;}// Otherwise, calling /api/session-start will set it in a cookie\ntry{await triggerSessionStart();}catch(err){// We shouldn't block calling events if setting the device ID fails\nconsole.error(\"Error setting the device ID\",err);}return true;},[triggerSessionStart]);/**\n     * @description Props to spread on links or buttons to hook into click events\n     */const clickEventTrackingProps=useCallback(button_name=>{return{onClick:()=>sendClickEvent(button_name)};},[sendClickEvent]);useEffect(()=>{async function ensureDeviceIdIsSet(){await getOrSetDeviceId();}// Wait until the deviceId is set\nensureDeviceIdIsSet();// Send a page view event on page load\nsendPageViewEvent();// Framer Button Click Tracking\ndocument.addEventListener(\"framer:click\",buttonClickListener);return()=>{return document.removeEventListener(\"framer:click\",buttonClickListener);};},[getOrSetDeviceId,buttonClickListener,sendPageViewEvent]);return{clickEventTrackingProps};}\nexport const __FramerMetadata__ = {\"exports\":{\"useTrackingEvents\":{\"type\":\"function\",\"annotations\":{\"framerContractVersion\":\"1\"}},\"__FramerMetadata__\":{\"type\":\"variable\"}}}\n//# sourceMappingURL=./useTrackingEvents.map"],"mappings":"uIAAgD,SAAgB,EAAS,CAAC,UAAQ,UAAQ,CAAC,GAAK,CAAC,EAAK,GAASA,EAAS,KAAK,CAAM,CAAC,EAAO,GAAWA,EAAS,gBAAgB,CAAO,EAAQC,MAAY,CAAI,GAAO,IAAS,OAAuK,OAAnI,EAAO,SAAS,SAA+B,SAAS,sBAAsB,CAAkB,kCAAyC,iCAAkC,EAAE,CAAC,CAAogB,MAAM,CAAC,OAAK,QAAjgBC,EAAY,KAAM,IAAS,CAAC,IAAM,EAAQ,GAAG,IAAU,IAAgB,EAAK,EAAQ,KAAK,UAAU,EAAQ,CAAC,IAAA,GAAU,GAAG,CAAC,IAAM,EAAQ,MAAM,MAAM,EAAQ,CAAC,SAAO,QAAQ,CAAC,GAAG,GAAS,CAAC,eAAe,mBAAmB,CAAC,OAAO,mBAAmB,CAAC,OAAK,CAAC,CAAC,GAAG,EAAQ,GAAG,CAAC,IAAM,EAAY,MAAM,EAAQ,MAAM,CAA2C,OAA1C,EAAQ,EAAY,CAAC,EAAU,UAAU,CAAQ,EAAa,MAAU,MAAM,sBAAsB,OAAQ,EAAI,CAAyB,MAAxB,QAAQ,MAAM,GAAG,IAAM,CAAW,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