How to count unique visitors on webpage - javascript

I want to know how to count unique visitors on webpage with Javascript (ReactJS + NodeJS(Express) i mean when user loads page it should send user's data to validate and if it is unique add it to database. there are this options (what i figured out)
Use client's IP address as validator.
Set Cookie
first one is not good because many device can share same network and many visitors will be lost.
Second one is not good option because cookie can be manipulated & deleted (if it is httpOnly then only deleted)
So what is best way to count visitors? my aim is to make something like . this

You can add a field in your database schema in which you can update it by adding 1 or something similar when a user visits a page. If you want to go further you can then store the user IP in the database or use cookies to avoid counting page visits for the same person multiple times.

You can use e.g. Google Analytics and their core reporting API. After you included GA in your site, the query explorer will get you started.

Related

How to track users across pages without using a cookie and without writing backend code?

I need to track users across pages without using a cookie. I do not have access to the web server, so my solution has to run from some JS each page can load.
Currently I am using a horrible solution of IP + timestamp.
So his navigation might look like this:
13:21:11 /index.php
13:21:15 /shop.php
13:21:35 /blue-shirt-1.php
Currently to track his session I have some logic like unique IP + navigated around in the past 10 minutes.
This is fine if every user visits the site once, and only for 10 minutes, or visits very infrequently, also for 10 minutes. But I need to be able to understand these two scenarios:
If he sits at /blue-shirt-1.php for half an hour, and then clicks another link, that should be considered a part of the same session.
If he closes his browser or navigates to a different website, and then comes back 5 minutes later, that should be considered a different session.
Is there any way to do this?
Note I can't use cookies because of a GDPR compliance issue.
Any ideas greatly appreciated.
An alternative to cookie-based sessions could be an URL-based session.
You could generate your own sessionids and use them as parameters like index.php?id=XXX and if ther is no id given you could generate a new one.
You should also take a look at PHPs URL based session management: http://www.php.net/manual/en/session.configuration.php#ini.session.use-trans-sid
Note that in this case the user could fake a new sessionid or use the id of an other user. For this
you could track the ids with the users ip and hold them after leaving
one page for a moment (for example by using onbeforeunload) and if the
given sessionid does not exist you could generate a new one, too...

Tracking Devices On same network or having same IP with php

I'm making a conversion tracking page with postback in php, for the same I need to generate a unique transaction ID on unique click.
So for tracking unique clicks I'm tracking the user's IP address with $_SERVER['REMOTE_ADDR'] and generating transaction ID by md5($_SERVER['REMOTE_ADDR']) but there is a problem. Suppose there are some users using a WiFi network or due to some other reasons they have the same IP address so the transaction ID which will be generated would be same for them because of which I will not be able to track the conversion while actually in happened because of different devices.
Can anybody suggest to me the way to resolve this problem with php or javascript?
You could generate an ID of some sort and put a cookie on their machine containing that ID that will identify that browser.
This isn't perfect, but it might get you what you need.
Another way it to fingerprint the device and store the value. For example you can use https://github.com/jackspirou/clientjs to do what you need. There are a few other libraries out there that a google search will turn up.
Another possible way.
echo getenv("username");
The username used in windows.
But cookie is better as suggested earlier

Store if user has pressed button at any point before

I am making an image gallery, and I wish to have a vote up button people can press to increment the number of votes an image has received. Obviously I don't want people spamming the vote up button, so I want to limit each user to 1 vote up.
I do not have access to any kind of database (except writing to files), and a login system would not be good for my target audience.
What is the best way to store whether a user has visited the page before? Would it be better to use PHP or JavaScript or something else for this?
Without an authentication system, you can limit the action of a user temporarily, but the user can trick your system to vote multiple times.
Here are solutions not using a login/auth system (and their drawbacks) :
You can store the upvote action client-side with cookies, localstorage or any other mean, but the user can clear all off that to regain the right to vote again. For most people it can be ok (depends on your audience), but any techy guy wanting to cheat the system will be able to do it.
You can store the update action server-side with a reference such as the user-agent or the IP adress. But these "authentication" system are not reliable : user can share the same user-agent and change them easily. Several user can share the same IP adress and use proxy to change their IP adress.
The third solution is to use an external system : a facebook +1 voting system (facebook uses its own auth system) or google+ or other external services. User won't be able to trick the system, but you don't own the upvote count on you side and someone without a facebook or google+ account will not be able to vote.
Personally I would go and find myself a database... but if you don’t want to use one, you can still write to a file. For example: Create an array and store the IP address of the user in it. Than write this array as JSON code to a file (json_encode) and store it. When a user clicks the like button, read the file, decode the json (json_decode) and check if the IP address exists in the file. If not: add to array and store to the file. The amount of likes, is equal to the amount of IP addresses in the JSON object.
The best way to implement this, is to make an ajax call when the like button is pressed. Than the visitor won’t see a page load.
Note: Technically you can set a cookie, to "remember" that the user already clicked the button. That would save you some reading of the file in which the likes are stored.

How to know an ip address has viewed all the links?

Suppose, I have 200 links and if the visitors visits continuously all the links then I want to offer a free pdf book
after last link but if someone directly visits last link then offer should not display.
In this case the user may visit the site per month or any long time to complete the tutorial site (this site) and the
user may delete the cookies so I should not use local storage or something like that.
So, I'm pulling an option with the users' ip address and if that ip's users completes the whole page visit the offer should be displayed.
So, how to decide if an ip address has viewed all the pages and if the user is at last link then display offer.
Does it have a login feature? If so I would track page views by url and userId.
The shortcoming of ip address is that different people using the same computer will count towards the same tracking. Also, you will not get credit if you visit the site from two different locations.
It is not possible to track the information with only the ip address. The IP address can change everytime the user reconnects to the internet e.g. reboot router. You will have to provide user login feature so you can associate the link visits with that user account.
The usual way to do this would be as follows using some server-side storage:
When a browser hits your server on any of the pages you are tracking, you see if there's already a tracking cookie in the browser. If not, you coin a unique ID for this browser and put it in a cookie that you set into that browser. Make sure the path allows visibility of the cookie anywhere on the site and set the expiration for however long you want.
In your server-side database, create an entry for this cookie ID and record that the page that was just hit has now been seen by this cookie ID.
On any subsequent page hit, get the cookie ID, look it up in the database, record that this page has now been viewed by that ID and check if all the required pages have now been viewed by that ID. If so, add the special offer to the delivery of the current page.
Using a cookied ID like this avoids issues with multiple browsers sharing a single IP address (which even happens on home networks and happens all the time on corporate networks).
If your site has a user login, it's even better to use the login ID as the user identifier because that allows you to accumulate the browsing history of the user even if they use multiple browsers/multiple computers as long as they login first.
FYI, some of the logic above can also be implemented via ajax calls made from the client upon each page load rather than work done at the time of serving the page - though this adds an extra server request for each page.

How do I know how long a user or a single IP address spent on a page of my site?

How do I know how long a user or a single IP address spent on a page of my site?
Can I use any event of JavaScript?
This is what Google Analytics is for. It keeps track of visitors to your website and reports statistics to you like such as length of visit. They even offer an API to help you get this information.
To implement inside your application
When a user loads a page, their "last page load time" is updated in the database.
A cron script runs every minute. It looks for users whose "last page load time" was in the last minute.
Increment the "time spent on site" for each user found.
In this way, I can keep track of users accurately to the minute. Just divide by 60 to get the hours.
Else
Google Analytics. You can check out it's features here.
I will go for a Redis installation that keeps tracks of a specific hash created by a combination of IP address, USERAGENT and login information (if you have it).
Save the stuff in Redis with a timestamp (use hashes or whatever you like) and then add a "farewell" timestamp when the user logs out or leaves the site for an external link or unloads the page.
Use Ajax to send keep-alive information to your backend. Save it as session data in your database.
The session data can save the current page the user is watching and was active last time. Send the keep-alive information using, for example, setInterval every x seconds (depending on resolution needs).
There is no need for cron...

Categories

Resources