Skip to content

Commit 3859940

Browse files
committed
feat: UrlTagCaptureService
1 parent a229d29 commit 3859940

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

src/app/app.component.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import {
2+
afterNextRender,
23
ChangeDetectionStrategy,
34
Component,
5+
inject,
46
ViewEncapsulation,
57
} from "@angular/core";
68
import { RouterOutlet } from "@angular/router";
9+
import { UrlTagCaptureService } from "./services/url-tag-capture.service";
710

811
@Component({
912
selector: "app-root",
@@ -13,5 +16,9 @@ import { RouterOutlet } from "@angular/router";
1316
changeDetection: ChangeDetectionStrategy.OnPush
1417
})
1518
export class AppComponent {
16-
title = "application";
19+
private readonly urlTagCaptureService = inject(UrlTagCaptureService);
20+
21+
constructor() {
22+
afterNextRender(() => this.urlTagCaptureService.captureAndClearTag());
23+
}
1724
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { isPlatformBrowser } from "@angular/common";
2+
import { inject, Injectable, isDevMode, PLATFORM_ID } from "@angular/core";
3+
import { Router } from "@angular/router";
4+
5+
@Injectable({ providedIn: "root" })
6+
export class UrlTagCaptureService {
7+
private readonly platformIdentifier = inject(PLATFORM_ID);
8+
private readonly router = inject(Router);
9+
10+
public captureAndClearTag(): void {
11+
if (!isPlatformBrowser(this.platformIdentifier)) {
12+
return;
13+
}
14+
15+
const searchParameters = new URLSearchParams(window.location.search);
16+
const tag = searchParameters.get("t");
17+
if (tag === null || tag.length === 0) {
18+
return;
19+
}
20+
21+
if (!isDevMode()) {
22+
import("posthog-js")
23+
.then(({ default: posthog }) => posthog.capture("url_tag_detected", { tag }))
24+
.catch((reason: unknown) => console.error(reason));
25+
}
26+
27+
this.router.navigate([], {
28+
queryParams: { t: null },
29+
queryParamsHandling: "merge",
30+
replaceUrl: true,
31+
});
32+
}
33+
}

0 commit comments

Comments
 (0)