Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ const path = require('path');
const app = express();
const ejs = require('ejs');
const haikus = require('./haikus.json');
const { initializeAnalytics } = require('./lib/analytics');
const port = process.env.PORT || 3000;

// Initialize Vercel Web Analytics
initializeAnalytics();

Choose a reason for hiding this comment

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

P1 Badge Avoid initializing analytics twice per page load

Calling initializeAnalytics() here while also hardcoding the Insights snippet in the EJS templates causes two independent initialization paths for the same tracker in Vercel deployments. When both paths are active, page views/events can be counted twice, which will skew analytics and any experiment decisions based on that data. Keep a single integration path (either inject()-based setup or template script tags) to prevent duplicated telemetry.

Useful? React with 👍 / 👎.


app.use(express.static('public'));
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
Expand Down
25 changes: 25 additions & 0 deletions lib/analytics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Vercel Web Analytics Integration
*
* This module provides utilities for integrating Vercel Web Analytics
* with the Express application.
*/

const { inject } = require('@vercel/analytics');

/**
* Initializes Vercel Web Analytics
* Should be called once during application startup
*/
function initializeAnalytics() {
try {
inject();
console.log('Vercel Web Analytics initialized');
} catch (error) {
console.error('Failed to initialize Vercel Web Analytics:', error.message);
}
}

module.exports = {
initializeAnalytics,
};
Loading
Loading