1

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 something but the way google describes their syntax looks wrong?

Ryan
  • 4,354
  • 2
  • 42
  • 78

2 Answers2

3

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]]);
}
SavoryBytes
  • 35,571
  • 4
  • 52
  • 61
  • 1
    But theres a way to pass multiple objects to the .push method according to google's site – Ryan Apr 07 '11 at 20:05
0

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.

Community
  • 1
  • 1
Cilvic
  • 3,417
  • 2
  • 33
  • 57