Hi All,
Our site has multiple dynamically generated pages. I want a user to be able to view those pages 5 times, after which they’ll need to log in. At the moment the site is set up so that their second view of the dynamic page requires them to log in and will display the info should they be logged in.
The code is as follows:
$w.onReady(function () {
if ((isLoggedIn == false) && (!local.getItem("visitedModelGuideBefore"))) {
// First visit
local.setItem("visitedModelGuideBefore", "yes");
} else if (isLoggedIn == true) {
// Let user view unlimited price guides if they're logged in
$w("#freeView").collapse()
}
})
Questions
-
How can I enable a user to view 5 price guides before asking them to log in?
-
Will this method work across multiple dynamic pages e.g. if they view 5 different dynamic pages it should trigger rather than only trigger if they view 1 dynamic page 5 times
Thank you thank you
A dynamic generated page is nothing else than a data-row inside your database.
10 data-rows inside your database = ??? RIGHT ! → 10 dynamic-pages you can zap trough.
To be able to count these data-rows for each user, you will have to do following steps using code…
- add a further data-field inside your database → call it for example → maxViews
- get the current user’s ID → you will need this to be able to defferentiate …
- generate the code which would COUNT-UP everytime, when the user clicks the button to open and read the file.
- Of course the update of this counter will be saved inside your DB again in the “maxViews” - DB-Field.
- You also will have to add an IF-ELSE-QUERY to check the max number of tries.
For example:
if(...) {...do something...} else {...do something...}
Et viola!!! You have generated a BUTTON-COUNTER which is able to count for each user.
Thanks @russian-dima
Is there not a way to loop through the local storage and see how many times a user has viewed a price guide? My concern with the method above is how to avoid having a separate column for each user that views one of the dynamic pages or are you suggesting a new table that contains the userID and the number of views of any dynamic page each individual has had?
At least you will need a counter if you want to do it for each of your user and you will have to store this counter somewhere for each user.
If you do it as GENERAL, that means nor for each user, then you can modify your old code and use the wix-storage …
local . setItem ( “visitedModelGuideBefore” , “yes” );
But changing this to a counterfunction (counting-up and checking function using wix-storage)
With you on the first bit. The challenge there is not all users will be logged in so I can’t generate a userID for each of them without that right?
Re your second point of if I do it using storage generally I can’t do it for each user. I thought that storage was unique to each user rather than aggregated across all users? Second question: is storage in this instance with dynamic pages looking at just the storage for that dynamic page instance or across all dynamic pages of that type?
I thought that storage was unique to each user rather than aggregated across all users?
Good question!
https://www.wix.com/velo/reference/wix-storage/introduction
I even can’t get any answer onto your question regarding the VELO-API-DOCS.
But you can test it. (i am not sure, so i won’t give an answer to this question, i would just test it and find out what is right and what is wrong).
If it is USER-INDIVIDUAL (UNIQUE) → then you can use it for your purposes.
- counting button-clicks and saving data inside LOCAL-storage.
- Of course a check-function should also run on the triggered click-action.
- Start checking function on every-click and if counter === 5 {do something}.
You even can do it for every individual item using the wix-storage, but as long as memory of storage do not expire (max 50kb).
-
write a separate CHECKING–RETURN-FUNCTION, which will return counter-result, each time when you click button and compare the result with your settep-up max-Count-Value.
-
if counter === 5 show message for LOGIN.