diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php index 920eff8a3a43ead12bdfb5b5f26c787be7755bc3..146ba9446dfbabae76ca210a9e9a8087ef3dc0ae 100644 --- a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php +++ b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php @@ -4,7 +4,7 @@ * * PHP version 5 * - * Copyright (C) The National Library of Finland 2014. + * Copyright (C) The National Library of Finland 2014-2016. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2, @@ -128,9 +128,12 @@ class Piwik extends \Zend\View\Helper\AbstractHelper $this->lightbox = $this->params['lightbox']; } - if ($results = $this->getSearchResults()) { + $results = $this->getSearchResults(); + if ($results && ($combinedResults = $this->getCombinedSearchResults())) { + $code = $this->trackCombinedSearch($results, $combinedResults); + } elseif ($results) { $code = $this->trackSearch($results); - } else if ($recordDriver = $this->getRecordDriver()) { + } elseif ($recordDriver = $this->getRecordDriver()) { $code = $this->trackRecordPage($recordDriver); } else { $code = $this->trackPageView(); @@ -161,6 +164,28 @@ class Piwik extends \Zend\View\Helper\AbstractHelper return $code; } + /** + * Track a Combined Search + * + * @param VuFind\Search\Base\Results $results Search Results + * @param array $combinedResults Combined Search Results + * + * @return string Tracking Code + */ + protected function trackCombinedSearch($results, $combinedResults) + { + $customVars = $this->lightbox + ? $this->getLightboxCustomVars() + : $this->getSearchCustomVars($results); + + $code = $this->getOpeningTrackingCode(); + $code .= $this->getCustomVarsCode($customVars); + $code .= $this->getTrackCombinedSearchCode($results, $combinedResults); + $code .= $this->getClosingTrackingCode(); + + return $code; + } + /** * Track a Record View * @@ -223,6 +248,25 @@ class Piwik extends \Zend\View\Helper\AbstractHelper return null; } + /** + * Get Combined Search Results if on a Results Page + * + * @return array|null Array of search results or null if not on a combined search + * page + */ + protected function getCombinedSearchResults() + { + $viewModel = $this->getView()->plugin('view_model'); + $children = $viewModel->getCurrent()->getChildren(); + if (isset($children[0])) { + $results = $children[0]->getVariable('combinedResults'); + if (is_array($results)) { + return $results; + } + } + return null; + } + /** * Get Record Driver if on a Record Page * @@ -440,10 +484,48 @@ EOT; $searchTerms = $escape($params->getDisplayQuery()); $searchType = $escape($params->getSearchType()); $resultCount = $results->getResultTotal(); + $backendId = $results->getOptions()->getSearchClassId(); + + // Use trackSiteSearch *instead* of trackPageView in searches + return <<<EOT + VuFindPiwikTracker.trackSiteSearch( + '$backendId|$searchTerms', '$searchType', $resultCount + ); + +EOT; + } + + /** + * Get Site Search Tracking Code for Combined Search + * + * @param VuFind\Search\Base\Results $results Search results + * @param array $combinedResults Combined Search Results + * + * @return string JavaScript Code Fragment + */ + protected function getTrackCombinedSearchCode($results, $combinedResults) + { + $escape = $this->getView()->plugin('escapeHtmlAttr'); + $params = $results->getParams(); + $searchTerms = $escape($params->getDisplayQuery()); + $searchType = $escape($params->getSearchType()); + $resultCount = 0; + foreach ($combinedResults as $currentSearch) { + if ($currentSearch['ajax']) { + // Some results fetched via ajax, so report that we don't know the + // result count. + $resultCount = 'false'; + break; + } + $resultCount += $currentSearch['view']->results + ->getResultTotal(); + } // Use trackSiteSearch *instead* of trackPageView in searches return <<<EOT - VuFindPiwikTracker.trackSiteSearch('$searchTerms', '$searchType', $resultCount); + VuFindPiwikTracker.trackSiteSearch( + 'Combined|$searchTerms', '$searchType', $resultCount + ); EOT; }