From f0c3ad3df3981da3eeea9ef0ee0afcf56c4ef331 Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Thu, 2 May 2024 12:38:59 +0100 Subject: [PATCH 1/4] removing duplicated calls --- src/demos/chips/analytics-first-party.ejs | 2 -- src/demos/chips/analytics-third-party.ejs | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/demos/chips/analytics-first-party.ejs b/src/demos/chips/analytics-first-party.ejs index 9fa7065..f8dca2a 100644 --- a/src/demos/chips/analytics-first-party.ejs +++ b/src/demos/chips/analytics-first-party.ejs @@ -14,6 +14,4 @@ - - <%- include(commonPath + '/footer.ejs') %> diff --git a/src/demos/chips/analytics-third-party.ejs b/src/demos/chips/analytics-third-party.ejs index 71c698f..f889fef 100644 --- a/src/demos/chips/analytics-third-party.ejs +++ b/src/demos/chips/analytics-third-party.ejs @@ -14,6 +14,4 @@ - - <%- include(commonPath + '/footer.ejs') %> From a72f3adae0e36a325c200ec421669356b309bbfe Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Thu, 2 May 2024 12:39:41 +0100 Subject: [PATCH 2/4] commenting analytics script --- src/demos/chips/analytics.ejs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/demos/chips/analytics.ejs b/src/demos/chips/analytics.ejs index c2f6512..d1034f7 100644 --- a/src/demos/chips/analytics.ejs +++ b/src/demos/chips/analytics.ejs @@ -2,11 +2,11 @@ function ( window ) { // Track user interactions function trackInteraction( interaction ) { - if ( 'buttonClickedCHIPS' === interaction ) { - var remoteURL = '<%= protocol %>://<%= domainC %><% if (isPortPresent) { %>:<%= port %><% } %>/chips/trackCHIPS'; - } else { - var remoteURL = '<%= protocol %>://<%= domainC %><% if (isPortPresent) { %>:<%= port %><% } %>/chips/track'; - } + // Determine the remote URL based on the type of interaction + const baseURL = '<%= protocol %>://<%= domainC %><% if (isPortPresent) { %>:<%= port %><% } %>'; + const remoteURL = interaction === 'buttonClickedCHIPS' ? `${baseURL}/chips/trackCHIPS` : `${baseURL}/chips/track`; + + // Send the interaction data to the remote URL fetch( remoteURL, { method: 'POST', credentials: 'include', @@ -15,18 +15,27 @@ }, body: JSON.stringify( {interaction: interaction} ) } ).then( function ( response ) { + // Check if the response was successful if ( !response.ok ) { throw new Error( 'Network response was not ok' ); } + + // Return the response text return response.text(); } ).then( function ( data ) { - var statusDiv = document.getElementById( 'status' ); + // Get the status div + let statusDiv = document.getElementById( 'status' ); + + // If the status div exists, update its text and color if ( statusDiv ) { statusDiv.textContent = 'Interaction tracked!'; statusDiv.style.color = 'green'; } } ).catch( function ( error ) { - var statusDiv = document.getElementById( 'status' ); + // Get the status div + let statusDiv = document.getElementById( 'status' ); + + // If the status div exists, update its text and color if ( statusDiv ) { statusDiv.textContent = 'Interaction could not be tracked.'; statusDiv.style.color = 'red'; From 7cb65479af652686f7b5be6dc26dba50ba55444c Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Thu, 2 May 2024 12:40:13 +0100 Subject: [PATCH 3/4] commenting router script --- src/demos/chips/routes.js | 45 ++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/src/demos/chips/routes.js b/src/demos/chips/routes.js index fc23e2b..0b45c50 100644 --- a/src/demos/chips/routes.js +++ b/src/demos/chips/routes.js @@ -19,6 +19,7 @@ router.get('/analytics-third-party', (req, res) => { // Serve the analytics.js file to the site router.get( '/analytics.js', ( req, res ) => { + // Check if the analyticsId cookie is set let analyticsId = req.cookies.analyticsId; if ( !analyticsId ) { // Generate a new analytics ID @@ -26,59 +27,69 @@ router.get( '/analytics.js', ( req, res ) => { // Store the analytics ID in a cookie res.cookie( 'analyticsId', analyticsId, { + // Set the domain of the cookie to the current domain Domain: `.${res.locals.domainC}`, - maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days + // Set the max age of the cookie to 30 days + maxAge: 30 * 24 * 60 * 60 * 1000, + // Set the httpOnly flag to true to prevent the cookie from being accessed by JavaScript httpOnly: true, + // Set the sameSite flag to "none" to allow the cookie to be sent across different sites sameSite: "none", + // Set the secure flag to true to require the cookie to be sent over HTTPS secure: true } ); } + // Check if the analyticsId-chips cookie is set let analyticsIdCHIPS = req.cookies['analyticsId-chips']; if ( !analyticsIdCHIPS ) { + // If the analyticsId-chips cookie is not set, generate a new analytics ID analyticsIdCHIPS = uuid.v4(); + + // Store the analytics ID in a cookie res.cookie( 'analyticsId-chips', analyticsIdCHIPS, { + // Set the domain of the cookie to the current domain domain: `.${res.locals.domainC}`, - maxAge: 30 * 24 * 60 * 60 * 1000, // 30 days + // Set the max age of the cookie to 30 days + maxAge: 30 * 24 * 60 * 60 * 1000, + // Set the httpOnly flag to true to prevent the cookie from being accessed by JavaScript httpOnly: true, + // Set the sameSite flag to "none" to allow the cookie to be sent across different sites sameSite: "none", + // Set the secure flag to true to require the cookie to be sent over HTTPS secure: true, + // Set the partitioned flag to true to prevent the cookie from being shared across different partitions partitioned: true } ); } - // Set the appropriate content type and send the analytics code + // Set the Content-Type header to 'application/javascript' and render the analytics file res.set( 'Content-Type', 'application/javascript' ); res.render(path.join(__dirname,'analytics')); } ); +// Route to track user interactions router.post( '/track', ( req, res ) => { const {interaction} = req.body; const analyticsId = req.cookies.analyticsId; - if ( interaction && analyticsId ) { - - // Send success status for successful interaction tracking - res.status( 200 ).send( analyticsId ); - } else { - res.status( 400 ).send( 'Invalid request' ); - } + // If interaction and analyticsId are present, send a success status + // Otherwise, send a 400 status with an error message + interaction && analyticsId ? res.status(200).send(analyticsId) : res.status(400).send('Invalid request'); } ); +// Route to track user interactions for CHIPS router.post( '/trackCHIPS', ( req, res ) => { const {interaction} = req.body; const analyticsIdCHIPS = req.cookies['analyticsId-chips']; - if ( interaction && analyticsIdCHIPS ) { - - // Send success status for successful interaction tracking - res.status( 200 ).send( analyticsIdCHIPS ); - } else { - res.status( 400 ).send( 'Invalid request' ); - } + // If interaction and analyticsIdCHIPS are present, send a success status + // Otherwise, send a 400 status with an error message + interaction && analyticsIdCHIPS ? res.status(200).send(analyticsIdCHIPS) : res.status(400).send('Invalid request'); } ); +// Export the router module.exports = router; From 3465333cf4dc3628182f33d34d9aa1c507d56feb Mon Sep 17 00:00:00 2001 From: Fellyph Cintra Date: Thu, 2 May 2024 14:49:27 +0100 Subject: [PATCH 4/4] removing spaces from code --- src/demos/chips/routes.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/demos/chips/routes.js b/src/demos/chips/routes.js index 0b45c50..99b5e3a 100644 --- a/src/demos/chips/routes.js +++ b/src/demos/chips/routes.js @@ -75,10 +75,10 @@ router.get( '/analytics.js', ( req, res ) => { router.post( '/track', ( req, res ) => { const {interaction} = req.body; const analyticsId = req.cookies.analyticsId; - + // If interaction and analyticsId are present, send a success status - // Otherwise, send a 400 status with an error message - interaction && analyticsId ? res.status(200).send(analyticsId) : res.status(400).send('Invalid request'); + // Otherwise, send a 400 status with an error message + interaction && analyticsId ? res.status(200).send(analyticsId) : res.status(400).send('Invalid request'); } ); // Route to track user interactions for CHIPS @@ -87,8 +87,8 @@ router.post( '/trackCHIPS', ( req, res ) => { const analyticsIdCHIPS = req.cookies['analyticsId-chips']; // If interaction and analyticsIdCHIPS are present, send a success status - // Otherwise, send a 400 status with an error message - interaction && analyticsIdCHIPS ? res.status(200).send(analyticsIdCHIPS) : res.status(400).send('Invalid request'); + // Otherwise, send a 400 status with an error message + interaction && analyticsIdCHIPS ? res.status(200).send(analyticsIdCHIPS) : res.status(400).send('Invalid request'); } ); // Export the router