From 1dc6b57ba864850ef44343774caeffaa49677a16 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 3 Jul 2017 09:38:49 -0400
Subject: [PATCH] Add config to prefix Piwik tracked searches. (#1000)

---
 config/vufind/config.ini                      |  6 +++++
 .../src/VuFind/View/Helper/Root/Factory.php   |  8 +++++--
 .../src/VuFind/View/Helper/Root/Piwik.php     | 24 +++++++++++++++----
 3 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/config/vufind/config.ini b/config/vufind/config.ini
index 8a79b62b643..390e4b73d67 100644
--- a/config/vufind/config.ini
+++ b/config/vufind/config.ini
@@ -1495,6 +1495,12 @@ treeSearchLimit = 100
 ; Uncomment the following setting to track additional information about searches
 ; and displayed records with Piwik's custom variables
 ;custom_variables = true
+; By default, Piwik searches are tracked using the format "Backend|Search Terms."
+; If you need to differentiate searches coming from multiple VuFind instances using
+; a shared site_id, you can set the searchPrefix to add an additional prefix to
+; the string, for example "SiteA|Backend|Search Terms." Most users will want to
+; leave this disabled.
+;searchPrefix = "SiteA|"
 
 ; Uncomment portions of this section to activate tabs in the search box for switching
 ; between search modules. Keys are search backend names, values are labels for use in
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
index 4987ebb9ace..75f036f557c 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Factory.php
@@ -245,13 +245,17 @@ class Factory
     {
         $config = $sm->getServiceLocator()->get('VuFind\Config')->get('config');
         $url = isset($config->Piwik->url) ? $config->Piwik->url : false;
-        $siteId = isset($config->Piwik->site_id) ? $config->Piwik->site_id : 1;
+        $options = [
+            'siteId' => isset($config->Piwik->site_id) ? $config->Piwik->site_id : 1,
+            'searchPrefix' => isset($config->Piwik->searchPrefix)
+                ? $config->Piwik->searchPrefix : null
+        ];
         $customVars = isset($config->Piwik->custom_variables)
             ? $config->Piwik->custom_variables
             : false;
         $request = $sm->getServiceLocator()->get('Request');
         $router = $sm->getServiceLocator()->get('Router');
-        return new Piwik($url, $siteId, $customVars, $router, $request);
+        return new Piwik($url, $options, $customVars, $router, $request);
     }
 
     /**
diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
index c628b764ffb..c69324b309d 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
@@ -52,6 +52,13 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
      */
     protected $siteId;
 
+    /**
+     * Search prefix (see config.ini for details)
+     *
+     * @var string
+     */
+    protected $searchPrefix;
+
     /**
      * Whether to track use custom variables to track additional information
      *
@@ -100,19 +107,26 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
      *
      * @param string|bool                      $url        Piwik address
      * (false if disabled)
-     * @param int                              $siteId     Piwik site ID
+     * @param int|array                        $options    Options array (or,
+     * if a single value, the Piwik site ID -- for backward compatibility)
      * @param bool                             $customVars Whether to track
      * additional information in custom variables
      * @param Zend\Mvc\Router\Http\RouteMatch  $router     Request
      * @param Zend\Http\PhpEnvironment\Request $request    Request
      */
-    public function __construct($url, $siteId, $customVars, $router, $request)
+    public function __construct($url, $options, $customVars, $router, $request)
     {
         $this->url = $url;
         if ($url && substr($url, -1) != '/') {
             $this->url .= '/';
         }
-        $this->siteId = $siteId;
+        if (is_array($options)) {
+            $this->siteId = $options['siteId'];
+            $this->searchPrefix = isset($options['searchPrefix'])
+                ? $options['searchPrefix'] : '';
+        } else {
+            $this->siteId = $options;
+        }
         $this->customVars = $customVars;
         $this->router = $router;
         $this->request = $request;
@@ -511,7 +525,7 @@ EOT;
         // Use trackSiteSearch *instead* of trackPageView in searches
         return <<<EOT
     VuFindPiwikTracker.trackSiteSearch(
-        '$backendId|$searchTerms', '$searchType', $resultCount
+        '{$this->searchPrefix}$backendId|$searchTerms', '$searchType', $resultCount
     );
 
 EOT;
@@ -546,7 +560,7 @@ EOT;
         // Use trackSiteSearch *instead* of trackPageView in searches
         return <<<EOT
     VuFindPiwikTracker.trackSiteSearch(
-        'Combined|$searchTerms', '$searchType', $resultCount
+        '{$this->searchPrefix}Combined|$searchTerms', '$searchType', $resultCount
     );
 
 EOT;
-- 
GitLab