Skip to content

Commit c875f5c

Browse files
committedAug 6, 2024·
func(Posthog): Add for both the SPA and the public facing blade templates. Only execute for production.
1 parent e223c11 commit c875f5c

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed
 

‎package-lock.json

+59
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"font-awesome": "^4.7.0",
2626
"markdown-it": "^13.0.2",
2727
"moment": "^2.29.4",
28+
"posthog-js": "^1.154.4",
2829
"vue3-apexcharts": "^1.4.4",
2930
"web3": "^4.1.2"
3031
}

‎plugins/posthog.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//./plugins/posthog.js
2+
import posthog from "posthog-js";
3+
4+
export default {
5+
install(app) {
6+
app.config.globalProperties.$posthog = posthog.init(
7+
"phc_Ab0TTrZPUF9LHNRWYgtGO6OkhrJ9My5nlCqmQF09JY5",
8+
{
9+
api_host: "https://us.i.posthog.com",
10+
person_profiles: "identified_only",
11+
capture_pageview: false,
12+
}
13+
);
14+
},
15+
};

‎resources/js/app.js

+13
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@ import { ZiggyVue } from "../../vendor/tightenco/ziggy/dist/vue.m";
88
import moment from "moment";
99
import Bugsnag from "@bugsnag/js";
1010
import BugsnagPluginVue from "@bugsnag/plugin-vue";
11+
import posthog from "posthog-js";
1112

1213
import "font-awesome/css/font-awesome.css";
1314

1415
const appName = import.meta.env.VITE_APP_NAME || "Laravel";
1516
const bugsnagApiKey = import.meta.env.VITE_BUGSNAG_API_KEY;
1617

18+
posthog.init(import.meta.env.VITE_POSTHOG_API_KEY, {
19+
api_host: import.meta.env.VITE_POSTHOG_HOST,
20+
});
21+
1722
createInertiaApp({
1823
title: (title) => `${title} - ${appName}`,
1924
resolve: (name) =>
@@ -54,4 +59,12 @@ if (process.env.NODE_ENV === "production") {
5459
page_location: event.detail.page.url,
5560
});
5661
});
62+
63+
router.on("navigate", (event) => {
64+
posthog.capture("$pageview", {
65+
distinct_id: posthog.get_distinct_id(),
66+
url: event.detail.page.url,
67+
referrer: document.referrer,
68+
});
69+
});
5770
}

‎resources/views/app.blade.php

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@routes
1818
@vite(['resources/js/app.js', "resources/js/Pages/{$page['component']}.vue"])
1919
@inertiaHead
20+
2021
</head>
2122

2223
<body class="font-sans antialiased">
@@ -35,6 +36,9 @@ function gtag() {
3536
gtag('config', '{{ env("GOOGLE_TAGMANAGER_ID") }}');
3637
</script>
3738
@endproduction
39+
40+
41+
3842
</body>
3943

4044
</html>

‎resources/views/public.blade.php

+28
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,34 @@
2020

2121
@yield('scripts')
2222

23+
@production
24+
<script>
25+
! function(t, e) {
26+
var o, n, p, r;
27+
e.__SV || (window.posthog = e, e._i = [], e.init = function(i, s, a) {
28+
function g(t, e) {
29+
var o = e.split(".");
30+
2 == o.length && (t = t[o[0]], e = o[1]), t[e] = function() {
31+
t.push([e].concat(Array.prototype.slice.call(arguments, 0)))
32+
}
33+
}(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);
34+
var u = e;
35+
for (void 0 !== a ? u = e[a] = [] : a = "posthog", u.people = u.people || [], u.toString = function(t) {
36+
var e = "posthog";
37+
return "posthog" !== a && (e += "." + a), t || (e += " (stub)"), e
38+
}, u.people.toString = function() {
39+
return u.toString(1) + ".people (stub)"
40+
}, 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 getNextSurveyStep onSessionId".split(" "), n = 0; n < o.length; n++) g(u, o[n]);
41+
e._i.push([i, s, a])
42+
}, e.__SV = 1)
43+
}(document, window.posthog || []);
44+
posthog.init('{{ env("POSTHOG_API_KEY") }}', {
45+
api_host: '{{ env("POSTHOG_HOST") }}',
46+
person_profiles: 'identified_only'
47+
})
48+
</script>
49+
@endproduction
50+
2351

2452
</head>
2553

0 commit comments

Comments
 (0)
Please sign in to comment.