Skip to content
Snippets Groups Projects
Commit aad47715 authored by Ere Maijala's avatar Ere Maijala Committed by Demian Katz
Browse files

Improved support for tracking combined searches in Piwik (#780)

* Track combined searches differently in Piwik so that they don't get reported in "no results".
parent d15f862f
No related merge requests found
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
* *
* PHP version 5 * 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 * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2, * it under the terms of the GNU General Public License version 2,
...@@ -128,9 +128,12 @@ class Piwik extends \Zend\View\Helper\AbstractHelper ...@@ -128,9 +128,12 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
$this->lightbox = $this->params['lightbox']; $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); $code = $this->trackSearch($results);
} else if ($recordDriver = $this->getRecordDriver()) { } elseif ($recordDriver = $this->getRecordDriver()) {
$code = $this->trackRecordPage($recordDriver); $code = $this->trackRecordPage($recordDriver);
} else { } else {
$code = $this->trackPageView(); $code = $this->trackPageView();
...@@ -161,6 +164,28 @@ class Piwik extends \Zend\View\Helper\AbstractHelper ...@@ -161,6 +164,28 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
return $code; 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 * Track a Record View
* *
...@@ -223,6 +248,25 @@ class Piwik extends \Zend\View\Helper\AbstractHelper ...@@ -223,6 +248,25 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
return null; 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 * Get Record Driver if on a Record Page
* *
...@@ -440,10 +484,48 @@ EOT; ...@@ -440,10 +484,48 @@ EOT;
$searchTerms = $escape($params->getDisplayQuery()); $searchTerms = $escape($params->getDisplayQuery());
$searchType = $escape($params->getSearchType()); $searchType = $escape($params->getSearchType());
$resultCount = $results->getResultTotal(); $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 // Use trackSiteSearch *instead* of trackPageView in searches
return <<<EOT return <<<EOT
VuFindPiwikTracker.trackSiteSearch('$searchTerms', '$searchType', $resultCount); VuFindPiwikTracker.trackSiteSearch(
'Combined|$searchTerms', '$searchType', $resultCount
);
EOT; EOT;
} }
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment