How to get unique page views in javascript - 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.

Related

PHP passing session id between different devices

My problem is simple.
I have two users on two different devices accessing the same servers pages (page 1 and page 2 respectively ).
I want to use php forms in one page to change the second users page.
(i.e. 1st user on page 1 presses a button and changes the image on 2nd users page 2 , on a separate device).
I thought I could do it by using the 2nd users session ID in the form on the 1st persons page, and fool the server to reload the 2nd users page with the updated info.
But i can see no example of it anywhere on the web. Is this possible or should I just use Ajax?
Thanks for any help.
I would suggest not hacking Session ID's like that, they're meant to identify a single user.
If you want data shared between users it's likely you'll be well served using a database, such as MySQL as commonly runs along PHP.

How to create a table in javascript and hold in memory for use on another page within a web application

I have two html files which are the two pages in my application
Page 1 = Home.html
Page 2 = Stats.html
When Page 1 is loaded, I am making AJAX calls to the Facebook API which returns some data and then I want to build a table using that data.
I then want to keep the table in memory and append it to a div on Page 2 when the user navigates to Page 2.
In this way, the wait time for the user is drastically reduced because in the time it takes them to navigate from Page 1 to Page 2, the majority of the work by the browser has been done.
Stats.html page should look into cached data which is fetched from Home.html. This is technically perfectly possible.
Before I give some details, ensure that the code that fecthes data from Facebook API is the same used between your two pages. The idea is to create a function that:
Looks for cached table data and retrieve it if found
If not found, fetch data from Facebook API and store it in the cache
Return the table data
Remember clearing or overriding the cache time to time.
In order to properly store data, there are two main ways:
Cookies
This is the traditional way to store data across a website. These are sent to your server at each request, which is not actually necessary but still helps you client-side. Cookies are controversial because subjects to many security flaws, so I wouldn't recommend using them.
Local storage
This is more modern in browsers, but less compatible with old browsers. This allows you to store data across a web site. This method is getting more and more common, I think you should use this. On top of that, I recommend using an helper library in order to ease the storage, like store.
More info about web storage at Mozilla's.

Javascript cookie to save user preference and display content using the shared preference?

I need a solution based on java script(cookies), which could save the user selected preferences and render the output(html pages) acc. to the cookie saved.
Here is the situation:
Lets say user starts from page1 and navigates to page2(having 40-50 hyperlinks) and there he selects or clicks one of the hyperlink and get directed to the target page(there will be 40-50 pages corresponding to those 40-50 links).
So all i need is to automate the whole process, so that after first visit user's selection could get saved and he will directly get navigated to final target(It will be one of the page from 40-50 pages).
Any code-snippet will highly be appreciated..
mrana
I am curious to know why can't you do this in a preference table on the server side. Cookies can be removed from the browser (which would force users to go through that step again) and storing 40-50 cookies in the browser is not a good solution, as cookies get transmitted to every HTTP request so it would waste users' bandwidth.
If you have these settings/preferences stored on the server side then you can easily determine where to send the user when he logs in to your site, instead of extracting those information from the cookies.
Alternatively you can store these preferences in localStorage which provides bigger storage for storing key/values. The downside is that you need to load a bootstrap JS first which will read the settings from localStorage and decide where to redirect the user.
But IMHO I'd still go with a server side solution if I have to store 40-50 preferences.
Note: Cookies can only have 4KB of data, this is a limit.

How to check number of live visitors for my asp.net website?

How do I check number of current visitors for my asp.net website? I am already familiar with following technique but its not close to accurate result.
Create application variable Application["UserCount"]
Increase it in session start
Decrease it in session end
This technique does not check whether the request comes from Web crawlers, robots or any other spammer servers which may request several pages in new sessions which kind of inflates the number.
I want to make sure that a person has opened up a browser (mobile or desktop) to view my website even if he is viewing other sites in same window or in different window but at least my site is open in at least one tab or one window.
Something like ajax hidden request maker that loads with each of my page and tells server that my site is open.
Thanks.
You could look at the user-agent on the request. You can use Request.Browser.Crawler to check if the request comes from a search engines crawler. It will return true if the browser is a crawler.
You could also use a javascript call to a page to update the visitor count. This could be combined with the above mentioned technique to ensure that visitor counts are only update if the request is a real user. You should also check that the request is not a crawler when you decrease the user count. For example
//increase the user count
if (Request.Browser.Crawler == false)
{
Application["user_count"] = +1;
}
//decrease the user count
if (Request.Browser.Crawler == false)
{
Application["user_count"] = -1;
}
You could also use the Session_End and Session_Start event in the application's global.asax file, to perform the same events.
You can use the Application to store the data. It's totally fine for the early stage of the website or web application. However, you may need to look into multiple access issue to the application state when the site is loaded.

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.

Categories