This is a self contained module that enables you to track and display 'view counter' on content types in your Yii based website
'import' => array( //... // Views counter module 'application.modules.PcViewsCounter.*', 'application.modules.PcViewsCounter.models.*', 'application.modules.PcViewsCounter.controllers.*', 'application.modules.PcViewsCounter.components.*', 'application.modules.PcViewsCounter.extensions.ViewsCountWidget.*', //... ), //... 'modules' => array( //... 'contentViewsCounter' => array( 'class' => 'application.modules.PcViewsCounter.PcViewsCounterModule', ), //... ),
$this->_clientIpAddress = Yii::app()->geoip->getRemoteIpAddress();
Sample code:
$this->widget("ViewsCountWidget", array('modelId' => $model->id, 'modelClassName' => get_class($model)));
if impression-cookie exists (this is for guest)
do nothing.
else
if authenticated
search PageViewBookeeping record for this user (based on user_id, PageViewsStat->id (which is an FK in the former class/table))
if not found search for PageViewBookeeping record based on ip address.
if found in either of the 2 checks above
add this information into the impression cookie.
return
else
add impression record for this user.
add this information into the impression cookie.
else (this is guest user)
add cookie for this guest user unique impression
search for PageViewBookeeping record based on ip address.
if NOT found
add impression record for guest (record his ip address)
else
return existing counter
add +1 to unique stats counter for this model name + id.

Total 15 comments
I have a problem getting this to work.. I am adding this: $this->widget("ViewsCountWidget", array('modelId' => $model->id, 'modelClassName' => get_class($model))); to the view file of the User(yii-user) model, and I always get: "views: 0".
What do u mean by: •Requires 'cache' application component. Be sure to either have it working or rewrite ViewsCountWidget._setPersistentCounterConfig() and ViewsCountWidget._getPersistentCounterConfig(). See those methods for more information.
Could it be not working because of this? Or what do you think?
Exactly: making a page submit AJAX calls is an effective search engine filter. Ever seen a search engine that after it loads the page will runs its JS and execute everything in it inc. AJAX calls? AFAIK no search engine does this. You're invited to check it on the web (Google etc).
thanks for your answer Boaz, i diden't understood exactly, thats way i asked it :)
but it's interesting for me, the ajax request the way to filter search robots?!
a pharse in my language: one can always learn something
:-)
Regarding Google Analytics - its totally different. This one only records page views and that's it. GA is much more. This extension is designed to show a nice counter on each page (or resource you render).
It should skip robots, as noted in the documentation above, since it uses AJAX to mark a view, and searching engines will not send the needed AJAX calls after page has been loaded.
is it as good, as google analytics??
is it counting search robots too, or how is it avoid that??
thanks for answers, and sorry bad english
Hi,
For a start, you're rendering the widget in a way that it always marks its impressions on model id =1. This is not what you want... . Notice the example I gave above for rendering of the widget. It uses the following code: array('modelId' => $model->id and not array('modelId' => 1...) ... :-)
Lets see how you're doing after this...
I think I have followed all your steps but it does not work.
1 - I have put PcBaseArModel.php in /protected/components
2 - I have updated my main.php like:
3- I have unpacked into /extensions/PcMaxmindGeoIp:
4- And I have put in my view file this code:
modelId is like a unique id for everymodel I have, isn't it?
5- and I have created into my database both tables:
Am I doing something wrong?? Did I miss something??
Because, if a call my/website/my/view/file.php I always see
And in my database, the table page_views_stats has been updated, but nothing happen with the table page_views_bookeeping (this one is always empty, why??)
Boaz, could you guide me with my installation?
Thank you for this tool!
Always - "use the force - read the source" :-)
I wonder about the warning or maybe even the total explosion of your PHP env on the use of gmmktime(). WTF? What's the problem with it? Although as a side note I must say that developing on as pedantic as possible environment is a good practice, and it appears that mine is not, at the moment.
I'm glad you find it useful after all! :)
"->> and the use of gmttime() instead of time.
I'm not sure what's the issue here. If you want, please elaborate more on this."
I have no clue, but yii halted on the lines using gmttime() telling me to use time() instead.
Thank you for telling me where to look, it helped, now I have the extension completely working.
->> Oh well, I finally have it working, kinda. It still says 0 for unique visitors but not unique ->> visits is updated. Should say 1 on unique visitors... shouldnt it?
Well it says whatever is in its page_views_stats.count_uniq (if your widget is set to uniq mode). If it comes to a decision that you already voted it wont count the uniq count column and simply show what's inside. And how does it decide if you viewed this content or not? I actually started writing here but its too long and too clear in the code. Have a look at ViewsCountWidget.php, line 136 (in the run() method).
->> There was many errors thrown by yii disliking things in the code, declaration of the findByAttributes method must match the parent declaration for example
I guess you're right. It would be nice if you could submit a bug on this on the github project page (see link in 'resources' section above.
->> and the use of gmttime() instead of time.
I'm not sure what's the issue here. If you want, please elaborate more on this.
->> name of module in the main config and the name of folder didnt match, gave url fails.
Hmm... you're probably right here as well. The folder name for this module should be PcViewsCounter. A bug on this would be nice as well... :)
Thanks for the report!
Yes I noticed that and installed that extension from the start, else how would Yii know the methods?:p
Oh well, I finally have it working, kinda. It still says 0 for unique visitors but not unique visits is updated. Should say 1 on unique visitors... shouldnt it?
There was many errors thrown by yii disliking things in the code, declaration of the findByAttributes method must match the parent declaration for example, and the use of gmttime() instead of time.
name of module in the main config and the name of folder didnt match, gave url fails. Thats the errors I haven't mentioned that I had to solve during installation.
Thanks for sharing this btw.
Yes, this extension requires PcBaseArModel - which is another extension I've written, and that I use. Its noted in the requirements section. That extension provides a class that requires a couple of its methods (which are abstract) - be implemented. This is really not a big deal. Read that extension's documentation and see how easy it is to implement those methods. This is relevant only if you're extending PcBaseArModel - which is yet again, a matter for the other extension, not this one (unless there's some bug that I'll be happy to hear about).
You're right about the 'geoip' app component I didn't mention. I've updated the documentation to reflect this need. Please see above... .
The views counting is not taken from the bookeeping table but rather from the 'main' stats table. But anyhow if you got logging enabled - please check the logs to get a better insights for problems this extension is facing.
Thanks for the feedback
I had some troubles installing, php threw errors over the abstract static methods in PcBaseArModel extension, and didnt find Yii::app()->geoip so tried changing to
$this->_clientIpAddress = Yii::app()->request->userHostAddress; also define tablenames '{{tablename}}' so users with table prefix doesnt have to change it.
It creates a row for the model whos view i visit in the db, but nothing in the page_views_bookeeping table. So the view count stays at 0 and updated at null. Maybe I'm just to tired now but what am I missing?:p Thx
@WebDevPT,
If you mean what's here, then I also recommend peeking at the source files inline documentation. That's where I leave my thoughts - typically for myself, but obviously other developers are welcomes to sync in, for a joyful and fruitful outcome.
thanks anyhow :) Boaz.
Very good documentation!
Leave a comment
Please login to leave your comment.