Reputation: 21
I have some menu, example:
<a href="/dashboard">Dashboard</a>
<a href="/list-book">List Table</a>
<a href="/book/category/">Category</a>
<a href="/book/detail/">Detail</a>
And I want to create Quick Actions dropdown menu with the most viewed. My jquery:
var mySession = window.sessionStorage, pageCount;
window.addEventListener('load', function(){
if(!mySession.getItem("pageCount")){
mySession.setItem('pageCount', 1);
} else {
pageCount = mySession.getItem("pageCount");
pageCount = parseInt(pageCount) + 1;
mySession.setItem('pageCount', pageCount );
}
console.log('page view count of current browsing session', mySession.getItem("pageCount"));
var x = pageCount;
console.log(x)
$.ajax({
type: 'POST',
url: '/dashboard',
data: {"count":x},
complete: function(r){
alert("success");
}
});
});
And my backend :
get "/dashboard" do |env|
render "views/dashboard.ecr"
end
post "/dashboard" do |env|
body = env.params.body
spawn do
data = {
"page" => "dashboard",
"count" => body["count"],
"user" => "admin",
}
check_page = DB1["page_count"].find_one({"page" => "dashboard"})
if check_page
DB1["page_count"].update({"page" => "dashboard"},
{
"$set" => {"count" => body["count"]},
})
else
DB1["page_count"].insert(data)
end
end
env.redirect "/dashboard"
end
But,this way count all page. I will count page per page every page is click by user. Can help me.
Upvotes: 0
Views: 76
Reputation: 4440
I would do it like this:
Opening the page the user make a GET request to the app -> route (for example '/dashboard'), accordingly, we can assume that one request as +1 for dashboard page count.
In the MongoDB you create collection with two keys: page_name
like dashboard_views_count
(for store page name) and views
(for increment count of views), then you need to learn operator $inc in MongoDB. This will give a simple way to increment counter.
After, we make middleware for catch requests. It help extract from env page path and write to MongoDB.
In result this will be look at:
P.S. Instead of Mongo I propose to consider Redis for counter store.
Upvotes: 1