From 44f0e7f9f7cff8939dc2f7da2e4d9cc1301bcc55 Mon Sep 17 00:00:00 2001 From: ScarletFlash Date: Sun, 12 Jul 2026 18:40:31 +0100 Subject: [PATCH 1/2] build: setup PostHog UI host --- .github/workflows/deploy.yml | 2 ++ angular.json | 3 ++- src/app/initializers/posthog-analytics.initializer.ts | 6 ++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index a4114ea..07e8144 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -48,10 +48,12 @@ jobs: env: POSTHOG_PROJECT_TOKEN: ${{ vars.POSTHOG_PROJECT_TOKEN }} POSTHOG_API_HOST: ${{ vars.POSTHOG_API_HOST }} + POSTHOG_UI_HOST: ${{ vars.POSTHOG_UI_HOST }} run: >- pnpm run build:prod --define "process.env.POSTHOG_PROJECT_TOKEN='$POSTHOG_PROJECT_TOKEN'" --define "process.env.POSTHOG_API_HOST='$POSTHOG_API_HOST'" + --define "process.env.POSTHOG_UI_HOST='$POSTHOG_UI_HOST'" - name: Upload source maps to PostHog env: diff --git a/angular.json b/angular.json index e2c696f..66254d0 100644 --- a/angular.json +++ b/angular.json @@ -80,7 +80,8 @@ }, "define": { "process.env.POSTHOG_PROJECT_TOKEN": "\"\"", - "process.env.POSTHOG_API_HOST": "\"\"" + "process.env.POSTHOG_API_HOST": "\"\"", + "process.env.POSTHOG_UI_HOST": "\"\"" } }, "configurations": { diff --git a/src/app/initializers/posthog-analytics.initializer.ts b/src/app/initializers/posthog-analytics.initializer.ts index f39a53a..cd6c47f 100644 --- a/src/app/initializers/posthog-analytics.initializer.ts +++ b/src/app/initializers/posthog-analytics.initializer.ts @@ -16,6 +16,11 @@ export function initializePosthogAnalytics(): void { throw new Error("POSTHOG_API_HOST is not configured at build time."); } + const uiHost = process.env["POSTHOG_UI_HOST"]; + if (uiHost === undefined || uiHost.length === 0) { + throw new Error("POSTHOG_UI_HOST is not configured at build time."); + } + const platformIdentifier = inject(PLATFORM_ID); if (!isPlatformBrowser(platformIdentifier)) { return; @@ -27,6 +32,7 @@ export function initializePosthogAnalytics(): void { .then(({ default: posthog }) => posthog.init(projectToken, { api_host: apiHost, + ui_host: uiHost, defaults: "2026-05-30", }), ) From 5288a303dcba7f4cf69d7dc95bac4ef0d4324095 Mon Sep 17 00:00:00 2001 From: ScarletFlash Date: Sun, 12 Jul 2026 19:01:51 +0100 Subject: [PATCH 2/2] feat: UrlTagCaptureService --- src/app/app.component.ts | 9 +++++- src/app/services/url-tag-capture.service.ts | 33 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/app/services/url-tag-capture.service.ts diff --git a/src/app/app.component.ts b/src/app/app.component.ts index d6d4a6e..1b6f563 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,9 +1,12 @@ import { + afterNextRender, ChangeDetectionStrategy, Component, + inject, ViewEncapsulation, } from "@angular/core"; import { RouterOutlet } from "@angular/router"; +import { UrlTagCaptureService } from "./services/url-tag-capture.service"; @Component({ selector: "app-root", @@ -13,5 +16,9 @@ import { RouterOutlet } from "@angular/router"; changeDetection: ChangeDetectionStrategy.OnPush }) export class AppComponent { - title = "application"; + private readonly urlTagCaptureService = inject(UrlTagCaptureService); + + constructor() { + afterNextRender(() => this.urlTagCaptureService.captureAndClearTag()); + } } diff --git a/src/app/services/url-tag-capture.service.ts b/src/app/services/url-tag-capture.service.ts new file mode 100644 index 0000000..26d7a22 --- /dev/null +++ b/src/app/services/url-tag-capture.service.ts @@ -0,0 +1,33 @@ +import { isPlatformBrowser } from "@angular/common"; +import { inject, Injectable, isDevMode, PLATFORM_ID } from "@angular/core"; +import { Router } from "@angular/router"; + +@Injectable({ providedIn: "root" }) +export class UrlTagCaptureService { + private readonly platformIdentifier = inject(PLATFORM_ID); + private readonly router = inject(Router); + + public captureAndClearTag(): void { + if (!isPlatformBrowser(this.platformIdentifier)) { + return; + } + + const searchParameters = new URLSearchParams(window.location.search); + const tag = searchParameters.get("t"); + if (tag === null || tag.length === 0) { + return; + } + + if (!isDevMode()) { + import("posthog-js") + .then(({ default: posthog }) => posthog.capture("url_tag_detected", { tag })) + .catch((reason: unknown) => console.error(reason)); + } + + this.router.navigate([], { + queryParams: { t: null }, + queryParamsHandling: "merge", + replaceUrl: true, + }); + } +}