Files
94425aa5-2830-41cb-94ae-2a2…/simulate_user_activity.js
2026-06-07 12:04:41 +00:00

155 lines
5.6 KiB
JavaScript

const BASE_URL = 'http://localhost:3000/api/track-search';
const casualSearchTerms = [
'travel ideas',
'vacation spots',
'cheap flights',
'hotels',
'trip planner',
'how to plan a trip',
'holiday packages'
];
const highIntentSearchTerms = [
'ItineraryGenie pricing',
'ItineraryGenie features',
'ItineraryGenie vs competitors',
'book luxury hotels Paris August 2024',
'AI trip planner reviews',
'best itinerary generator',
'ItineraryGenie demo request'
];
const casualPageViews = [
'/',
'/about',
'/blog',
'/contact'
];
const highIntentPageViews = [
'/',
'/#features',
'/#how-it-works',
'/#use-cases',
'/#project-insights',
'/#contact',
'/pricing'
];
const getRandomItem = (arr) => arr[Math.floor(Math.random() * arr.length)];
const getRandomDelay = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min;
async function sendData(endpoint, payload) {
try {
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
if (response.ok) {
console.log(`Successfully sent data to ${endpoint}:`, payload);
} else {
console.error(`Failed to send data to ${endpoint}. Status: ${response.status}`, payload);
const errorText = await response.text();
console.error('Error response:', errorText);
}
} catch (error) {
console.error(`Error sending data to ${endpoint}:`, error, payload);
}
}
async function simulateCasualUser(userId) {
console.log(`[Casual User ${userId}] Starting casual browsing session.`);
const sessionDuration = getRandomDelay(60000, 180000); // 1-3 minutes
const startTime = Date.now();
while (Date.now() - startTime < sessionDuration) {
const actionType = Math.random();
if (actionType < 0.6) { // 60% chance of a page view
const pagePath = getRandomItem(casualPageViews);
const payload = {
userId,
timestamp: new Date().toISOString(),
eventType: 'pageView',
pagePath: pagePath
};
await sendData(BASE_URL.replace('/track-search', '/track-pageview'), payload); // Assuming a separate pageview endpoint
console.log(`[Casual User ${userId}] Viewed page: ${pagePath}`);
} else { // 40% chance of a search
const searchTerm = getRandomItem(casualSearchTerms);
const payload = {
userId,
timestamp: new Date().toISOString(),
query: searchTerm,
intent: 'casual'
};
await sendData(BASE_URL, payload);
console.log(`[Casual User ${userId}] Searched for: "${searchTerm}"`);
}
await new Promise(resolve => setTimeout(resolve, getRandomDelay(5000, 15000))); // 5-15 second delay between actions
}
console.log(`[Casual User ${userId}] Casual browsing session ended.`);
}
async function simulateHighIntentUser(userId) {
console.log(`[High-Intent User ${userId}] Starting high-intent browsing session.`);
const sessionDuration = getRandomDelay(180000, 300000); // 3-5 minutes
const startTime = Date.now();
while (Date.now() - startTime < sessionDuration) {
const actionType = Math.random();
if (actionType < 0.4) { // 40% chance of a page view
const pagePath = getRandomItem(highIntentPageViews);
const payload = {
userId,
timestamp: new Date().toISOString(),
eventType: 'pageView',
pagePath: pagePath
};
await sendData(BASE_URL.replace('/track-search', '/track-pageview'), payload); // Assuming a separate pageview endpoint
console.log(`[High-Intent User ${userId}] Viewed page: ${pagePath}`);
} else { // 60% chance of a search
const searchTerm = getRandomItem(highIntentSearchTerms);
const payload = {
userId,
timestamp: new Date().toISOString(),
query: searchTerm,
intent: 'high-intent'
};
await sendData(BASE_URL, payload);
console.log(`[High-Intent User ${userId}] Searched for: "${searchTerm}"`);
}
await new Promise(resolve => setTimeout(resolve, getRandomDelay(3000, 8000))); // 3-8 second delay between actions
}
console.log(`[High-Intent User ${userId}] High-intent browsing session ended.`);
}
async function startSimulation(numCasualUsers = 2, numHighIntentUsers = 1) {
console.log(`Starting simulation with ${numCasualUsers} casual users and ${numHighIntentUsers} high-intent users.`);
const userPromises = [];
for (let i = 0; i < numCasualUsers; i++) {
const userId = `casual-${Math.random().toString(36).substring(2, 9)}`;
userPromises.push(simulateCasualUser(userId));
await new Promise(resolve => setTimeout(resolve, getRandomDelay(1000, 5000))); // Stagger user starts
}
for (let i = 0; i < numHighIntentUsers; i++) {
const userId = `high-intent-${Math.random().toString(36).substring(2, 9)}`;
userPromises.push(simulateHighIntentUser(userId));
await new Promise(resolve => setTimeout(resolve, getRandomDelay(1000, 5000))); // Stagger user starts
}
await Promise.all(userPromises);
console.log('All user simulations completed.');
}
// Run the simulation
startSimulation(3, 2); // Simulate 3 casual users and 2 high-intent users