Seconding Noah on this one! His snippet would be the best move.
That said, if you were looking to go beyond that, there are lots of variables to consider here when tracking user actions, even with cookies or Local Storage.
As an arbitrary example, let’s say:
- A user goes to a
Recipes category page, and opens an article for
How to make Chocolate Cake
- The script you write would save the
Recipes page as the page to go back to within Local Storage
- Then, at the same time in a different tab, the user goes to the
Interviews category page and then clicks on an
Interview with Vlad article from there
- The script then overwrites your previous destination of
Recipes because it thinks that the user wants to go back to
Interviews now when they click the back button on any page on the website.
So, when the user clicks back on the
How to Make Chocolate Cake page, they get taken back to the
Interviews category page instead of the
Recipes category page, because the last action the user took in another tab (going to read an
Interview with Vlad from the
Interviews category page) overwrote what was supposed to happen, if that makes sense.
All this to say, there are lots of things you’d have to end up considering when writing the snippet and testing it. It can definitely be done, but if you’re not super familiar with JS, I would consider hiring a freelance dev to do it. (There are some good devs on the forums – just post to Freelance!)
Hope that helps!