Google Analytics: _gaq.push multiple items from another array
I'm triyng to push multiple items into the _gaq.push() for google analytics.
I have an array of Ids that im looping through to create the array to pass to .push();
var gaDetails = new Array();
var productIdsArray = productIds.split(",");
for(var i = 0; i < productIdsArray.length; ++i)
gaDetails.push(['_trackEvent', 'Quote', '' + step, '' + productIdsArray[i]]);
_gaq.push(gaDetails);
It looks like theres an extra set of [] around each array. Maybe I'm not seeing some开发者_如何学Gothing but the way google describes their syntax looks wrong?
you don't need the other array, and while you're at it you might as well use a faster loop.
var productIdsArray = productIds.split(","),
i = productIdsArray.length;
while(i--)
{
_gaq.push(['_trackEvent', 'Quote', '' + step, '' + productIdsArray[i]]);
}
As mentioned in the comment from Ryan, Google encourages to push an multiple commands via one call of _gaq.push
If one has an array of commands and wants to add them all to _gaq this can be achieved by
_gaq.push.apply(_gaq, gaDetails);
Got a clue from Javascript push array values into another array
For performance it's probably fine to just call _gaq.push with each element.
精彩评论