R
R
raxweb2022-02-03 17:04:08
AJAX
raxweb, 2022-02-03 17:04:08

How can php + js accurately and easily determine that a person has visited a page (for the hit counter)?

For several pages of my site (for example, here ) I set a page visit counter (so far only in php).

Please tell me a javascript event that:

- Occurs in the browser only for humans (not for robots).
- Occurs once - so that from js to give the php-script of the counter an event only once when viewing the page - so that the script would be less twitchy.

I will clarify - the counter should only count the fact of viewing the page. Those. you need a hit count.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
raxweb, 2022-02-04
@raxweb

I have compiled a working script, please have a look.
If it can somehow be simplified or facilitated, then write, I will make corrections.

<script type="text/javascript">

var human_detected = false;//для однократного запуска функции

//3 события, каждое из которых показывает, что на странице человек
document.onmouseover = is_human_view;//нахождение мыши на документе
document.onscroll = is_human_view;//скролл
document.addEventListener("touchstart", is_human_view);//тач на мобилке

function is_human_view ()
{
  if (human_detected == false)//если функция еще не запускалась
  {
  alert ('IS HUMAN VIEW');
  human_detected = true;//уже определили просмотр страницы человеком, больше запускать фунцию не будем
  }
}

</script>

You can see his work, for example, here - on a human view event, the is_human_view function is launched, and at the top of the page (next to the star rating) you will see a green +1 .

I
Igor, 2022-02-03
@Igor_307

- Occurs in the browser only for humans (not for robots).
- Happens once

event DOMContentLoaded for example, or onload more details here
document.addEventListener("DOMContentLoaded", function(){
    //ваш ajax запрос
});

only all this is garbage, if you need visitor statistics, it's easier to connect some kind of analytics

N
Nadim Zakirov, 2022-02-03
@zkrvndm

Hang a reaction to the scroll event, and in order not to send it twice, save the sending status to a variable.

S
spaceatmoon, 2022-02-03
@spaceatmoon

99% accurate is buying something on your site. Everything else is generated by millions for free.
Regarding the question, you can count only unique sessions, do not count the same session twice.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question