Skip to content
Snippets Groups Projects
Commit b2d0a174 authored by Ere Maijala's avatar Ere Maijala
Browse files

Refactored Piwik support for better readability and extensibility. Added a...

Refactored Piwik support for better readability and extensibility. Added a couple more custom variables (Sort, Page, Limit, View) to search tracking.
parent 8367ba5b
No related merge requests found
...@@ -88,82 +88,206 @@ class Piwik extends \Zend\View\Helper\AbstractHelper ...@@ -88,82 +88,206 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
return ''; return '';
} }
$search = false; if ($results = $this->getSearchResults()) {
$facets = ''; $code = $this->trackSearch($results);
$facetTypes = ''; } else if ($recordDriver = $this->getRecordDriver()) {
$searchTerms = ''; $code = $this->trackRecordPage($recordDriver);
$searchType = 'false'; } else {
$record = false; $code = $this->trackPageView();
$formats = ''; }
$id = '';
$author = ''; $inlineScript = $this->getView()->plugin('inlinescript');
$title = ''; return $inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $code, 'SET');
$institutions = ''; }
$view = $this->getView(); /**
$escape = $view->plugin('escapeHtmlAttr'); * Track a Search
$viewModel = $view->plugin('view_model'); *
* @param VuFind\Search\Base\Results $results Search Results
*
* @return string Tracking Code
*/
protected function trackSearch($results)
{
$customVars = $this->getSearchCustomVars($results);
$code = $this->getOpeningTrackingCode();
$code .= $this->getCustomVarsCode($customVars);
$code .= $this->getTrackSearchCode($results);
$code .= $this->getClosingTrackingCode();
return $code;
}
/**
* Track a Record View
*
* @param VuFind\RecordDriver\AbstractBase $recordDriver Record Driver
*
* @return string Tracking Code
*/
protected function trackRecordPage($recordDriver)
{
$customVars = $this->getRecordPageCustomVars($recordDriver);
$code = $this->getOpeningTrackingCode();
$code .= $this->getCustomVarsCode($customVars);
$code .= $this->getTrackPageViewCode();
$code .= $this->getClosingTrackingCode();
return $code;
}
/**
* Track a Generic Page View
*
* @return string Tracking Code
*/
protected function trackPageView()
{
$customVars = $this->getGenericCustomVars();
$code = $this->getOpeningTrackingCode();
$code .= $this->getCustomVarsCode($customVars);
$code .= $this->getTrackPageViewCode();
$code .= $this->getClosingTrackingCode();
return $code;
}
/**
* Get Search Results if on a Results Page
*
* @return VuFind\Search\Base\Results|null Search results or null if not
* on a search page
*/
protected function getSearchResults()
{
$viewModel = $this->getView()->plugin('view_model');
$children = $viewModel->getCurrent()->getChildren(); $children = $viewModel->getCurrent()->getChildren();
if (isset($children[0])) { if (isset($children[0])) {
$template = $children[0]->getTemplate(); $template = $children[0]->getTemplate();
if (!strstr($template, '/home')) { if (!strstr($template, '/home')) {
$results = $children[0]->getVariable('results'); $results = $children[0]->getVariable('results');
} if (is_a($results, 'VuFind\Search\Base\Results')) {
$recordDriver = $children[0]->getVariable('driver'); return $results;
}
if ($results && is_a($results, 'VuFind\Search\Base\Results')) {
$search = true;
$resultCount = $results->getResultTotal();
if ($this->customVars) {
$facets = array();
$facetTypes = array();
$params = $results->getParams();
foreach ($params->getFilterList() as $filterType => $filters) {
$facetTypes[] = $escape($filterType);
foreach ($filters as $filter) {
$facets[] = $escape($filter['field']) . '|'
. $escape($filter['value']);
}
}
$facets = implode('\t', $facets);
$facetTypes = implode('\t', $facetTypes);
$searchType = $escape($params->getSearchType());
$searchTerms = $escape($params->getDisplayQuery());
}
} elseif ($recordDriver
&& is_a($recordDriver, 'VuFind\RecordDriver\AbstractBase')
) {
$record = true;
$id = $escape($recordDriver->getUniqueID());
if (is_callable(array($recordDriver, 'getFormats'))) {
$formats = $recordDriver->getFormats();
if (is_array($formats)) {
$formats = implode(',', $formats);
}
$formats = $escape($formats);
}
if (is_callable(array($recordDriver, 'getPrimaryAuthor'))) {
$author = $escape($recordDriver->getPrimaryAuthor());
if (!$author) {
$author = '-';
} }
} }
if (is_callable(array($recordDriver, 'getTitle'))) { }
$title = $escape($recordDriver->getTitle()); return null;
if (!$title) { }
$title = '-';
} /**
* Get Record Driver if on a Record Page
*
* @return VuFind\RecordDriver\AbstractBase|null Record driver or null if not
* on a record page
*/
protected function getRecordDriver()
{
$viewModel = $this->getView()->plugin('view_model');
$children = $viewModel->getCurrent()->getChildren();
if (isset($children[0])) {
$driver = $children[0]->getVariable('driver');
if (is_a($driver, 'VuFind\RecordDriver\AbstractBase')) {
return $driver;
} }
if (is_callable(array($recordDriver, 'getInstitutions'))) { }
$institutions = $recordDriver->getInstitutions(); return null;
if (is_array($institutions)) { }
$institutions = implode(',', $institutions);
} /**
$institutions = $escape($institutions); * Get Custom Variables for Search Results
*
* @param VuFind\Search\Base\Results $results Search results
*
* @return array Associative array of custom variables
*/
protected function getSearchCustomVars($results)
{
if (!$this->customVars) {
return array();
}
$facets = array();
$facetTypes = array();
$params = $results->getParams();
foreach ($params->getFilterList() as $filterType => $filters) {
$facetTypes[] = $filterType;
foreach ($filters as $filter) {
$facets[] = $filter['field'] . '|' . $filter['value'];
} }
} }
$facets = implode("\t", $facets);
$facetTypes = implode("\t", $facetTypes);
return array(
'Facets' => $facets,
'FacetTypes' => $facetTypes,
'SearchType' => $params->getSearchType(),
'SearchBackend' => $params->getSearchClassId(),
'Sort' => $params->getSort(),
'Page' => $params->getPage(),
'Limit' => $params->getLimit(),
'View' => $params->getView()
);
}
/**
* Get Custom Variables for a Record Page
*
* @param VuFind\RecordDriver\AbstractBase $recordDriver Record driver
*
* @return array Associative array of custom variables
*/
protected function getRecordPageCustomVars($recordDriver)
{
$id = $recordDriver->getUniqueID();
$formats = $recordDriver->tryMethod('getFormats');
if (is_array($formats)) {
$formats = implode(',', $formats);
}
$formats = $formats;
$author = $recordDriver->tryMethod('getPrimaryAuthor');
if (empty($author)) {
$author = '-';
}
// Use breadcrumb for title since it's guaranteed to return something
$title = $recordDriver->tryMethod('getBreadcrumb');
if (empty($title)) {
$title = '-';
}
$institutions = $recordDriver->tryMethod('getInstitutions');
if (is_array($institutions)) {
$institutions = implode(',', $institutions);
}
$institutions = $institutions;
return array(
'RecordFormat' => $formats,
'RecordData' => "$id|$author|$title",
'RecordInstitution' => $institutions
);
}
/**
* Get Custom Variables for a Generic Page View
*
* @return array Associative array of custom variables
*/
protected function getGenericCustomVars()
{
return array();
}
$code = <<<EOT /**
* Get the Initialization Part of the Tracking Code
*
* @return string JavaScript Code Fragment
*/
protected function getOpeningTrackingCode()
{
return <<<EOT
var _paq = _paq || []; var _paq = _paq || [];
(function(){ (function(){
_paq.push(['setSiteId', {$this->siteId}]); _paq.push(['setSiteId', {$this->siteId}]);
...@@ -172,46 +296,80 @@ _paq.push(['setCustomUrl', location.protocol + '//' ...@@ -172,46 +296,80 @@ _paq.push(['setCustomUrl', location.protocol + '//'
+ location.host + location.pathname]); + location.host + location.pathname]);
EOT; EOT;
}
if ($search) { /**
if ($this->customVars) { * Get the Finalization Part of the Tracking Code
$code .= <<<EOT *
_paq.push(['setCustomVariable', 1, 'Facets', "$facets", 'page']); * @return string JavaScript Code Fragment
_paq.push(['setCustomVariable', 2, 'FacetTypes', "$facetTypes", 'page']); */
_paq.push(['setCustomVariable', 3, 'SearchType', "$searchType", 'page']); protected function getClosingTrackingCode()
{
return <<<EOT
_paq.push(['enableLinkTracking']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.defer=true; g.async=true;
g.src='{$this->url}piwik.js';
s.parentNode.insertBefore(g,s); })();
EOT; EOT;
} }
// Use trackSiteSearch *instead* of trackPageView in searches
$code .= <<<EOT
_paq.push(['trackSiteSearch', '$searchTerms', "$searchType", $resultCount]);
EOT; /**
} else if ($record && $this->customVars) { * Convert a Custom Variables Array to JavaScript Code
*
* @param array $customVars Custom Variables
*
* @return string JavaScript Code Fragment
*/
protected function getCustomVarsCode($customVars)
{
$escape = $this->getView()->plugin('escapeHtmlAttr');
$code = '';
$i = 0;
foreach ($customVars as $key => $value) {
++$i;
$value = $escape($value);
$code .= <<<EOT $code .= <<<EOT
_paq.push(['setCustomVariable', 1, 'RecordFormat', "$formats", 'page']); _paq.push(['setCustomVariable', $i, '$key', '$value', 'page']);
_paq.push(['setCustomVariable', 2, 'RecordData', "$id|$author|$title", 'page']);
_paq.push(['setCustomVariable', 3, 'RecordInstitution', "$institutions", 'page']);
EOT; EOT;
} }
return $code;
}
if (!$search) { /**
$code .= <<<EOT * Get Site Search Tracking Code
_paq.push(['trackPageView']); *
* @param VuFind\Search\Base\Results $results Search results
*
* @return string JavaScript Code Fragment
*/
protected function getTrackSearchCode($results)
{
$escape = $this->getView()->plugin('escapeHtmlAttr');
$params = $results->getParams();
$searchTerms = $escape($params->getDisplayQuery());
$searchType = $escape($params->getSearchType());
$resultCount = $results->getResultTotal();
EOT; // Use trackSiteSearch *instead* of trackPageView in searches
}; return <<<EOT
$code .= <<<EOT _paq.push(['trackSiteSearch', '$searchTerms', '$searchType', $resultCount]);
_paq.push(['enableLinkTracking']);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.defer=true; g.async=true;
g.src='{$this->url}piwik.js';
s.parentNode.insertBefore(g,s); })();
EOT; EOT;
}
$inlineScript = $view->plugin('inlinescript'); /**
return $inlineScript(\Zend\View\Helper\HeadScript::SCRIPT, $code, 'SET'); * Get Page View Tracking Code
*
* @return string JavaScript Code Fragment
*/
protected function getTrackPageViewCode()
{
return <<<EOT
_paq.push(['trackPageView']);
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