Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You can't reliably use Array#concat on the _gaq object. Once Google Analytics loads, it replaces the _gaq array with a custom object that has it's own push() method. That object doesn't have concat() on it.

So, your core coding style will only work with very particular GA implementations; anything that deviates from that could very easily break or be subject to race conditions.

You'll also get better results if you use events rather than custom variables; custom variable reporting isn't as flexible and can be very confusing (ie, visit start). It'll also mitigate the need to use concat() to cut the line, since Events can be fired at any time (whereas custom variables need to happen before the first hit of the session to be well-reported).

I also really like the idea, though. I actually pass all my Optimizely test results as Google Analytics events with this snippet:

     if (window.optimmizely && optimizely.variationNamesMap) {
		var optmap = optimizely.variationNamesMap;
		for (var expt in optmap) {
			if (optmap.hasOwnProperty(expt)) {
				_gaq.push(["_trackEvent", "Optimizely", optimizely.allExperiments[expt].name, optmap[expt], 0, true]);
			}
		}
	}


Concat is only called before GA is loaded, so I don't think any race condition could happen.


Only if everyone loads it the same way. But there are lots of different ways to load GA; the only reason _gaq.push() isn't scary and doesn't raise the specter of race conditions is because of the 'spoofing' GA does.

There are LOTS of different versions of the GA snippet floating around the internet; the assumptions this configuration makes would only work for some of them.

For example, some people load GA like this:

     <script async src="//google-analytics.com/ga.js">     </script>
     <script>
     var _gaq = _gaq || [];
     ...
     </script>
Folks who do this will break at minimum with older browsers that don't support async; not sure what it would do with async support, though.

It seems unnecessarily risky.


Ok, now I understand what the issue is. I guess a good solution would be to pass _gaq to my library initializer and push every value with a for cycle, instead of using concat. Thanks for the tip.


I updated the library on Github: now it doesn't use concat anymore!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: