Skip to content

Instantly share code, notes, and snippets.

@visnaut
Created May 11, 2012 15:45
Show Gist options
  • Save visnaut/2660533 to your computer and use it in GitHub Desktop.
Save visnaut/2660533 to your computer and use it in GitHub Desktop.
Load Google Analytics Asynchronously via yepnope.js (Modernizr.load)
/**
* Using Google Analytics & yepnope.js (Modernizr.load) in the same project?
* Replace Google's default snippet with the code below to load and initialize GA asynchronously.
*
* Don't forget to copy and paste your full web property ID.
*
* If you use Modernizr:
* 1. Build with the Modernizr.load build option:
* http://modernizr.com/download/#-load
* 2. Replace 'yepnope' below with 'Modernizr.load'
*
* If you use jQuery:
* - Delay initialization until DOM is ready by wrapping the _gaq.push() call with:
* $(document).ready(function() { _gaq.push(...); });
*
* You can verify GA initialized correctly by running the following in your console:
* _gat._getTrackerByName()._getAccount();
*/
yepnope([
{
load: (document.location.protocol === 'https:' ? '//ssl' : 'http://www') + '.google-analytics.com/ga.js',
complete: function() {
_gaq.push(['_setAccount', 'UA-XXXXXX-X'], ['_trackPageview']);
}
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment