Update Count of specified page title visits on another database table


#1

I with following JS code on each page count each page view and with an ajax request update the count of page views on other database table:
javascript in page:


<script>
_paq.push(['setDocumentTitle', "<?php echo $model->id ?>"]);
_paq.push(['trackPageView']);
var request = $.ajax({
	type: 'get',
	url: "<?php echo Yii::app()->createUrl('site/updateHint') ?>",
	data: {ad_id:"<?php echo $model->id ?>"},
	success: function(data, textStatus, jqXHR){console.log(data+' - '+textStatus)},
	error: function(jqXHR,textStatus, errorThrown){console.log(errorThrown)},
});
<script>

site/updateHint action:


	public function actionUpdateHint($ad_id)
	{
		if(Advert::updateHint((int)$ad_id)) 
			echo $ad_id.' updated'; 
		else echo 
			$ad_id.' NUpdate';		
	}

Advert::updateHint static function:


	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=range&date=2013-00-00,today&format=php";
		$url .= "&token_auth=c0e024d9200b5705bc4804722636378a";
		$url .= "&showColumns=nb_hits,nb_visits";

		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$ad = unserialize(curl_exec($ch));
		curl_close($ch);
		
		$hints; $visits; $page_title; //$uniq_visitors = 0;
		if(!empty($ad)) {
			$page_title = $ad[0]['label'];
			//$uniq_visitors = $ad[0]['nb_uniq_visitors'];
			$hints = $ad[0]['nb_hits'];
			$visits = $ad[0]['nb_visits'];
		}else return false;

		$model = Advert::model()->findByPk($ad_id);
		if(is_null($model)) return false;
		if(!$model->saveAttributes(array('hint'=>$visits))) return false;
			
		return true;
	}

Is suitable using of curl method in Advert::updateHint?
Is there another way for do it?


#2

Can anyone tell me,
Is suitable using of CUrl method for piwik tracking api?


(Matthieu Aubry) #3

Curl is suitable to call the tracking API


#4

@matt Thanks for answers