Commits

Nick Freear committed 471e7f6

Fixes to capret-piwik.js - so the tracking-GIF works when pasted..

* URL encode '_cvar' custom variables JSON array, url, action_name segments etc.
* Add 'copyTime' (ct) to '_cvar' custom variables,
* Tidied Javascript,
* Todo: Piwik plugin, implementing 'Tracker.getNewVisitObject' hook

  • Participants
  • Parent commits 95fc316

Comments (0)

Files changed (1)

application/assets/capret/js/capret-piwik.js

 * N.D.Freear, 2012-08-21.
 * See, http://piwik.org/docs/tracking-api/reference
 */
+
 (function( jQuery ){
 	jQuery = jQuery.noConflict(true);
 	var
-	  //piwik_url_0 = 'http://track.olnet.org/piwik/',
 	  piwik_url = get_data('url', 'http://track.olnet.org/piwik/')
 	, idsite = get_data('idsite', 1)
-	, rec = get_data('rec', 1)
+	, record = get_data('rec', 1)
 	, debug = get_data('debug', false)
 	// Aliases
 	, M = Math
+	, Enc = encodeURIComponent
 	, J = JSON  //3rd party?
 	, doc = document
 	, win = window
 	}
 	function final_params_pi(copy_text, env){
 		var tx = truncate(copy_text, 100);
-		env._cvar['2'] = ['length', copy_text.length]; //'len'
-		env._cvar['3'] = ['text', tx]; //'txt' 50 //escape()
-		env._cvar['4'] = ['modified', doc.lastModified]; //'lmod'
 
 		// Piwik..
 		env.idsite = idsite;
-		env.rec = rec;
+		env.rec = record;
 		env.rand = M.floor(M.random()*100);
-		env.action_name = 'CaPReT/ '+ document.title +'/ "'+ tx +'"';
-		env._cvar = J.stringify(env._cvar);
+		env.action_name = Enc(doc.title) +'/'+ Enc(' '+tx); //+'"';
+
+		//Custom variable JSON (last, in case it is stripped on paste?)
+		env._cvar = {
+			'1': ['via', 'CaPReT'],
+			'2': ['length', copy_text.length], //'len'
+			'3': ['text', tx], //'txt'
+			'4': ['modified', doc.lastModified], //'lmod'
+			'5': ['copyTime', new Date().toUTCString()] //.toISOString?
+		};
+		env._cvar = Enc( J.stringify(env._cvar) );
+
 		return jQuery.param(env);
 	}
 	function image_tag_piwik(copy_text, env){
 		img = '<img src="'+ piwik_url +'piwik.php?' + final_params_pi(copy_text, env) + '"/>';
 		if (debug) {
-			console.log(img, env, piwik_url);
-			console.log(J.stringify({a:1}));
+			console.log(img);
+			console.log(env);
+			//console.log(J.stringify({a:1}));
 		}
 		return img;
 	}
 	jQuery(function() {
-	  var env = {};
-	  env._cvar = {
-	    '1':['via', 'capret']
-	    //, '2':[]
-	  };
-	  env.url = doc.location.href;
-	  env.res = win.innerWidth +'x'+ win.innerHeight;
-	  //env.bw = window.innerWidth;
-	  //env.bh = window.innerHeight;
-		env.ct = new Date().getTime();
+		var env = {};
+		env.url = Enc(doc.location.href);
+		env.res = Enc(win.innerWidth +'x'+ win.innerHeight);
 		//env.id = make_id();
 		var license = license_parser.get_license();
 		jQuery('body').clipboard({
 				// A side effect of adding the image tag to the clipboard is that the browser will make a request out to the stats server.
 				// That notifies us that text was copied
 				if (jQuery.browser.msie) {
-					// A fix for MSIE 8.. seems to work!
+					// Maybe a fix for MSIE 8.. seems to work, partly!
 					$('body').append( image_tag_piwik(e, env) );
 
 					return license.license_html;