-
Notifications
You must be signed in to change notification settings - Fork 1
feat(analytics): tag Plausible pageviews with foundation/project/lens #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
d9ba4ba
706ed86
1537d87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,9 +6,12 @@ import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; | |
| import { NavigationEnd, Router } from '@angular/router'; | ||
| import { environment } from '@environments/environment'; | ||
| import { PLAUSIBLE_DOMAIN, PLAUSIBLE_SRC } from '@lfx-one/shared/constants'; | ||
| import { PlausibleCall } from '@lfx-one/shared/interfaces'; | ||
| import { PlausibleCall, PlausiblePageviewContext } from '@lfx-one/shared/interfaces'; | ||
| import { filter } from 'rxjs'; | ||
|
|
||
| import { LensService } from './lens.service'; | ||
| import { ProjectContextService } from './project-context.service'; | ||
|
|
||
| /** | ||
| * Plausible analytics service for privacy-friendly page and event tracking. | ||
| * Uses Angular's afterNextRender for SSR-safe script loading. | ||
|
|
@@ -19,6 +22,17 @@ import { filter } from 'rxjs'; | |
| export class PlausibleService { | ||
| private readonly router = inject(Router); | ||
| private readonly destroyRef = inject(DestroyRef); | ||
| private readonly projectContextService = inject(ProjectContextService); | ||
| private readonly lensService = inject(LensService); | ||
|
|
||
| // Paths where context resolves asynchronously (public meeting page loads | ||
| // project data from the API after navigation). The auto-pageview is skipped | ||
| // for these and the owning component fires `trackPage()` once data settles, | ||
| // so we record one enriched event per visit instead of a context-free hit. | ||
| // Matches both upcoming-meeting URLs (`/meetings/<id>`) and past-meeting | ||
| // occurrence URLs (`/meetings/<id>-<13-digit-timestamp>`); both forms route | ||
| // to MeetingJoinComponent — see isPastMeetingOccurrenceId in that file. | ||
| private static readonly deferredPageviewPattern = /^\/meetings\/\d+(-\d{13})?$/; | ||
|
|
||
| private scriptLoaded = false; | ||
| private analyticsReady = false; | ||
|
|
@@ -117,7 +131,13 @@ export class PlausibleService { | |
| // only after the bundle has executed and replaced the queue stub. | ||
| script.onload = () => { | ||
| this.analyticsReady = true; | ||
| this.trackPage(); | ||
| // Skip the initial pageview on deferred paths so a refresh on a | ||
| // public meeting URL doesn't leak a context-free hit before the | ||
| // component fires its enriched pageview. | ||
| if (PlausibleService.deferredPageviewPattern.test(window.location.pathname)) { | ||
| return; | ||
| } | ||
| this.trackPage(this.buildContextProps()); | ||
|
Comment on lines
+149
to
+152
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't suppress the onload fallback before the deferred hit can actually be sent.
🤖 Prompt for AI Agents |
||
| }; | ||
|
|
||
| script.onerror = (error) => { | ||
|
|
@@ -149,14 +169,51 @@ export class PlausibleService { | |
| if (typeof document === 'undefined') { | ||
| return; | ||
| } | ||
| const path = this.getSanitizedPath(event.urlAfterRedirects); | ||
| if (PlausibleService.deferredPageviewPattern.test(path)) { | ||
| return; | ||
| } | ||
| this.trackPage({ | ||
| path: this.getSanitizedPath(event.urlAfterRedirects), | ||
| path, | ||
| url: this.getSanitizedUrl(), | ||
| title: document.title, | ||
| ...this.buildContextProps(), | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Build the foundation / project / lens custom-props for the current | ||
| * pageview from the active app state. Keys with empty values are omitted | ||
| * so Plausible doesn't store empty strings as a distinct dimension value. | ||
| * Returned as a Record so callers can spread into the broader pageview | ||
|
niravpatel27 marked this conversation as resolved.
Outdated
|
||
| * props payload that `trackPage` forwards to Plausible. | ||
| */ | ||
| private buildContextProps(): Record<string, unknown> { | ||
| const context: PlausiblePageviewContext = {}; | ||
| // Read foundation and project independently so the project lens emits | ||
| // both keys — letting the dashboard filter by either dimension. | ||
| const foundation = this.projectContextService.selectedFoundation(); | ||
| if (foundation?.slug) { | ||
| context.foundation = foundation.slug; | ||
| } | ||
| if (foundation?.name) { | ||
| context.foundation_name = foundation.name; | ||
| } | ||
| const project = this.projectContextService.selectedProject(); | ||
| if (project?.slug) { | ||
| context.project = project.slug; | ||
| } | ||
| if (project?.name) { | ||
| context.project_name = project.name; | ||
| } | ||
| const lens = this.lensService.activeLens(); | ||
| if (lens) { | ||
|
niravpatel27 marked this conversation as resolved.
Outdated
|
||
| context.lens = lens; | ||
| } | ||
| return { ...context }; | ||
| } | ||
|
|
||
| /** | ||
| * Build a privacy-safe URL string for Plausible. | ||
| * Strips query params and hash to avoid leaking auth tokens, OTPs, or | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.