-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattributionUtils.ts
More file actions
162 lines (133 loc) · 4.36 KB
/
attributionUtils.ts
File metadata and controls
162 lines (133 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"use client";
import { normaliseReferrer } from "@/utils/referrer";
const UTM_STORAGE_KEY = "attribution_params";
const INITIAL_REFERRER_KEY = "initial_referrer";
const INITIAL_REFERRER_COOKIE = "initial_referrer";
const isAttributionDebugEnabled =
process.env.NEXT_PUBLIC_ATTRIBUTION_DEBUG === "true";
type AttributionParams = {
utm_source: string | null;
utm_medium: string | null;
utm_campaign: string | null;
};
export type StoredAttributionParams = Partial<AttributionParams> & {
initial_referrer: string | null;
};
const getSafeCurrentUrl = (): string => {
try {
const currentUrl = new URL(window.location.href);
if (currentUrl.hash) {
const hashParams = new URLSearchParams(currentUrl.hash.slice(1));
if (
hashParams.get("access_token") ||
hashParams.get("refresh_token") ||
hashParams.get("token")
) {
currentUrl.hash = "#[redacted]";
}
}
return currentUrl.toString();
} catch (_error) {
return window.location.origin + window.location.pathname;
}
};
const getCookie = (name: string): string | null => {
const cookie = document.cookie
.split("; ")
.find((entry) => entry.startsWith(`${name}=`));
if (!cookie) return null;
const value = cookie.split("=").slice(1).join("=");
if (!value) return null;
const cleanedValue = normaliseReferrer(value);
const trimmedValue = cleanedValue.trim();
if (!trimmedValue) return null;
return trimmedValue;
};
const getExternalDocumentReferrer = (): string | null => {
const referrer = document.referrer;
if (!referrer) return null;
try {
const referrerUrl = new URL(referrer);
if (referrerUrl.host === window.location.host) {
return null;
}
return referrer;
} catch {
return null;
}
};
const storeInitialReferrer = (referrer: string) => {
localStorage.setItem(INITIAL_REFERRER_KEY, normaliseReferrer(referrer));
};
export function captureAttributionParams() {
if (typeof window === "undefined") return;
const params = new URLSearchParams(window.location.search);
const utmParams: AttributionParams = {
utm_source: params.get("utm_source"),
utm_medium: params.get("utm_medium"),
utm_campaign: params.get("utm_campaign"),
};
const cookieReferrer = getCookie(INITIAL_REFERRER_COOKIE);
if (isAttributionDebugEnabled) {
console.log("Attribution Debug:", {
currentUrl: getSafeCurrentUrl(),
referrer: document.referrer,
cookieReferrer,
hasUtmParams: Object.values(utmParams).some((value) => value),
});
}
const hasStoredReferrer = localStorage.getItem(INITIAL_REFERRER_KEY);
const initialReferrer = cookieReferrer ?? getExternalDocumentReferrer();
if (!hasStoredReferrer && initialReferrer) {
if (isAttributionDebugEnabled) {
console.log("Storing referrer:", initialReferrer);
}
storeInitialReferrer(initialReferrer);
}
const hasStoredUtm = localStorage.getItem(UTM_STORAGE_KEY);
if (!hasStoredUtm && Object.values(utmParams).some((value) => value)) {
if (isAttributionDebugEnabled) {
console.log("Storing UTM params:", utmParams);
}
localStorage.setItem(UTM_STORAGE_KEY, JSON.stringify(utmParams));
}
}
export function getStoredAttributionParams(): StoredAttributionParams {
if (typeof window === "undefined") {
return {
initial_referrer: null,
};
}
try {
const stored = localStorage.getItem(UTM_STORAGE_KEY);
const storedInitialReferrer = localStorage.getItem(INITIAL_REFERRER_KEY);
const cookieReferrer = getCookie(INITIAL_REFERRER_COOKIE);
const initialReferrer =
storedInitialReferrer !== null
? normaliseReferrer(storedInitialReferrer)
: cookieReferrer;
if (storedInitialReferrer === null && initialReferrer) {
storeInitialReferrer(initialReferrer);
} else if (
storedInitialReferrer !== null &&
initialReferrer &&
initialReferrer !== storedInitialReferrer
) {
storeInitialReferrer(initialReferrer);
}
return {
...(stored ? JSON.parse(stored) : {}),
initial_referrer: initialReferrer || null,
};
} catch (e) {
console.error("Error reading attribution params:", e);
return {
initial_referrer: null,
};
}
}
export function clearAttributionParams() {
if (typeof window === "undefined") return;
localStorage.removeItem(UTM_STORAGE_KEY);
localStorage.removeItem(INITIAL_REFERRER_KEY);
}