Skip to content
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

Update CHIPS code comments #68

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 0 additions & 2 deletions src/demos/chips/analytics-first-party.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@

<script src="<%= protocol %>://<%= domainA %><% if (isPortPresent) { %>:<%= port %><% } %>/chips/analytics.js"></script>

<script src="<%= protocol %>://<%= domainA %><% if (isPortPresent) { %>:<%= port %><% } %>/chips/analytics.js"></script>

<%- include(commonPath + '/footer.ejs') %>
2 changes: 0 additions & 2 deletions src/demos/chips/analytics-third-party.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,4 @@

<script src="<%= protocol %>://<%= domainC %><% if (isPortPresent) { %>:<%= port %><% } %>/chips/analytics.js"></script>

<script src="<%= protocol %>://<%= domainC %><% if (isPortPresent) { %>:<%= port %><% } %>/chips/analytics.js"></script>

<%- include(commonPath + '/footer.ejs') %>
23 changes: 16 additions & 7 deletions src/demos/chips/analytics.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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';
Expand Down
45 changes: 28 additions & 17 deletions src/demos/chips/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,66 +19,77 @@ 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
analyticsId = uuid.v4();

// 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');
fellyph marked this conversation as resolved.
Show resolved Hide resolved
} );

// 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');
fellyph marked this conversation as resolved.
Show resolved Hide resolved
} );

// Export the router
module.exports = router;