Hi there,
has anyone experiences on how to track "add to cart" actions in VM 3.x and Google Analytics when this happens with AJAX?
Cheers,
Pyrrus
Here is a sample :
$(document).ready(function() {
$("form.js-recalculate").each(function() {
var jqForm = $(this);
var jsForm = this;
var action = jqForm.attr("action");
jqForm.submit(function(event) { // when someone submits the form(s)
event.preventDefault(); // don't submit the form yet
_gaq.push(["_trackPageview", window.location.pathname]); // create a dynamic pageview
setTimeout(function() { // now wait 300 milliseconds...
jsForm.submit(); // ... and continue with the form submission
},300);
});
});
});
See http://www.lunametrics.com/blog/2013/07/02/jquery-event-tracking-generator-google-analytics/, for the generator.
It's possible you have to modify it a little and the code itself can be simplified.
I think it only work when you add it in YOURSITE\components\com_virtuemart\assets\js\vmprices.js
Virtuemart.sendtocart = function (form){
if (Virtuemart.addtocart_popup ==1) {
Virtuemart.cartEffect(form) ;
} else {
form.append('<input type="hidden" name="task" value="add" />');
form.submit();
}
}
change it to :
Virtuemart.sendtocart = function (form){
_gaq.push(["_trackPageview", window.location.pathname]); // create a dynamic pageview
setTimeout(function() { // now wait 300 milliseconds...
if (Virtuemart.addtocart_popup ==1) {
Virtuemart.cartEffect(form) ;
} else {
form.append('<input type="hidden" name="task" value="add" />');
form.submit();
}
},300);
}
Perhaps you don't need to use setTimeout but simply the line : _gaq.push(["_trackPageview", window.location.pathname]); when you use modal popup
Note, i don't tested it and you need to load your google analytics tracking code before in all page or your add to cart fail.