Install Vercel Web Analytics - #183
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
Reviewer's GuideIntegrates Vercel Web Analytics into the Angular frontend via a new environment-aware AnalyticsService that is initialized at app startup and adds the @vercel/analytics dependency to the project. Sequence diagram for Vercel analytics initialization on app startupsequenceDiagram
actor BrowserUser
participant AngularFramework
participant AppComponent
participant AnalyticsService
participant VercelAnalytics
BrowserUser->>AngularFramework: Load Angular application
AngularFramework->>AppComponent: Instantiate root component
AngularFramework->>AnalyticsService: Instantiate via dependency injection
AnalyticsService->>AnalyticsService: constructor()
AnalyticsService->>VercelAnalytics: inject(mode)
VercelAnalytics-->>AnalyticsService: Analytics initialized
AppComponent->>AnalyticsService: Hold reference for lifecycle
AppComponent-->>BrowserUser: Render application with analytics enabled
Class diagram for Angular analytics integrationclassDiagram
class AppModule {
}
class AnalyticsService {
+AnalyticsService()
}
class AppComponent {
-themeService
-location
-viewportScroller
-analyticsService
-tooltipConfig
-locale
+AppComponent(themeService, location, viewportScroller, analyticsService, tooltipConfig, locale)
}
AppModule --> AnalyticsService : provider
AppComponent --> AnalyticsService : injects
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
152b9d4 to
50671f8
Compare
There was a problem hiding this comment.
Code Review
This pull request integrates Vercel Analytics into the application by adding the @vercel/analytics dependency and creating a new AnalyticsService. The service is registered in the AppModule and injected into the AppComponent for initialization. However, the current implementation of AnalyticsService will cause errors during Server-Side Rendering (SSR) because it calls the inject() function—which relies on browser-specific globals—without checking the execution platform. It is recommended to wrap the initialization logic with a check for isPlatformBrowser to ensure compatibility with SSR.
I am having trouble creating individual review comments. Click here to see my feedback.
frontend/src/app/services/analytics.service.ts (1-15)
The inject() function from @vercel/analytics is designed for browser-side execution as it interacts with DOM-specific globals like window. Since this application uses Server-Side Rendering (SSR) and Prerendering (as evidenced by the domino dependency and build:ssr scripts), calling this function directly in the constructor will cause errors (e.g., ReferenceError: window is not defined) when the code executes on the server.
You should wrap the initialization in a check using isPlatformBrowser and the PLATFORM_ID token to ensure it only runs in the browser environment.
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { isPlatformBrowser } from '@angular/common';
import { inject } from '@vercel/analytics';
import { environment } from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class AnalyticsService {
constructor(@Inject(PLATFORM_ID) private platformId: Object) {
if (isPlatformBrowser(this.platformId)) {
// Initialize Vercel Analytics with environment-aware configuration
inject({
mode: environment.production ? 'production' : 'development',
});
}
}
}
Vercel Web Analytics Integration
Successfully configured Vercel Web Analytics for the mempool Angular application.
Changes Made:
1. Created New File:
inject()function from@vercel/analyticspackage2. Modified Files:
Implementation Details:
The implementation follows best practices for Angular applications:
AnalyticsServicethat encapsulates the Vercel Analytics initialization logicprovidedIn: 'root'to ensure a single instance across the applicationAppComponentto ensure analytics are initialized as early as possible in the application lifecycleTechnical Approach:
Since Vercel Analytics doesn't have specific Angular instructions in their documentation, I used their vanilla JavaScript/TypeScript approach with the
inject()function. This is the recommended pattern for frameworks without dedicated integrations.The configuration passes a
modeparameter to control whether analytics run in development or production mode:Verification:
Next Steps:
To enable Web Analytics on Vercel:
The analytics will automatically start tracking page views and web vitals once the application is deployed and the feature is enabled in the Vercel dashboard.
Summary by Sourcery
Integrate Vercel Web Analytics into the Angular frontend and ensure it initializes on app startup with environment-aware configuration.
New Features:
Enhancements:
Build: