Getting Country/Referrer stats for multiple (many) page titles

Hi,

A client has extremely specific (anal) design for a report, that gets country and referrer stats (if existing) for every page in a hierarchy. I’ve written the PHP using the PHP API which gets the hierarchy (page title (label), hits, and visits), which has a subfunction to further call the API for Country and Referrer information for each page title. The problem is that, since there are several thousand accessed pages this is extreeeeeeeemely slow. I’m a bit of a newb at this kind of thing, so I’m fully aware that there might be a better way to do things.

The code for the additional country stats follows, which is called for each record with the complete hierarchy separated by ‘::’. This results in @segments being:


&segment=pageTitle=@XX;pageTitle=@YYY;pageTitle=@XXXX;pageTitle=@Reel;pageTitle=@ZZZ;


function additionalstats($page_titles) {
	$segments = '&segment=';
	$page_arr = explode("::",$page_titles);
	$i = 0;
	$len = count($page_arr);
	foreach($page_arr as $page) {
		$page_title = rawurlencode(trim($page));
		$page_title = str_replace(".", "%2E", $page_title);
		if (strlen($page_title) > 1) {
			$segments .= "pageTitle=@".$page_title;
			if ($i != $len - 1) {
				$segments .= ";";
				}
			}
		$i++;
		}
	unset($page_arr);

	$countryreq = new Piwik_API_Request('
		method=UserCountry.getCountry
		&idSite=1
		&period=month
		&date=today
		'.$segments.'
		&format=xml
		&filter_limit=3
		&token_auth=XXXXXXXX
	');
	$countries = $countryreq->process();
	
	$country = new SimpleXMLElement($countries);

	foreach ($country->row as $country)
		{
		$html .= "<td>".$country->label.": ".$country->nb_visits."</td>";
		}


Any advice?

My advice would be to request yesterday’s data in the script (or another finished period), and then have a cron script that would load this report so that all reports are pre-processed when you need them, and fast for your boss!