I would like to keep track of page views count for each page that a user visits so I can only fire tags (via GTM) if the page view count is < 2.
This is the structure of my desired localStorage:
Key: {PageTitle1: pageViewCount, PageTitle2: pageViewCount, PageTitle3: pageViewCount}
And this is an example of how it could look like in reality:
PageViewCounter: {Home Page: 8, Blog: 64, Contact Page: 2}
On each page it loads, it would either increment the numerical value of the counter (if the Page Name already exists in the localStorage) or append the new page with value of 1 to the object (if the Page Name doesn't yet exist in the localStorage).
I have tried:
//If it doesn't, create it and set it to 1
else if (!localStorage.PageViewCounter) {
console.log('PageViewCounter does not exist, setting to 1...');
PageViewCounter = 1;
var PageTitle = document.title;
PageAndCounter = {
PageTitle: PageViewCounter
}
localStorage.setItem('PageViewCounter', JSON.stringify(PageAndCounter));
}
But the PageTitle doesn't get the value from the variable but rather saves it as PageTitle: 1. I have also tried putting it into an array like this:
PageAndCounter = {
[PageTitle: PageViewCounter]
}
But I am using Google Tag Manager and it won't allow me to use this feature.
Error at line 6, character 3: This language feature is only supported for ECMASCRIPT_2015 mode or better: const declaration.
If this isn't possible, I will just create a new localStorage entry for each page visited, but I assume that's not a good practice?