cujojs / curl

curl.js is small, fast, extensible module loader that handles AMD, CommonJS Modules/1.1, CSS, HTML/text, and legacy scripts.
https://github.com/cujojs/curl/wiki
Other
1.89k stars 216 forks source link

Google Analytics AMD module works with curl.js #241

Closed szepeviktor closed 10 years ago

szepeviktor commented 10 years ago

I couldn't find in google - so I've made an issue

this is a working (tested) AMD module for Google Analytics (Universal Analytics analytics.js) https://gist.github.com/ismyrnow/6252718

unscriptable commented 10 years ago

Interesting. Thanks @szepeviktor!

szepeviktor commented 10 years ago

I use a WP global object (printed in the head by WordPress)

// AMD module: analytics
// https://gist.github.com/ismyrnow/6252718
define('analytics', function (require) {

  var module;

  // Setup temporary Google Analytics objects.
  window.GoogleAnalyticsObject = 'ga';
  window.ga = function () { (window.ga.q = window.ga.q || []).push(arguments); };
  window.ga.l = 1 * new Date();

  // Immediately add a pageview event to the queue.
  window.ga('create', WP.ga.UA, WP.ga.domain);
  window.ga('send', 'pageview');

  // Create a function that wraps `window.ga`.
  // This allows dependant modules to use `window.ga` without knowingly
  // programming against a global object.
  module = function () { window.ga.apply(this, arguments); };

  // Asynchronously load Google Analytics, letting it take over our `window.ga`
  // object after it loads. This allows us to add events to `window.ga` even
  // before the library has fully loaded.
  require(['ga']);

  return module;
});