How to handle blocked scripts in requirejs

require takes a 3rd argument which is an error callback, so you can assign window.ga to a function which always returns undefined. This avoids errors when calling google analytics functions elsewhere in your code.

require(['ga'], function(data) {
  window.ga('create', 'UA-XXXXXXXX-X');
  window.ga('send', 'pageview');
}, function() {
  window.ga = function(){};
});

You could require the module within your own module code but outside of the module definiton requirements, but this does mean you can't quite as easily chain on dependencies you need. i.e.

define([ /* Normal dependencies here ... */], function() {

    try {
        require(['ga']);
    } catch (error) {
        // Handle lack of GA if needed
    }

};

Alternatively you'd have to write your own module wrapper which synchronously blocks as it attempts the above, then returns GA if it was successful, or null otherwise.