Simplest way to count new visitors in a website - javascript

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!

Related

Store data for session with an expiration date

I am building a mobile application in Angular (Ionic 5 to be precise) and I need a way to keep session data during my users workflow.
I considered using the sessionStorage for that, but one important thing is that my user session should expire automatically after 5 minutes. So I would like to store data and reset those data if the user finish the workflow or reset them if the user didn't finished the workflow within 5 minutes.
What is the best way to do this?
Store the expiry time along with the data in sessionStorage and when reading the key, check the time to make sure it is still valid for usage.
See this article for reference.
I can't include a Stack Snippet here because it fails on security privileges (editing the localStorage etc.), so here's the link to try this on GitHub.
You can use the user reaction events(click, keypress, ...etc) to refresh your session and made any control you want ! something like (click)="refreshSession()". All your controls will be made inside the refreshSession()

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.

Is there a cross browser way to turn off cookies using Javascript?

Everyone of us knows the new law which is a pain for developers who are using cookies on their websites.
We have to ask our users if they allow us to create cookies on their machines. If they won't allow us this thing, how do we turn off cookies with javascript?
Is there a way to turn off cookies just for one website? Is there a compatible script which will work for all browsers?
What about 3rd parties cookies (Youtube, or Google Analytics? They are also stored in our website folder..)
Thank you!
You can't turn off cookies from the client. Your client code can stop creating new cookies, but even then the server could still be putting cookies on the page. If you want to stop creating cookies for a particular site, then you need to stop doing so in both your client code and your server code.
FYI, is it really cookies-per-se that are the problem - or is it more what you do with those cookies (like tracking a given user). For example, if you set a cookie just to remember what tab the current user was last on (with no other identifying information in the cookie and no use of that cookie on the server), is that a problem? Or, a cookie that stores a temporary session ID so that the server can keep track of the items in your shopping cart. I doubt these are issues because these aren't tracking or privacy issues.
As every country has their own laws in regard to this, if you are aiming for compliance with a particular country's laws, you will have to consult their exact laws.
I have no idea what all the different laws are across different countries, but the Dutch cookielaws aren't all that strict and most sites won't have to change a thing. You might want to look into the exact laws that apply to your website before you try to purge all cookie creations from it.
In short Dutch cookielaws come down to:
A website must ask permission to place cookies that track the user (this includes 3rd party cookies, such as cookies added by Google).
A website doesn't need to ask users for permission to place cookies that are required to run the website. An example of such a cookie is a cookie that keeps track of what you placed in your shopping cart.
Most implementations of this law that I have seen so far are similar to the one found on fok.nl. The first time the user opens the site a large popup is thrown into their face that will only let the user open the real website if they permit the site to place cookies. I assume this setting is then stored in a new cookie so the user never gets bothered with it again. This solution shouldn't be too difficult to implement.
Edit:
Here are some more examples of implementations: http://www.stormmc.nl/nieuws/nederlandse-voorbeelden-implementatie-cookiewet/ (just click the links).

Counting page visits with javascript and cookies

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.

Bookmarklet to make session cookies expire later

Background: There are several sites that I visit regularly on my iPhone that require me to log in. Unfortunately, the sites set cookies that expire at the end of the session. This unfortunately means I end up having to log in up to a dozen times per day.
I would like to write a bookmarklet that will go through and change all my 'expires at end of session' cookies to 'expires in 1 week.' I'm new to JavaScript/bookmarklets (I am a C++/C# programmer)... so I have a feeling this might take me a while. I just wanted to consult with experts who might be able to tell me if this is feasible/possible at all to begin with. I don't want to end up wasting my time if it's not even possible to accomplish this via JavaScript.
(and if it matters, I use 'Atomic Browser' on the iPhone which does have bookmarklet support)
Well, there are bookmarklets that allow you to view all cookies set by the current site, so I don't see any reason why this wouldn't be possible. If anything, you'd just need to enumerate/grab all the cookies, and then re-set them using a new expiration time/date.

Categories