From d9066a6beb4a84ab42d638810d25c002601678a4 Mon Sep 17 00:00:00 2001
From: Hailey <me@haileyok.com>
Date: Thu, 13 Jun 2024 14:31:19 -0700
Subject: 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(+)

(limited to 'src/lib/statsig/statsig.tsx')

diff --git a/src/lib/statsig/statsig.tsx b/src/lib/statsig/statsig.tsx
index 166d468a1..d57f1c0aa 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,
-- 
cgit 1.4.1

