Search code examples
yii-extensionsyiiyii-componentsyii-events

How To Count Views On Click Of A Button Or Web Page Is There Any Extension


I am a newbie interested to know are there any extension to count views on click of a button as to know no. of registered users or visiters to web page to know the view count on click of a image is there any extension.

Plz let me know if any

thanx :)


Solution

  • I think , there is no need of any extension. Make a Ajax call on click button or image you are interested.

    Improved: I supposed you have Site as controller and index as action. then, please keep this code on views/site/index.php .

    Yii::app()->clientScript->registerScript('logo_as_image_script', '$(document).ready(function() {
            $("#logo_as_image").click(function() {
                $.post("'.Yii::app()->createAbsoluteUrl('site/index').'",
                        {
                            clicked: "1"
                        },
                function(data, status) {
                    alert("Data: " + data + "\nStatus: " + status);
                });
            });
        });');
    Yii::app()->clientScript->registerCoreScript('jquery');
    echo CHtml::image(Yii::app()->baseUrl . '/images/logo.png', 'Logo as Image', array('id' => 'logo_as_image'));
    

    And, keep this code on SiteController.php .

    public function actionIndex() 
    {
        // keep record of data ; do more filtering ; other manupulation 
        if(isset($_POST['clicked'])){
            $nextCount = Yii::app()->user->getState('clickCount')+1; 
            Yii::app()->user->setState('clickCount',$nextCount );
            echo $nextCount;
            Yii::app()->end();
        }
        #other codes here. 
        $this->render('index');
    }