Track Web Traffic with PHP

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 06:16:34

问题


Is there an effective way to track web traffic (or at least the origin of web traffic) with PHP?

I was thinking of using custom canonical links for each search engine and other websites, which would mean anybody who visits mywebsite.com without a parameter is likely direct traffic. But then I would somehow need to change the href value of the link rel='canonical' element for each engine crawler (e.g. https://mywebsite.com/?ref=google, https://mywebsite.com/?ref=duckduckgo, etc), and I'm not exactly sure how to go about this (through robots.txt, meta tags or?).

I really don't want to use Google Analytics if I don't have to. I'd prefer to have all of my analytics under one roof so to speak, but I'm stuck for ideas of how to achieve this, and most of my searches on SO seem to pull up stuff related GA.


回答1:


well ive read all over SO about how in many cases the header can be and is simply omitted for various reasons such as AV software, browser extensions, switching from http to https, etc? is this often the case?

Yes, this can happen. How often for your particular site's visitors is anyone's guess.

does GA rely on the referer header?

Not quite... as Google Analytics runs client-side, it's getting that information from document.referrer, which contains the same value as what is sent in the Referer header.

but i would of course like to have numbers that are as accurate as possible

With any web analytics, there are things you simply can't measure. The best way is to use a client-side analytics script to send data to your server. There are a handful of reasons why this is better than simply looking at the data you get in the HTTP request data in PHP:

  • Pages can be cached, so you'll be able to see page loads at times when the browser never even checked in with your server to load the page.
  • The Performance API is available, allowing you to track specific load timings that you can work to improve on over time.
  • In most browsers, you can use the Beacon API to get a sense for when the user leaves the page, so you have accurate time-on-page measurements.

id like to get an idea of what traffic is direct and what traffic is not direct and where non-direct traffic is coming from

document.referrer is what you want, and gets you as close to accurate as you can get.



来源:https://stackoverflow.com/questions/57208956/track-web-traffic-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!