Set custom action titles for Ajax request to improve Piwik integration

Change-Id: I38fe710584b838f6dc9215bf9ec212db1abc9877
diff --git a/Changes b/Changes
index 8275345..1d19d04 100755
--- a/Changes
+++ b/Changes
@@ -1,4 +1,4 @@
-0.30 2018-11-12
+0.30 2018-11-13
         - Rewrote backend:
           - Removed dependency of Mojolicious::Plugin::Search.
           - Removed abstract API.
diff --git a/dev/js/src/api.js b/dev/js/src/api.js
index 1d71550..e536ea2 100644
--- a/dev/js/src/api.js
+++ b/dev/js/src/api.js
@@ -9,7 +9,7 @@
   KorAP.URL = KorAP.URL !== undefined ? KorAP.URL : '';
 
   KorAP.API = KorAP.API || {};
-  
+
   /**
    * Retrieve information about a match
    */
@@ -28,31 +28,39 @@
 
     // This is for legacy support
     var legacy = legacySigle.exec(match.textSigle);
+    var docFragment = "";
     if (legacy !== null && legacy[0]) {
-      url += '/' + legacy[1] + '/' + legacy[2] + '/' + legacy[3];
+      docFragment = legacy[1] + '/' + legacy[2] + '/' + legacy[3];
     }
     else {
-      url += '/' + match.textSigle;
+      docFragment = match.textSigle;
     }
-    
-    url += '/' + match.matchID;
+
+    docFragment += '/' + match.matchID;
+    url += '/' + docFragment;
 
     // { spans: true, layer:x, foundry : y}
     if (param['spans'] == true) {
       url += '?spans=true';
-      if (param['foundry'] !== undefined)
+      docFragment += ' +spans ';
+      if (param['foundry'] !== undefined) {
 	      url += '&foundry=' + param['foundry'];
-      if (param['layer'] !== undefined)
+        docFragment += param['foundry'];
+      };
+      if (param['layer'] !== undefined) {
 	      url += '&layer=' + param['layer'];
+        docFragment += '/'+param['layer'];
+      }
     }
     
     // { spans : false, layer: [Array of KorAP.InfoLayer] }
     else {
       // TODO
+      docFragment += ' -spans';
       url += '?spans=false';
     }
 
-    KorAP.API.getJSON(url, cb);
+    KorAP.API.getJSON(url, cb, "MatchInfo: " + docFragment);
   };
 
 
@@ -72,7 +80,7 @@
     else {
       url += '?fields=@all'; // TODO: Maybe '*'?
     }
-    KorAP.API.getJSON(url, cb);
+    KorAP.API.getJSON(url, cb, "TextInfo: " + doc.textSigle);
   };
 
 
@@ -80,7 +88,7 @@
    * Retrieve information about collections
    */
   KorAP.API.getCollections = function (cb) {
-    KorAP.API.getJSON(KorAP.URL + '/collection', cb);
+    KorAP.API.getJSON(KorAP.URL + '/collection', cb, "CorpusInfo");
   };
 
 
@@ -114,21 +122,24 @@
    */
   KorAP.API.getCorpStat = function (cq, cb){
   	var url  =  KorAP.URL;
-    url += "/corpus?cq=" + cq;
-  	KorAP.API.getJSON(url, cb);
+    url += "/corpus?cq=" + encodeURIComponent(cq);
+  	KorAP.API.getJSON(url, cb, "CorpusInfo: " + cq);
   };
   
   /**
    * General method to retrieve JSON information
    */
-  KorAP.API.getJSON = function (url, onload) {
+  KorAP.API.getJSON = function (url, onload, title) {
     var req = new XMLHttpRequest();
     req.open("GET", url, true);
 
     // Dispatch global "window" event
     var reqE = new CustomEvent('korapRequest', {
       bubbles : false,
-      detail: { "url" : url }
+      detail: {
+        "url" : url,
+        "title" : title
+      }
     });
     window.dispatchEvent(reqE);
     
diff --git a/dev/js/src/view/corpstatv.js b/dev/js/src/view/corpstatv.js
index 9d4da6d..1cc6f0c 100644
--- a/dev/js/src/view/corpstatv.js
+++ b/dev/js/src/view/corpstatv.js
@@ -35,13 +35,9 @@
     getStatistic : function(cb) {
       // cq = corpusQuery
       var vc = this.vc;
-      
-       // cq = corpusQuery
-      var cq;
 
-      cq = encodeURIComponent(vc.toQuery());
       try {
-        KorAP.API.getCorpStat(cq, function(statResponse) {
+        KorAP.API.getCorpStat(vc.toQuery(), function(statResponse) {
           if (statResponse === null) {
             cb(null);
             return;
diff --git a/lib/Kalamar/Plugin/Piwik.pm b/lib/Kalamar/Plugin/Piwik.pm
index 8bdcd1d..bffbbdb 100644
--- a/lib/Kalamar/Plugin/Piwik.pm
+++ b/lib/Kalamar/Plugin/Piwik.pm
@@ -30,6 +30,7 @@
 % if (stash('piwik.embed')) {
   %= javascript begin
 window.addEventListener('korapRequest', function(e) {
+  _paq.push(['setDocumentTitle', e.detail.title]);
   _paq.push(['setReferrerUrl', location.href]);
   _paq.push(['setCustomUrl', e.detail.url]);
   _paq.push(['trackPageView']);
diff --git a/t/plugin/piwik.t b/t/plugin/piwik.t
index 28c770b..5559cc6 100644
--- a/t/plugin/piwik.t
+++ b/t/plugin/piwik.t
@@ -20,6 +20,9 @@
   ->element_exists('section[name=piwik-opt-out] iframe')
   ->content_like(qr!var _paq!)
   ->content_like(qr!window\.addEventListener\('korapRequest!)
+  ->content_like(qr!setDocumentTitle!)
+  ->content_like(qr!setCustomUrl!)
+  ->content_like(qr!trackPageView!)
   ;
 
 # No embedding