Skip to content

Instantly share code, notes, and snippets.

@Sara3
Created October 13, 2017 23:48
Show Gist options
  • Save Sara3/0117a35bb64bf54de2bb5501ca8b9834 to your computer and use it in GitHub Desktop.
Save Sara3/0117a35bb64bf54de2bb5501ca8b9834 to your computer and use it in GitHub Desktop.
//array of two objects [{name, time}, {name, time}, {name, time}]
//# pageViews=> how many times a page is viewed
//# of users => have many different users have used the web
//# sessions => how many have viewed the page with half an hour time break in between
function main(){
//example array
var pageView = [{'name':'Sara', 'time': 1}, {'name': 'John', 'time':2},{'name':'Sara', 'time': 1}, {'name': 'Hadis', 'time':2},{'name':'Sara', 'time': 1}, {'name': 'Hadis', 'time':33}]
// var pageView = [{name: 'sara', 'time': 1}]
//output object
var output={'pageViews': 0, 'users': 0, 'sessions': 0}
//get the # of pageViews => easy. It is the length of the array
output.pageViews = pageView.length;
// # of different/unique users
//loop through the array
//make a new array to put the unique names in there
var count = 0;
var uniqueNames = {}
for(var i = 0; i <pageView.length; i++) {
//loop through uniqueNames and see if the current name is already in the uniqueNames array
if(!uniqueNames[pageView[i].name]){
uniqueNames[pageView[i].name]=pageView[i].name;
count++;
}
}
// console.log(uniqueNames)
output.users = count;
return output;
}
console.log(main());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment