Updating Ad Counter Via Piwik After Render

Hi there,

Scenario: I counting visit for each ad using Piwik Analytics. Know i want after each ad view update the hint field in ad table. I have the following code:

in Controller:


 public function actionView($catId,$id)

    {

        $this->layout = 'index';

        $model=Advert::model()->find(

            '(id=:id and category_id=:catId) and (status=:verified or status=:active)',array(

            ':id'=>(int)$id, ':catId'=>(int)$catId, ':verified'=>Advert::Verified, ':active'=>Advert::Active,

        ));

        if(!is_null($model)){

            $this->render('adView',array('model'=>$model));

        }else

            throw new CHttpException(404,Yii::t('application','The requested page does not exist.'));

    }

    

    protected function afterAction($action)

    {

        if(Yii::app()->controller->action->id == 'view')

        {

            Advert::updateHint((int)Yii::app()->request->getQuery('id'));

        }

        

    } 

In afterAction the updateHint() is a static method in Advert model as follow:




	public static function updateHint($ad_id)

    {

        $url ="http://localhost/piwik/index.php?";

        $url .= "module=API&method=Actions.getPageTitle";

        $url .= "&pageName=$ad_id";

        $url .= "&idSite=1&period=day&date=today&format=php";

        $url .= "&token_auth=c0e024d9200b5705bc4804722636378a";


        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_HEADER, 0);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $ad = unserialize(curl_exec($ch));

        

        $hints = 0;

        $visits = 0;

        $uniq_visitors = 0;

        if(!empty($ad)) {

            $hints = $ad[0]['nb_hits'];

            $visits = $ad[0]['nb_visits'];

        }    

        curl_close($ch);


        $model = Advert::model()->findByPk($ad_id);

        if(!is_null($model))

            $model->saveAttributes(array('hint'=>$hints));    

    } 



adView also is as fllow:




<script type="text/javascript">

$(document).ready(function(){	

	_paq.push(['setDocumentTitle', "<?php echo $model->id ?>"]);

	_paq.push(['trackPageView']);

});

</script>


<div class="ad_view">


	<?php if((int)$model->type===Advert::Photo_Ad){ ?>

	<div class="row image">

    	<?php echo CHtml::image(

    	Yii::app()->baseUrl.'/images/ads/'.$model->image,

    	$model->image,array(

        	'id'=>'currentImage', 'height'=>150,'width'=>180,

    	))?>

	</div>

	<?php } ?>


	<div class="row">

    	<?php echo CHtml::activeLabel($model,'title'); ?>

    	<?php echo CHtml::tag('p',array(),$model->title); ?>

	</div>


	<div class="row">

		<?php if(!empty($model->description)){ ?>

        	<?php echo CHtml::activeLabel($model,'description'); ?>

			<div class='description'>

				<?php echo $model->description;?>

			</div>

    	<?php } ?>

	</div>	


	<div class="row">

    	<?php if(!is_null($model->user->name)){

    	echo CHtml::Label(Yii::t('form','Name'),'');

    	echo CHtml::tag('p',array(),$model->user->name);

	}?>

	</div>


	<div class="row">

    	<?php if(!is_null($model->user->surname)){

    	echo CHtml::Label(Yii::t('form','Surname'),'');

    	echo CHtml::tag('p',array(),$model->user->surname);

	}?>

	</div>


	<div class="row">

    	<?php if(!is_null($model->user->telephone)){

    	echo CHtml::Label(Yii::t('form','Telephone'),'');

    	echo CHtml::tag('p',array(),$model->user->telephone);

	}?>

	</div>


	<div class="row">

    	<?php if(!is_null($model->user->mobile)){

    	echo CHtml::Label(Yii::t('form','Mobile'),'');

    	echo CHtml::tag('p',array(),$model->user->mobile);

	}?>

	</div>


	<div class="row">

    	<?php if(!is_null($model->user->email)){

        	echo CHtml::Label(Yii::t('form','Email'),'');

        	echo CHtml::tag('p',array(),$model->user->email);

    	}?>

	</div>


	<div class="row">

    	<?php if(!is_null($model->user->www)){

        	echo CHtml::Label(Yii::t('form','www'),'');

        	echo CHtml::tag('p',array(),$model->user->www);

    	}?>

	</div>


</div>

<hr>

<div>

	<?php echo CHtml::link(Yii::t('form','Back'),array('/')) ?>

</div>



Problem is updateHint method always is rear one. for example if in Piwik cpanel pageview was 25, updateHint return 24.

Note that if updateHint be run once again after ending ad view in other place, updateHint returns right report(returns 25).

In my opinion problem is updateHint be run before ad view be completed. In your opinion afterRender is a right place for putting updateHint method?

excuse me if description is not much clean.

Thanks friends

In fact i want execute the updateHint($ad_id) after the CController->processOutput.

How can i execute a function after CController->processOutput?