Skip to content

Instantly share code, notes, and snippets.

@jesgundy
Forked from ismyrnow/google-analytics-amd.js
Created August 19, 2016 17:41
Show Gist options
  • Save jesgundy/2f5b49dc10657067a39f3c07b90cf61f to your computer and use it in GitHub Desktop.
Save jesgundy/2f5b49dc10657067a39f3c07b90cf61f to your computer and use it in GitHub Desktop.
Google Analytics AMD Module
define(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", "{{TrackingID}}", "{{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(["//www.google-analytics.com/analytics.js"]);
return module;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment