Counting page visits with javascript and cookies - javascript

I would like to count a users visit to a site within a session and implement certain functionality based on how many pages the user has visited. I would like to perform this solely with the use of JavaScript and cookies (using js).
Essentially I would just like to do create var = 0 and ++ to that same var on each additional visit to another page within that users session.
What is the proper way to implement this?

The only reliable way will be to use AJAX and keep track on the server, associated with the user's session ID.
Each load will need to poll the server, then tell the server to increment the counter.

You could for example, when the user access the page, First check if the cookie exists, if not then set the cookie name and value and also a (token var) as being set so it dosnt increment on page refreshes.
Then one every other page you require the cookie, again check for cookie and then increment it.
I find a easy to use coolkie script Cookies use.

Related

Simplest way to count new visitors in a website

How can I code a counter of the total number of unique visitors in a website without using google analytics or other similar platforms. Is there a way to detect a new visitor in JavaScript and send it to my database? (I use firebase). How to detect the fact that a visitor already visited my site?
Thankyou.
Here's a few things you can do:
You can check the IP, that might work, but an IP can be shared by more than one user. A cookie could be a viable option, but a cookie can expire or be modified by the client.
In your case, if does not seem to be a big issue that the cookie is modified you could use a cookie in a case like this. When the page is loaded, check if there is a cookie, if there is not, create one and add a +1 to views. If it is set, don't do the +1.
Set the cookies expiration date to whatever you want it to be, week or day if that's what you want, and it will expire after that time. After expiration, it will be a unique user again!

How to get unique page views in javascript

I am trying to get unique page views like if the page is already visited it should not be counted.
So the solution I thought was like on each reload save the page path to a localStorage in an array and on next reload check if the page path already exists in the localStorage array dont increment else increment the count by 1. If there are 500 pages in website then we need to store all 500 page path in localStorage which would be high is what I felt. This is the scenario when the user opens all the 500 pages of the website
The requirement is we need to get all this data and send it along with form submission to a third party server.
Any better solutions?
Doing this client-side is sub-optimal. I suggest doing this on your server, based on IP and using a cookie on the client to track unique clients, so that you can evaluate which pages they have viewed etc.
If you absolutely have to do this client-side, using localStorage might be slower and/or problematic due to limitations. You could look into indexedDB, as well.

Tracking clicks of guest users, with some decent kind of uniqueness

Please be advised, this is not a code-problem question.
I have a resource in my app that is available only for authenticated users. In case a guest user tries to access the resource by clicking on it - registration modal shows up.
Now, I'm building logic using jQuery and PHP in order to store those clicks of guests. However, I also would like to implement something, that will let me retrieve rough information on unique users among the ones who clicked.
The idea I have is this:
When a specific page is loaded, set a JS cookie of "unique" value,
like this: [random string of fixed size][timestamp]. The cookie
would expire in a year from now. If the cookie already exists, don't
do anything.
When the guest clicks on the resource, make an Ajax
call to store the click AND the cookie value.
Later, to get "unique" clicks, make a SELECT that will GROUP BY
the cookie value.
Unless the cookies are cleared or the custom cookie expiration date is exceeded, this should give me a way to determine unique clicks (users) among guests.
I'm interested, if anyone can provide me with a better way to achieve what I need.
I wouldn't reinvent the wheel and use Google Analytics for this kind of task, since it's almost doing al the job and giving you nice graphics and statistics for free.

How to detect/stop a user opening a new browser window?

This is in context to an ASP.Net application. The application makes use of a specific data which is set for a page. After this data has been set all the operations from this page onwards use the set data.
The problem is that if the user opens another tab with a competing data it overwrites the older data for the same session and for the same user which invalidates the operations on the first tab.
I know the suggested way is to refactor the code to remove such coupling but that is not possible. Here's another thread that discussed this but didn't specify any solutions other than refactoring the code (http://stackoverflow.com/questions/632062/ways-to-detect-ctrl-n-or-when-a-user-opens-a-new-window)
So, how can I detect (and notify the user) or stop the user from opening another tab - through javascript/Jquery?
You could set a session variable isActive and set it to true, along with all the other session data when the user opens the application the first time. After this, if the user opens another tab, check to see if isActive is true. If it is, inform the user and don't set the data again.
In pseudo-code, your logic should flow like this
if (!isActive)
//set session data
else
//alert the user: You have another active session
This would be a better solution because there is no guarantee the user does not visit the page to set the session, then temporarily turn off Javascript to launch a new tab without you being notified.
You should realize that you cannot prevent multiple pages being open on the same site by the same user. A user can always do such an operation using multiple different browsers on the same computer or browsers on different computers. As such, what you really need to do is to design your application to either just handle this situation gracefully or detect such a conflict and decide what the safest action is to take when it occurs (chances are, at the server, you either ignore the data from all sessions but one or you somehow merge them all together). What the safe action is depends upon what the data is or how it was changed.
The most straightforward option is to coin a new server-based session for the user each time the user visits and, at the server, invalidate all previous sessions so any older session that tries to make any future updates to the server will be denied because of an invalid session. This prevents any sort of multi-session data conflict.
If you want to be able to inform the user when their session becomes invalid, you could do a slow poll of the server (say once every 20 mins) as long as the window is open and on your site to check the session validity such that you can inform the user when their session has expired.

How to setup visitor variables in Google Analytics

I don't quite get how to use the visitor level variables. Do I need to put them in some sort of condition to determine if the visitor is a repeat visitor, or do I just use the _SetCustomVar method like I would the page or session variables?
Google Analytics will determine if someone is the same visitor based on the presence of its cookies. So, to the extent that GA is able to differentiate Visitors (ie, same computer, same browser as past visits without having removed cookies), the custom variable will persist.
All you need to do to set a variable as 'visitor' is to set the fourth parameter (scope) to 1.
Just be sure to set the custom variable before your page-view call, otherwise the custom variable data doesn't get sent to Google.

Categories