ZK - Open Source Ajax Java FrameworkZK - Open Source Ajax Java Framework

Tracking ajaz requests with Google analytics

kapil4ya
21 May 2010 01:43:56 GMT
21 May 2010 01:43:56 GMT

Guys, I need to make use of Google analytics, I am using ZK EE 5.0.2 library.

On this url : http://docs.zkoss.org/wiki/New_Features_of_ZK_5.0.2

I see a section named " Tracking Ajax requests with Google Analytics" which says you need to put a particular code in your zul files. I am lost as to where I need to put that code?

Please can someone help me out here ?

Thanks

madruga0315Top Contributor
21 May 2010 07:25:53 GMT
21 May 2010 07:25:53 GMT

Agreed,

I also would like a more in depth view on this feature.

Also a demo with some sample data, showing the results on GA would be great to taste it and have a idea of the impact of this feature.

King regards,
Madruga

kapil4ya
21 May 2010 23:57:46 GMT
21 May 2010 23:57:46 GMT

Does anyone has any knowledge about this? The docs are very unclear about this.

PeterKuo
23 May 2010 22:07:39 GMT
23 May 2010 22:07:39 GMT

Please refer to
http://docs.zkoss.org/wiki/Client_Side_Programming#Override_a_Default_Widget_Method_for_Whole_Application

madruga0315Top Contributor
24 May 2010 08:17:38 GMT
24 May 2010 08:17:38 GMT

Hi perter,

Thanks for the javadoc. But we're looking for more info on the script itself, how it works, how to configure, sample data etc...
Could/Is this feature being used on zk forum?

Regards,
Madruga

PeterKuo
3 Jun 2010 21:24:48 GMT
3 Jun 2010 21:24:48 GMT

You can look into the au.js source code of zk.

beforeSend() is called inside sendNow()
/** Enforces all pending AU requests of the specified desktop to send immediately
* @param Desktop dt
* @return boolean whether it is sent successfully. If it has to wait
* for other condition, this method returns false.
*/
sendNow: function (dt) {

And pageTracker._trackPageview is defined by google analytic to log such event.
Therefore, the script is enough to log the event.

var auBfSend = zAu.beforeSend;
zAu.beforeSend = function (uri, req) {
try {
var target = req.target;
if (target.id) {
var data = req.data||{},
value = data.items && data.items[0]?data.items[0].id:data.value;
pageTracker._trackPageview((target.desktop?target.desktop.requestPath:"") + "/" + target.id + "/" + req.name + (value?"/"+value:""));
}
} catch (e) {
}
return auBfSend(uri, req);
};

kapil4ya
14 Jun 2010 03:06:31 GMT
14 Jun 2010 03:06:31 GMT

Thank you Peter for the help, here is the problem I am facing...


I have included this google analytics code in each zul page of our application...


<script type="text/javascript">
	
	
   var _gaq = _gaq || [];
   _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
   _gaq.push(['_trackPageview']); 

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
	
	
</script>

Where the account property ID is properly set.

Now, I make an entry in the zk.xml as follows to execute a java script code each time a request comes :


<language-config>
		<addon-uri>/WEB-INF/analytics-lang-addon.xml</addon-uri>
	</language-config>

And the contents of the analytics-lang-addon.xml are as follows -

<?xml version="1.0" encoding="UTF-8"?>
<language-addon>
	<language-name>xul/html</language-name>
	<javascript src="/js/analytics.js" charset="UTF-8"/>	
</language-addon>

So, eventually, this js gets called each time, we are half done till now. The analytics.js looks something like -


var auBfSend = zAu.beforeSend;

zAu.beforeSend = function (uri, req) {
	
	try {
		var target = req.target;
		
		if (target.id) {
			var data = req.data||{},
				value = data.items && data.items[0]?data.items[0].id:data.value;
			pageTracker._trackPageview((target.desktop?target.desktop.requestPath:"") + "/" + target.id + "/" + req.name + (value?"/"+value:""));
		}
	} catch (e) {
		alert('error while logging '+e.message);
	}
	alert('value '+auBfSend(uri, req));
	return auBfSend(uri, req);
};



With this code, I am able to see something being returned, but at the same time it says in the catch statement that "pageTracker is not defined".

With this analytics module we are having, the page views requests are logged but the ajax requests are not logged.

Where do you thing I am missing the things? I suppose, I am on the right track but just can not get all the things work together in unison.

Thank you again!

iantsai
17 Jun 2010 21:21:44 GMT
17 Jun 2010 21:21:44 GMT

you can trace the code inside ZKDemo, at /userguide/index.zul there's a runnable code which is used in online ZKDemo Service.

moonline
27 Dec 2011 02:22:42 GMT
27 Dec 2011 02:22:42 GMT

<script type="text/javascript">


var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; http://www.lalinguaarabapertutti.com/s.parentNode.insertBefore(ga, s);


i tried it it is working
})();


</script>