Skip to content

Commit 528836b

Browse files
authored
Merge pull request #23 from sfbrigade/jdunning/feat/posthog-test
[Closes #19] Add Posthog analytics
2 parents 8b2aeda + 2d148c1 commit 528836b

File tree

6 files changed

+22
-8
lines changed

6 files changed

+22
-8
lines changed

Diff for: .env.example

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
POSTHOG_API_KEY=fill_in_key_for_metrics

Diff for: .github/workflows/deploy.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
name: Deploy to GitHub Pages
22

33
on:
4-
# Runs on pushes targeting the default branch
4+
# Runs on pushes and PRs targeting the default branch
55
push:
66
branches: ["main"]
7+
pull_request:
8+
branches: ["main"]
79

810
# Allows you to run this workflow manually from the Actions tab
911
workflow_dispatch:
@@ -22,7 +24,7 @@ concurrency:
2224

2325
env:
2426
BUILD_PATH: "." # default value when not using subfolders
25-
# BUILD_PATH: subfolder
27+
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}
2628

2729
jobs:
2830
build:
@@ -74,6 +76,8 @@ jobs:
7476
path: ${{ env.BUILD_PATH }}/dist
7577

7678
deploy:
79+
# Only deploy from the main branch, or when manually triggered
80+
if: github.ref == 'refs/heads/main' || github.event_name == 'workflow_dispatch'
7781
environment:
7882
name: github-pages
7983
url: ${{ steps.deployment.outputs.page_url }}

Diff for: astro.config.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default defineConfig({
55
site: "https://www.sfcivictech.org",
66
base: "",
77
trailingSlash: "ignore",
8-
compressHTML: false,
8+
compressHTML: true,
99
integrations: [
1010
icon({
1111
iconDir: "src/assets/icons",

Diff for: src/components/Metadata.astro

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { meta: {
1616
1717
const tags = Object.entries(meta)
1818
.map(([key, value]) => metaTag(key, value))
19-
.join("\n");
19+
.join("\n\t\t");
2020
const ogTags = Object.entries({
2121
title,
2222
url,
@@ -28,9 +28,9 @@ const ogTags = Object.entries({
2828
})
2929
.map(([key, value]) => value ? ogMetaTag(key, value) : null)
3030
.filter(Boolean)
31-
.join("\n");
31+
.join("\n\t\t");
3232
---
3333

3434
<link rel="canonical" href={String(url)} />
35-
<Fragment set:html={tags} />
36-
<Fragment set:html={ogTags} />
35+
<Fragment set:html={tags} />
36+
<Fragment set:html={ogTags} />

Diff for: src/components/MetricsSnippet.astro

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
const phKey = import.meta.env.POSTHOG_API_KEY;
3+
---
4+
5+
<script is:inline define:vars={{ phKey }}>
6+
!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||[]);
7+
posthog.init(phKey, {api_host: 'https://us.i.posthog.com', person_profiles: 'identified_only'})
8+
</script>

Diff for: src/layouts/Page.astro

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { base } from "@/utils/urls";
33
import Metadata from "@/components/Metadata.astro";
44
import type { MetadataProp } from "@/components/Metadata.astro";
5+
import MetricsSnippet from "@/components/MetricsSnippet.astro";
56
import "@picocss/pico";
67
import "./global.css";
78
@@ -40,8 +41,8 @@ const baseURL = import.meta.env.BASE_URL.replace(/([^/])$/, "$1/");
4041
title="SF Civic Tech"
4142
href={new URL("rss.xml", Astro.site)}
4243
/>
43-
4444
<Metadata meta={meta} />
45+
<MetricsSnippet />
4546

4647
<title>{pageTitle}</title>
4748
</head>

0 commit comments

Comments
 (0)