Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jaridmargolin
Created May 14, 2014 21:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaridmargolin/c0e91638c5248ad4d94f to your computer and use it in GitHub Desktop.
Save jaridmargolin/c0e91638c5248ad4d94f to your computer and use it in GitHub Desktop.
AMD google analytics compatible with almond.js
/*!
* analytics.js
*
* Copyright (c) 2014
*/
define([
'require',
'env'
], function (require, env) {
// ----------------------------------------------------------------------------
// Module
// ----------------------------------------------------------------------------
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', env.gaID, 'auto');
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.
var head = document.getElementsByTagName('head')[0],
script = document.createElement('script');
script.async = true;
script.src = 'http://www.google-analytics.com/analytics.js';
head.appendChild(script);
// ----------------------------------------------------------------------------
// Expose
// ----------------------------------------------------------------------------
return module;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment