Skip to content

Commit

Permalink
chore: add posthog to MainHead
Browse files Browse the repository at this point in the history
  • Loading branch information
Alvin Januar committed Jun 2, 2024
1 parent f0d004c commit 6e93e41
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/components/MainHead.astro
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
---
import '../styles/global.css';
import "../styles/global.css";
import Posthog from "./posthog.astro";
interface Props {
title?: string | undefined;
description?: string | undefined;
}
const {
title = 'Alvin Januar Fandy: Personal Site',
description = 'The personal site of Alvin Januar Fandy',
title = "Alvin Januar Fandy: Personal Site",
description = "The personal site of Alvin Januar Fandy",
} = Astro.props;
---

Expand All @@ -29,20 +30,37 @@ const {
<script is:inline>
// This code is inlined in the head to make dark mode instant & blocking.
const getThemePreference = () => {
if (typeof localStorage !== 'undefined' && localStorage.getItem('theme')) {
return localStorage.getItem('theme');
if (
typeof localStorage !== "undefined" &&
localStorage.getItem("theme")
) {
return localStorage.getItem("theme");
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
};
const isDark = getThemePreference() === 'dark';
document.documentElement.classList[isDark ? 'add' : 'remove']('theme-dark');
const isDark = getThemePreference() === "dark";
document.documentElement.classList[isDark ? "add" : "remove"](
"theme-dark"
);

if (typeof localStorage !== 'undefined') {
if (typeof localStorage !== "undefined") {
// Watch the document element and persist user preference when it changes.
const observer = new MutationObserver(() => {
const isDark = document.documentElement.classList.contains('theme-dark');
localStorage.setItem('theme', isDark ? 'dark' : 'light');
const isDark =
document.documentElement.classList.contains(
"theme-dark"
);
localStorage.setItem(
"theme",
isDark ? "dark" : "light"
);
});
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class"],
});
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] });
}
</script>
<Posthog />
85 changes: 85 additions & 0 deletions src/components/posthog.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
---
---

<script>
!(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.async = !0),
(p.src =
s.api_host +
"/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 =
"capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags getFeatureFlag getFeatureFlagPayload reloadFeatureFlags group updateEarlyAccessFeatureEnrollment getEarlyAccessFeatures getActiveMatchingSurveys getSurveys onSessionId".split(
" "
),
n = 0;
n < o.length;
n++
)
g(u, o[n]);
e._i.push([i, s, a]);
}),
(e.__SV = 1));
})(document, window.posthog || []);
posthog.init("phc_9Utfol296QwPeqrpQzwtagBPYXVDjGfDl7kBGt50k5c", {
api_host: "https://us.i.posthog.com",
person_profiles: "identified_only",
});
</script>

0 comments on commit 6e93e41

Please sign in to comment.