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


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:


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 .= ";";

	$countryreq = new Piwik_API_Request('
	$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!