From d9066a6beb4a84ab42d638810d25c002601678a4 Mon Sep 17 00:00:00 2001 From: Hailey Date: Thu, 13 Jun 2024 14:31:19 -0700 Subject: [PATCH 1/2] add `document.referrer` to statsig custom (#4509) * add referrer to statsig custom dont include referrer if hostname is bsky.app save add `document.referrer` to statsig custom * add a hostname field * account for ssr * account for ssr --- src/lib/statsig/statsig.tsx | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx index 166d468a..d57f1c0a 100644 --- a/src/lib/statsig/statsig.tsx +++ b/src/lib/statsig/statsig.tsx @@ -26,6 +26,8 @@ type StatsigUser = { bundleDate: number refSrc: string refUrl: string + referrer: string + referrerHostname: string appLanguage: string contentLanguages: string[] } @@ -33,12 +35,22 @@ type StatsigUser = { let refSrc = '' let refUrl = '' +let referrer = '' +let referrerHostname = '' if (isWeb && typeof window !== 'undefined') { const params = new URLSearchParams(window.location.search) refSrc = params.get('ref_src') ?? '' refUrl = decodeURIComponent(params.get('ref_url') ?? '') } +if (isWeb && typeof document !== 'undefined' && document != null) { + const url = new URL(document.referrer) + if (url.hostname !== 'bsky.app') { + referrer = document.referrer + referrerHostname = url.hostname + } +} + export type {LogEvents} function createStatsigOptions(prefetchUsers: StatsigUser[]) { @@ -198,6 +210,8 @@ function toStatsigUser(did: string | undefined): StatsigUser { custom: { refSrc, refUrl, + referrer, + referrerHostname, platform: Platform.OS as 'ios' | 'android' | 'web', bundleIdentifier: BUNDLE_IDENTIFIER, bundleDate: BUNDLE_DATE, From bdeac28d74abd54b0373663cbd57b7858888280f Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Thu, 13 Jun 2024 23:06:22 +0100 Subject: [PATCH 2/2] Try/catch URL parsing of referrer (#4511) --- src/lib/statsig/statsig.tsx | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx index d57f1c0a..f6aed999 100644 --- a/src/lib/statsig/statsig.tsx +++ b/src/lib/statsig/statsig.tsx @@ -43,12 +43,19 @@ if (isWeb && typeof window !== 'undefined') { refUrl = decodeURIComponent(params.get('ref_url') ?? '') } -if (isWeb && typeof document !== 'undefined' && document != null) { - const url = new URL(document.referrer) - if (url.hostname !== 'bsky.app') { - referrer = document.referrer - referrerHostname = url.hostname - } +if ( + isWeb && + typeof document !== 'undefined' && + document != null && + document.referrer +) { + try { + const url = new URL(document.referrer) + if (url.hostname !== 'bsky.app') { + referrer = document.referrer + referrerHostname = url.hostname + } + } catch {} } export type {LogEvents}