This repository was archived by the owner on Jul 14, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathposthog.js
More file actions
232 lines (192 loc) · 7.53 KB
/
Copy pathposthog.js
File metadata and controls
232 lines (192 loc) · 7.53 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// posthog see https://posthog.com/docs/libraries/js#installation
!(function (t, e) {
var o, n, p, r;
e.__SV ||
((window.posthog = e),
(e._i = []),
(e.init = function (i, s, a) {
function g(t, e) {
var o = e.split(".");
2 == o.length && ((t = t[o[0]]), (e = o[1])),
(t[e] = function () {
t.push([e].concat(Array.prototype.slice.call(arguments, 0)));
});
}
((p = t.createElement("script")).type = "text/javascript"),
(p.crossOrigin = "anonymous"),
(p.async = !0),
(p.src =
s.api_host.replace(".i.posthog.com", "-assets.i.posthog.com") +
"/static/array.js"),
(r = t.getElementsByTagName("script")[0]).parentNode.insertBefore(p, r);
var u = e;
for (
void 0 !== a ? (u = e[a] = []) : (a = "posthog"),
u.people = u.people || [],
u.toString = function (t) {
var e = "posthog";
return "posthog" !== a && (e += "." + a), t || (e += " (stub)"), e;
},
u.people.toString = function () {
return u.toString(1) + ".people (stub)";
},
o =
"init capture register register_once register_for_session unregister unregister_for_session getFeatureFlag getFeatureFlagPayload isFeatureEnabled reloadFeatureFlags updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures on onFeatureFlags onSessionId getSurveys getActiveMatchingSurveys renderSurvey canRenderSurvey getNextSurveyStep identify setPersonProperties group resetGroups setPersonPropertiesForFlags resetPersonPropertiesForFlags setGroupPropertiesForFlags resetGroupPropertiesForFlags reset get_distinct_id getGroups get_session_id get_session_replay_url alias set_config startSessionRecording stopSessionRecording sessionRecordingStarted captureException loadToolbar get_property getSessionProperty createPersonProfile opt_in_capturing opt_out_capturing has_opted_in_capturing has_opted_out_capturing clear_opt_in_out_capturing debug".split(
" ",
),
n = 0;
n < o.length;
n++
)
g(u, o[n]);
e._i.push([i, s, a]);
}),
(e.__SV = 1));
})(document, window.posthog || []);
const parseCookiesForConsentCookie = () => {
const cookies = document.cookie.split(";");
const octomindConsentCookie = cookies.find((c) =>
c.trim().startsWith("octomind-consent="),
);
if (!octomindConsentCookie) {
return undefined;
}
const value = octomindConsentCookie.split("=")[1];
if (!value) {
return undefined;
}
let parsedValue;
try {
parsedValue = JSON.parse(window.decodeURIComponent(value));
} catch (e) {
return undefined;
}
return parsedValue;
};
const OCTOMIND_USER_AGENT = "octomind";
const writeCookie = (cookieValue) => {
const stringified = window.encodeURIComponent(JSON.stringify(cookieValue));
// maximum allowed, see https://developer.chrome.com/blog/cookie-max-age-expires
const daysUntilExpiry = 400;
const msUntilExpiry = daysUntilExpiry * 24 * 3600 * 1000;
const expires = new Date(Date.now() + msUntilExpiry);
document.cookie = `octomind-consent=${stringified};sameSite=strict;Domain=.octomind.dev;path=/;expires=${expires.toUTCString()}`;
};
const posthogInit = (persistence) => {
const currentUrl = new URL(window.location.href);
const distinctId = currentUrl.searchParams.get("distinctId");
posthog.init("phc_DZrVg5kgD45m4Au5nFt6m9rykBTg3mAkeHNY3atWNbW", {
api_host: "https://app.octomind.dev/events",
ui_host: "https://eu.posthog.com",
disable_session_recording: true,
person_profiles: "always",
persistence,
bootstrap: distinctId
? {
distinctID: distinctId,
}
: undefined,
loaded: (posthog) => {
document.querySelectorAll("a").forEach((el) => {
if (!el.hasAttribute("href")) {
return;
}
if (!el.getAttribute("href").includes("octomind.dev")) {
return;
}
const url = new URL(el.getAttribute("href"));
url.searchParams.set("distinctId", posthog.get_distinct_id());
el.href = url.toString();
});
},
});
if (window.navigator.userAgent.includes(OCTOMIND_USER_AGENT)) {
posthog.opt_out_capturing();
}
};
const hideBanner = () => {
document.getElementById("cookie-banner-overlay").style.display = "none";
};
const showBanner = () => {
document.getElementById("cookie-banner-overlay").style.display = "block";
};
const COOKIE_PERSISTENCE = "localStorage+cookie";
const setupCookieButton = (id, allowOptionalValueToPersist) => {
const cookieButton = document.getElementById(id);
const persistence = allowOptionalValueToPersist
? COOKIE_PERSISTENCE
: "memory";
cookieButton.onclick = () => {
writeCookie({
consentDate: new Date(),
allowOptional: allowOptionalValueToPersist,
});
posthog.set_config({
persistence,
});
hideBanner();
};
};
const addCookieBanner = () => {
const cookieBannerOverlay = document.createElement("div");
cookieBannerOverlay.id = "cookie-banner-overlay";
cookieBannerOverlay.className = "cookie-overlay";
const cookieBannerImage = document.createElement("img");
cookieBannerImage.src =
"https://storage.googleapis.com/assets.octomind.dev/cookieBanner.webp";
cookieBannerImage.className = "cookie-image";
cookieBannerImage.loading = "lazy";
cookieBannerImage.alt = "octopus tentacles holding a cookie";
cookieBannerOverlay.appendChild(cookieBannerImage);
const cookieModal = document.createElement("div");
cookieModal.id = "cookie-modal";
cookieModal.className = "modal-cookie";
cookieBannerOverlay.appendChild(cookieModal);
const cookieFrame = document.createElement("div");
cookieFrame.className = "cookie-frame";
cookieModal.appendChild(cookieFrame);
const cookieTextBlock = document.createElement("div");
cookieTextBlock.className = "cookie-text-block";
cookieFrame.appendChild(cookieTextBlock);
const cookieText = document.createElement("div");
cookieText.className = "cookie-text";
cookieText.innerHTML = `We are using cookies for secure log-in and to improve our app. First party only, no data krakens 🦑 Learn more in our <a href="https://octomind.dev/privacy-policy" target="_blank" class="link-white">privacy policy</a>.`;
cookieTextBlock.appendChild(cookieText);
const essentialButton = document.createElement("a");
essentialButton.innerHTML = `
<a id="allow-essential-btn" class="allow-essential-btn button">
<span class="essential-button-inner">allow essential<br /> cookies</span>
</a>
`;
cookieFrame.appendChild(essentialButton);
const allCookiesButton = document.createElement("a");
allCookiesButton.innerHTML = `<a id="allow-all-btn" class="button-allow-all button">allow all cookies</a>`;
cookieFrame.appendChild(allCookiesButton);
document.querySelector("main").appendChild(cookieBannerOverlay);
};
const sleep = async (numberOfMilliseconds) => {
return new Promise((resolve) => {
setTimeout(resolve, numberOfMilliseconds);
});
};
const onLoad = async () => {
while (!window.next?.router?.isReady) {
console.log("waiting for next hydration");
await sleep(10);
}
addCookieBanner();
const consentCookieValue = parseCookiesForConsentCookie();
if (consentCookieValue) {
if (consentCookieValue.allowOptional) {
posthogInit(COOKIE_PERSISTENCE);
} else {
posthogInit("memory");
}
return;
}
posthogInit("memory");
showBanner();
setupCookieButton("allow-essential-btn", false);
setupCookieButton("allow-all-btn", true);
};
void onLoad();