diff --git a/src/app/system/navigation/navigate.ts b/src/app/system/navigation/navigate.ts index e01cf0b27ea..adf1c34c4a9 100644 --- a/src/app/system/navigation/navigate.ts +++ b/src/app/system/navigation/navigate.ts @@ -1,6 +1,6 @@ import { EventEmitter } from "events" import { ActionType, OwnerType, Screen } from "@artsy/cohesion" -import { addBreadcrumb } from "@sentry/react-native" +import { addBreadcrumb, captureMessage } from "@sentry/react-native" import { AppModule, ViewOptions, modules } from "app/AppRegistry" import { LegacyNativeModules } from "app/NativeModules/LegacyNativeModules" import { BottomTabType } from "app/Scenes/BottomTabs/BottomTabType" @@ -68,8 +68,21 @@ export async function navigate(url: string, options: NavigateOptions = {}) { // marketing url requires redirect if (targetURL.startsWith("https://click.artsy.net")) { - const response = await fetch(targetURL) - if (response.url) { + let response + try { + response = await fetch(targetURL) + } catch (error) { + if (__DEV__) { + console.warn(error) + } else { + captureMessage( + `[navigate] Error fetching marketing url redirect on: ${targetURL} failed with error: ${error}`, + "error" + ) + } + } + + if (response?.url) { targetURL = response.url } } diff --git a/src/app/utils/useDeepLinks.ts b/src/app/utils/useDeepLinks.ts index 911d4b2bd76..c7424c056d2 100644 --- a/src/app/utils/useDeepLinks.ts +++ b/src/app/utils/useDeepLinks.ts @@ -1,3 +1,4 @@ +import { captureMessage } from "@sentry/react-native" import { GlobalStore } from "app/store/GlobalStore" import { navigate } from "app/system/navigation/navigate" import { useEffect, useRef } from "react" @@ -29,30 +30,45 @@ export function useDeepLinks() { } }, [isHydrated, isLoggedIn]) - const handleDeepLink = (url: string) => { - // These will be redirected, avoided double tracking - if (!url.includes("click.artsy.net")) { - trackEvent(tracks.deepLink(url)) + const handleDeepLink = async (url: string) => { + let targetURL + + // If the url is a marketing url, we need to fetch the redirect + if (url.includes("click.artsy.net")) { + try { + targetURL = await fetch(url) + } catch (error) { + if (__DEV__) { + console.warn(error) + } else { + captureMessage( + `[handleDeepLink] Error fetching marketing url redirect on: ${url} failed with error: ${error}`, + "error" + ) + } + } } + const deepLinkUrl = targetURL?.url ?? url + + // We track the deep link opened event + trackEvent(tracks.deepLink(deepLinkUrl)) + // If the state is hydrated and the user is logged in // We navigate them to the the deep link if (isHydrated && isLoggedIn) { - navigate(url) + // and we track the deep link + + navigate(deepLinkUrl) } // Otherwise, we save the deep link url // to redirect them to the login screen once they log in - launchURL.current = url + launchURL.current = deepLinkUrl } useEffect(() => { if (isLoggedIn && launchURL.current) { - // These will be redirected, avoided double tracking - if (!launchURL.current.includes("click.artsy.net")) { - trackEvent(tracks.deepLink(launchURL.current)) - } - // Navigate to the saved launch url navigate(launchURL.current) // Reset the launchURL