How to count website visitors accurately in php? - php

I am trying to implement visitor counter on a project but I am confused about one thing and it is what to accurately count as one visit or view. If I go with IP based counter then it means even if many people are visiting the website on same computer with same IP (like from a cyber cafe or a shared pc) it will count as one visit. If I go with simply incrementing visits every time homepage is opened then someone can keep refreshing the homepage to increase the count and it will not be accurate page views count.
So neither option gives accurate picture of visits.
So I am thinking of implementing IP based page views and if someone opens the homepage with same IP before 5 minutes it will not be counted as another view. Only after five minutes page count will be increase for same IP. So I want to ask whether this approach will give most accurate page view count or there is any other optimal solution?
Google analytics cannot be used as this website will be used on an intranet network.

Google Analytics is still an option for internal websites. I created a workflow application which is only available through our internal network, but Google Analytics still works. Only requirement is that the user that uses the application has internet access, so that the Google-Analytics Snippet can communicate with the servers.
I'd not recommend using your own methods to count visitors, unless you're planning to show these informations for all users (like it is the case with the view here on SO). You could still create some kind of internal mechanism easily, given the fact that people authenticate on your application or you can identify them somehow else.

Google Analytics and other tracking applications use cookies through javascript to track page visits and especially visitors. Due to the fact that cookies can be unique per session of a browser, this makes identifying different people on the same IP more easy.
However as #Ahatius points out, better not to reinvent the wheel if possible.
Google Analytics also has a php api (which I've successfully implemented in the past). However in that scenario you still have to do decide by yourself how to identify visitors and pageviews.

Related

Tracking visitors coming from Facebook

I have written a PHP based blog for the company i work for. Not using any frameworks. I am having trouble tracking users who come from my facebook page's posts to my blog (not wordpress).
I have created a shortlink url. Let's say it is sample.co and it redirects traffic to sample.com. Everything seems fine until here. The problem starts here.
I am adding all user's ip's, user agents. But if even i get 500 visits, my code adds somethig like 3.000 visits. Facebook stats and Analytics shows similar stats (~500 visits). I see that ip's added to MySQL are all different. It usually happens with Android users. I have read somewhere that Facebook sometimes renders to their users the actual URL when FB shows the post. I mean instead of the widget, Facebook shows the whole page. I am not quite sure about that to be honest.
To solve this problem, I have created and added an jquery script to my page and listened users' scroll event. It worked great. Not seeing too much traffic. But this time the problem is i am counting less users. Even I get 500 users from facebook and Analytics shows similar results, my script adds only 200-300 to MySQL.
Does anyone know a better way to track real traffic? Or do you aware of such problem?
Thanks
It should be filtered on the basis of user agent.
https://developers.facebook.com/docs/sharing/webmasters/crawler
how to detect search engine bots with php?
Identifying users through IP is a good idea, but if your IP keeps changing, it's a good idea to use cookies.
http://php.net/manual/en/function.uniqid.php
If the cookie does not exist, you should see it as a new user.
I have found the answer. The problem is called preview (prefetch). Here is the link:
https://www.facebook.com/business/help/1514372351922333
Simply, facebook preloads everything when FB shows the thumbnail to the visitor to speed up your page's load speed. They send X-Purpose: preview header. So you can simply check if HTTP_X_PURPOSE header's value is preview or not. If so, do not count it as a visitor.!
Here are more detailed descriptions:
http://inchoo.net/dev-talk/mitigating-facebook-x-fb-http-engine-liger/
http://inchoo.net/dev-talk/magento-website-hammering-facebook-liger/

Advertising or redirecting to new website

The website of our institution supports 30 languages and it has almost 400,000 articles. It's Global Rank in alexa.com is always between 11000 to 11500 and we are always on the top 5 google result if any body search on what we are working. Every day we receive almost 200 emails from people all around the world.
We recently developed a new version of our website by Drupal with a slightly different URL.
The old url is like this example.com/html/LANGUAGE/... but the new one is LANGUAGE.EXAMPLE.COM so we need to somehow tell current users to visit the new website without loosing our Global and Google rank.
The most straightforward solution is to modify the header of our website and ask the users to visit the new one but our chief ask me to examine the redirecting method and see what the conclusions of doing this is going to be.
I would like to know If I redirect every example.com/html/LANGUAGE/* to LANGUAGE.example.com will this effect our Global Rank on Google? Won't this redirection to the front page be a negative factor in Google? like 403 redirect? or Is there any better solution for our problem?
Note: There is no logical relationship between the content of old website to the new one. I mean there is no mapping between the contents so when a user visits a content in old website there is no way to redirect him to new website of the same content. He must be redirected to the front page
Thanks.
First of all Alexa rank related with your traffic data but Google PageRank calculating with various factors.
Positive factors are; backlinks, high traffic, correctly written meta tags, meta tags should be related with your body content etc.
Negative factors are; broken links, unrelated keywords, unexpected page content changes etc.
On your situation;
First you should definitely redirect all of your pages to new
destination.
Access to Google Web Master Tools Console.
Send sitemap of your new website.
Checkout Crawling Errors tab and correct the issues.
You won't face a huge impact on Alexa Rank if you redirect all of your pages correctly
Checkout Google Index > Content Keywords you should arrange your new website protecting these keyword chart.
I think Google Web Master Tools is crucial for you. As I said you can't do much things for Alexa Rank.
*I don't recommend you to empty your parent domain and using a subdomain. Subdomain is here for secondary contents.

How can i store visitor infomation like reach time ,duration of visit,and exact location of visitor in php

I want to store visitor information like reach time ,duration of visit and also exact location of the visitor.
Can any one give me any ideas about it? Please help me.
Just a few links to get you started:
JavaScript heat map generator used to track where users clicked on your site
Recording the time a user spent on any single page
Tracking the user's location via IP
Official Geolocation API specification
In my opinion, best thing would be to use some external website statistics tool (like Google Analytics or similar). You can also search for some solutions you install locally (like AWStats, in case of privacy concerns). No need to reinvent the wheel there.

Google Analytics and the precision of data/report

I was just curious to know if the data presented in the google analytics report includes bots/spiders/crawlers. One of the websites that we are building is still stealth(zero marketing, though the site went live about 20 odd days ago). My boss was happy and proud that we are having visitors from all over the world already. But I am a little skeptical :)
Will be great if someone can clarify this for me!
Thanks in advance!
Cheers,
--
Sniper
As explained above, Google Analytics uses a Javascript based mechanism, and since most crawlers won't execute JavaScript, you shouldn't see crawlers into your stats, but true visitors.
In some situations however you could get some "noise" into your stats:
someone has put, by mistake ?, your UA number into its website pages, and you get hits from another web site into yours
some services that you subscribed to monitor the availability of your site from all over the world (like IP Label) run embedded browsers that execute JS and then will showup into your stats
last, you run the mobile tracking code of GA on your site, which is server side and no more JS based, then you can get crawlers into your stats while GA should remove most of them.
To assess if case 1 applies, go into Visitors / Network properties / Hostnames and check if only your hostname is diplayed. In case of other domain names showing up, you can build an advanced filter to include only your hostname
For case 2, look at visits per service providers from day to day to highlight service providers having a stable number of visits per day over time. You may also look at pages with a high share of direct access + bounce + similar volume of pageviews per day over time : this is typical of a monitoring system looking always at the same page.
For case 3, look at web browsers identified by GA in Visitors / Browsers
Google Analytics uses JavaScript that isn't usually executed by bots so your reports shouldn't include data from them. You should be able to look at the visitor overview report to see that your visits are coming from agents that identify themselves as browsers, not bots.

Save users activity

I am a web developer and I want to design a commercial website to sell a customer's product. Sell and buy activities are important and I need to maintain user activity information to keep the site secure.
I want to write a dynamic website. I want to control all user activity and then decide whether to save user activity information in a database. Some of site's visitors are registered users and some are anonymous. I want to save online information such as ip address, username, page name, and date/time for my registered users.
I want to know:
How do I save a user's IP address?
What more do I need to save?
Saving each HTTP request details into database will work for low traffic web sites, but you will have performance issues in case of popular website, since writing to database in relatively slow operation.
Why not to use server HTTP logs instead?
All HTTP web servers create plain text log files which record remote user IP address, URL requested, etc. You can create activity report by writing your own script or using log file report tools. AWStats ( http://awstats.sourceforge.net/ ) is one of the most popular open-source tools for this.
On client side you can use Google Analytics to track user activity. It also provides means to track custom events:
_gaq.push(['_trackEvent', 'login', 'user_login', "custom data"]);
More info at: http://code.google.com/apis/analytics/docs/tracking/asyncUsageGuide.html
This option only tracks users with JavaScript enabled, so it won't show bots, crawlers or users having analytics blocking addons installed.
I'm not sure I understand all of your question...but to address at least one aspect of it, if the user is behind a proxy, then you have no way of determining what their real IP is. That's the whole point. The proxy is the one making the request and then forwarding it. Without asking the proxy yourself, you cannot determine that. With regards to what else you need to save, it depends entirely on what you want to do and you haven't done a good job of explaining why you are saving this data. If you can clarify that, perhaps we can help you a bit more in determining what data you should be saving.
Edit To address your clarification, if you wanted to be crazy, you could log everything that a person does. Every link they click, every product they view, etc. I don't necessarily advocate that as I find it a bit creepy, but there are definitely sites that do it. At the bare minimum, I would suggest logging what products people look at and then what products they buy. I would also log that information on a per-session basis. Basically, what products do people look at and then end up buying on the same trip to your store. I wouldn't worry too much about the "real" IP address. Most people won't be behind a proxy and those that are, you can't do anything about anyway.
How do I save a user's IP address?
$_SERVER['REMOTE_ADDR']
What more do I need to save?
That's quite strange question. It's your application, not someone's else. How can we guess what information you need?
However, at least one issue I can point out: a page name is not sufficient to log "all user activity". Query string and POST data usually contains important details on that activity.

Categories