diff --git a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
index c69324b309de3b510f64cf8a564609c9ce85ceb7..ae1f1a899e3eeb4e6224a822e6bd30576c87c61a 100644
--- a/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
+++ b/module/VuFind/src/VuFind/View/Helper/Root/Piwik.php
@@ -417,6 +417,7 @@ class Piwik extends \Zend\View\Helper\AbstractHelper
      */
     protected function getOpeningTrackingCode()
     {
+        $escape = $this->getView()->plugin('escapejs');
         return <<<EOT
 
 function initVuFindPiwikTracker{$this->timestamp}(){
@@ -424,7 +425,7 @@ function initVuFindPiwikTracker{$this->timestamp}(){
 
     VuFindPiwikTracker.setSiteId({$this->siteId});
     VuFindPiwikTracker.setTrackerUrl('{$this->url}piwik.php');
-    VuFindPiwikTracker.setCustomUrl('{$this->getCustomUrl()}');
+    VuFindPiwikTracker.setCustomUrl('{$escape($this->getCustomUrl())}');
 
 EOT;
     }
@@ -436,7 +437,7 @@ EOT;
      */
     protected function getCustomUrl()
     {
-        $path = $this->request->getUri()->getPath();
+        $path = $this->request->getUri()->toString();
         $routeMatch = $this->router->match($this->request);
         if ($routeMatch
             && $routeMatch->getMatchedRouteName() == 'vufindrecord-ajaxtab'