User Vue Router to Count Number of Views Visited per session

I am looking to count the number of views a visitor viewed, and change certain button actions based on that count.

I was going to use cookies and just count +1 to the cookie value against each route in router file. But I am using history mode and am fairly sure there is a better way to do this?

If it’s just a session count, then using sessionStorage or even just a global JS variable and attaching the event to a router navigation guard would be enough. If you want to track unique views, then store the route as the key to check if it already exists - this would also be a good way to track how many times each page gets hit.

Thanks for your help. I have gone for the prototype option, and set Vue.prototype.$pageCount = 1 in main js. My Vue router already uses beforeEnter, so was going to update that variable through that.

I am not sure how to get vue router to see $pageCount though? It is visible in the components, but not in the router.js file. how would I make it available there?

Thanks

You could export your main Vue instance and import it into your router.

You could also just use a seperate module for this. I don’t see why it needs to be attached to the Vue instance.