I am adding a Google Analytics tracking event to nav menu links, with this code:
(function ($) {
"use strict";
$(function () {
$("body").on('click', '.menu-item a', function () {
var trackingCode = $(this).next(".ga-tracking");
if (trackingCode.length > 0) {
var t1 = trackingCode.data("tracking-1"),
t2 = trackingCode.data("tracking-2"),
t3 = trackingCode.data("tracking-3"),
t4 = trackingCode.data("tracking-4"),
params = "'" + t1 + "','" + t2 + "','" + t3 + "'";
_gaq.push([params]);
}
});
});
}(jQuery));
you can see it working here: http://paulwp.com/blog/
to trigger the code, click on the Blog link in the top black bar.
This is the error I get:
_gaq.push processing "'_trackEvent','Store_Outbound','Link_Click'" for args: "[]":
Called method "'_trackEvent','Store_Outbound','Link_Click'" threw exception.TypeError: Cannot call method 'apply' of undefined
whereas it should give something like this:
_gaq.push processing "_trackEvent" for args: "[Store_Outbound,Link_Click]":
guess it’s the way I build the parameters with the variables that’s causing the issue
You are passing the values wrong. This is effectively what you are doing:
_gaq.push(["a,b,c"]);That is an array with a single element.
This is what it should look like:
_gaq.push(["a","b","c"]);This is an array with multiple elements.
So basically you need to do
or just put them directly in: