function collectQueryStringMetrics() {
const metrics = {};
// Function to extract and process a query string parameter
function processQueryString(key, value) {
if (key && value) {
// Sanitize key and value (optional, but recommended)
const sanitizedKey = key.replace(/[^a-zA-Z0-9_-]/g, '_'); // Replace invalid chars with underscore
metrics[sanitizedKey] = {
count: (metrics[sanitizedKey] || 0) + 1,
values: metrics[sanitizedKey].values || [],
};
metrics[sanitizedKey].values.push(value);
}
}
// Get the query string parameters
const queryString = window.location.search;
if (queryString) {
const params = queryString.split('&');
for (const param of params) {
const [key, value] = param.split('=');
processQueryString(key, value);
}
}
return metrics;
}
// Example usage (you'll need to adapt this to your environment)
const metrics = collectQueryStringMetrics();
// Log the metrics (or send them to a server)
console.log(metrics);
// Optionally, you can implement a periodic reporting mechanism
// setInterval(() => {
// console.log("Metrics at:", new Date());
// console.log(metrics);
// }, 60000); // Log every minute
Add your comment