Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
});
@szepeviktor

This comment has been minimized.

Copy link

@szepeviktor szepeviktor commented Dec 3, 2013

thank you!
it works in curl.js
cujojs/curl#241

@iDVB

This comment has been minimized.

Copy link

@iDVB iDVB commented Jan 12, 2014

Anyone have any issues where this is working locally with hostfile change but not in production?
GA docs say "be sure the tracking code in included just before the closing /HEAD tag. RequireJS is usually just before the closing /BODY tag, not not sure if thats an issue?

Its the weirdest thing that I have this working locally and pushed it live and now GA Admin is not reporting any traffic under Realtime at all.

I have the url in GA admin set to www.jamdeo.com and the domain in the tracking code set to the same.

@saxicek

This comment has been minimized.

Copy link

@saxicek saxicek commented Feb 13, 2014

You should use require(["//www.google-analytics.com/analytics.js"] instead of require(["http://www.google-analytics.com/analytics.js"].

@ismyrnow

This comment has been minimized.

Copy link
Owner Author

@ismyrnow ismyrnow commented Jun 5, 2014

Thanks @saxicek for that change. I've updated the gist.

For anyone wondering, I'm using this in production with RequireJS and the R.js optimizer, and it works great.

@ltodorov

This comment has been minimized.

Copy link

@ltodorov ltodorov commented Aug 2, 2014

I would recommend the Google's option for changing the global name of the analytics object.
Just add another variable gaName and replace all window.ga instances with window[gaName]

define(function (require) {

  var module,
      gaName = "ga"; // Global name of analytics object. Defaults to `ga`.

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

  // Immediately add a pageview event to the queue.
  window[gaName]("create", "{{TrackingID}}", "{{Domain}}");
  window[gaName]("send", "pageview");

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

  // Asynchronously load Google Analytics, letting it take over our `window[gaName]`
  // object after it loads. This allows us to add events to `window[gaName]` even
  // before the library has fully loaded.
  require(["//www.google-analytics.com/analytics.js"]);

  return module;

});
@paulcanning

This comment has been minimized.

Copy link

@paulcanning paulcanning commented Nov 3, 2015

If I add this to my RJS project, how exactly do I use it?

@prrraveen

This comment has been minimized.

Copy link

@prrraveen prrraveen commented Jan 13, 2016

@paulcanning. You can just just add the module to your require / define method. It will take care of rest of things.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment