Search code examples
web2pyvisitor-statistic

web2py count unique visitors


I have created a web site with web2py. I need to count daily unique visitor number for each argument in a page, such as:

.../controller/function/argument1
.../controller/function/argument2
.../controller/function/argument3

e.g. daily unique visitors:
argument1: 20
argument2: 53
argument3: 15

Is there a built in function in web2py to do that?
what is the best way you can suggest to do it?
also, if a visitor has visited the web site today, how can web2py know it?

Thanks in advance


Solution

  • web2py doesn't include any built-in functionality to count URL visits. One simple approach is to use something like Google Analytics, which allows you to create custom reports showing users by URL by date. The web2py scaffolding application includes the necessary Google Analytics code in the layout, so all you have to do is sign up for an account and include your Google Analytics ID.

    If you want to track things in web2py, you can set up a database table to store the count for each URL each day and use cookies to track which users have already made a visit during the current day (so you only count once per day). When the function in question is called, check for the cookie. If it doesn't exist, then increment the counter for that argument by one and set a cookie with the current URL as the path and with an expiration at the end of the current day (so any subsequent visits to that URL during the remainder of the day won't be counted). See the web2py documentation on cookies.