Skip to content

Install Vercel Web Analytics - #183

Closed
Dargon789 wants to merge 0 commit into
masterfrom
vercel/install-vercel-web-analytics-v7gyuq
Closed

Install Vercel Web Analytics#183
Dargon789 wants to merge 0 commit into
masterfrom
vercel/install-vercel-web-analytics-v7gyuq

Conversation

@Dargon789

@Dargon789 Dargon789 commented May 11, 2026

Copy link
Copy Markdown
Owner

Vercel Web Analytics Integration

Successfully configured Vercel Web Analytics for the mempool Angular application.

Changes Made:

1. Created New File:

  • frontend/src/app/services/analytics.service.ts - New Angular service to initialize Vercel Analytics
    • Implements proper environment-aware configuration
    • Uses inject() function from @vercel/analytics package
    • Automatically switches between 'production' and 'development' modes based on environment configuration

2. Modified Files:

  • frontend/src/app/app.module.ts - Added AnalyticsService to the providers array
  • frontend/src/app/components/app/app.component.ts - Injected AnalyticsService into the root component to ensure it initializes on app startup
  • frontend/package.json - Added @vercel/analytics ^2.0.1 to dependencies (was already present, order normalized)
  • frontend/package-lock.json - Updated lock file to reflect dependency tree

Implementation Details:

The implementation follows best practices for Angular applications:

  1. Service-Based Architecture: Created a dedicated AnalyticsService that encapsulates the Vercel Analytics initialization logic
  2. Environment-Aware Configuration: The service automatically detects whether the app is running in production or development mode and configures analytics accordingly
  3. Singleton Pattern: The service uses providedIn: 'root' to ensure a single instance across the application
  4. Early Initialization: The service is injected into the root AppComponent to ensure analytics are initialized as early as possible in the application lifecycle

Technical 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 mode parameter to control whether analytics run in development or production mode:

  • Production mode: Full analytics tracking enabled
  • Development mode: Analytics can be tested without affecting production data

Verification:

  • ✅ TypeScript compilation passes without errors
  • ✅ Linter runs successfully with no new errors introduced
  • ✅ Package is properly installed (@vercel/analytics@2.0.1)
  • ✅ Service is properly registered in Angular's dependency injection system

Next Steps:

To enable Web Analytics on Vercel:

  1. Navigate to your Vercel dashboard
  2. Select the project
  3. Go to the Analytics section
  4. Click "Enable" to activate Web Analytics
  5. Deploy the application to see analytics data

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:

  • Add an Angular AnalyticsService that initializes Vercel Web Analytics with production or development mode based on environment settings.

Enhancements:

  • Register AnalyticsService in the root module and inject it into the root AppComponent to trigger early analytics initialization.

Build:

  • Add @vercel/analytics as a frontend dependency and update the package lockfile accordingly.

@snyk-io

snyk-io Bot commented May 11, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
🔚 Open Source Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@sourcery-ai

sourcery-ai Bot commented May 11, 2026

Copy link
Copy Markdown

Reviewer's Guide

Integrates 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 startup

sequenceDiagram
    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
Loading

Class diagram for Angular analytics integration

classDiagram
    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
Loading

File-Level Changes

Change Details Files
Introduce an Angular AnalyticsService that initializes Vercel Web Analytics with environment-aware configuration.
  • Create AnalyticsService as an Angular injectable singleton with providedIn: 'root'.
  • Use inject() from @vercel/analytics in the service constructor to initialize analytics.
  • Configure analytics mode dynamically based on the Angular environment.production flag.
frontend/src/app/services/analytics.service.ts
Wire AnalyticsService into the application bootstrap path so analytics initialize on app startup.
  • Register AnalyticsService in the app.module providers array alongside existing services.
  • Inject AnalyticsService into the root AppComponent constructor to trigger service instantiation early in the app lifecycle.
frontend/src/app/app.module.ts
frontend/src/app/components/app/app.component.ts
Add and normalize the Vercel Analytics dependency and update the lockfile.
  • Add @vercel/analytics ^2.0.1 to dependencies and slightly reorder existing dependencies for consistency.
  • Update package-lock.json to reflect the new dependency graph.
frontend/package.json
frontend/package-lock.json

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@Dargon789 Dargon789 closed this May 11, 2026
@Dargon789
Dargon789 force-pushed the vercel/install-vercel-web-analytics-v7gyuq branch from 152b9d4 to 50671f8 Compare May 11, 2026 07:51

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

high

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',
      });
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant