Use Wicket to generate Google Analytics e-commerce tracking
I'm developing an e-commerce using Wicket to render pages. I have a "BasePage" with the main layout and, in my "thank you for purchasing" page, I want to add Google Analytics e-commerce tracking code. As reference, the JS code like this:
<script type="text/javascript>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345678-9']);
_gaq.push(['_addTrans', '123', '', '12.56', '', '5.00']);
_gaq.push(['_addItem', '123', 'sku-1', 'Product 1', 'Category X', '12.56', '1']);
_gaq.push(['_addItem', '123', 'sku-2', 'Product 2', 'Category Y', '13.45', '1']);
_gaq.push(['_trackTrans']);
(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>
HTML code for cart and checkout was easy, but how can I generate this JS dynamically with Wicket?
Yes just to have a simple use of IHeaderContributor. Btw, are you using a framework for your wicket e-commerce? I have been trying to integrate one or two without much luck.
class BasePage extends Page implements IHeaderContributor {
public BasePage(String id){
super(id);
}
@Override
public void renderHead(IHeaderResponse response){
//You might want renderOnLoadJavascript....
// Btw, I didn't format your string for java, you need to add quotation
// marks or put it as one line.
response.renderJavascript("var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345678-9']);
_gaq.push(['_addTrans', '123', '', '12.56', '', '5.00']);
_gaq.push(['_addItem', '123', 'sku-1', 'Product 1', 'Category X', '12.56', '1']);
_gaq.push(['_addItem', '123', 'sku-2', 'Product 2', 'Category Y', '13.45', '1']);
_gaq.push(['_trackTrans']);
(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);
})();"
);
}
}
I don't think Wicket gives you any help in building the javascript itself, you have to construct the JS string manually, and then either declare your <script>
tag as a Label
component and set the JS string as its model or use the IHeaderContributor
interface.
精彩评论