It would be simpler to do this :
var ga = ga || (function(){});
If ga
is defined, this does nothing. If it's undefined
, it sets its value to a no-op function, preventing the error.
But I don't think it's a good idea to disable the script when you develop : it makes one more reason to have an unexpected bug in production. The best practice here would be, in my opinion, to add a filter in Google Analytics. See Exclude internal traffic.