I want to build a visitor tracking system on my Shopify store that displays the same count to all users. Right now I have a basic JavaScript solution but it only works locally on each browser.
var categories = []; // collection names array
var click_counts = []; // tracking clicks per category
// Extract collection names from navigation
categories[0] = {{linklists[page.handle].links[0].title| json}};
categories[1] = {{linklists[page.handle].links[1].title| json}};
categories[2] = {{linklists[page.handle].links[2].title| json}};
categories[3] = {{linklists[page.handle].links[3].title| json}};
categories[4] = {{linklists[page.handle].links[4].title| json}};
categories[5] = {{linklists[page.handle].links[5].title| json}};
categories[6] = {{linklists[page.handle].links[6].title| json}};
categories[7] = {{linklists[page.handle].links[7].title| json}};
categories[8] = {{linklists[page.handle].links[8].title| json}};
// Initialize counter values
for(var j=0; j<9; j++){
click_counts[categories[j]] = 0;
}
// Handle click events
function trackClick(selected_item){
var threshold; // maximum clicks allowed
var display_element; // where to show the count
if(click_counts[selected_item] < max_clicks-1){
click_counts[selected_item] += 1;
document.getElementById(display_element).innerHTML = click_counts[selected_item]+" / " + threshold;
} else{
click_counts[selected_item] += 1;
document.getElementById(display_element).innerHTML = click_counts[selected_item] +" / " + threshold;
document.getElementById(selected_item).href = "NEW DESTINATION";
return true;
}
}
The problem is this only works for individual browsers. I need all visitors to see the same numbers updated in real time. I think this requires server side coding but I’m not sure how to save and load the counter data from the server. Any ideas on how to make this work across all users?