robflaherty/riveted

Document issue with loading

SonOfLilit opened this issue · 3 comments

This code sometimes ran before GA was loaded:

    /*
     * Determine which version of GA is being used
     * "ga", "_gaq", and "dataLayer" are the possible globals
     */

    if (typeof ga === "function") {
      universalGA = true;
    }

    if (typeof _gaq !== "undefined" && typeof _gaq.push === "function") {
      classicGA = true;
    }

    if (typeof dataLayer !== "undefined" && typeof dataLayer.push === "function") {
      googleTagManager = true;
    }

I don't understand why not to put it in init() and ask people to call init() as:

  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
  ga('create', 'UA-XXXXXX', 'xxxxxxxxxxxxxxxx');
  ga('send', 'pageview');
  ga(function() {
    riveted.init();
  });

(which I did too, just to be safe), but at least document that riveted should be loaded after GA, violating their instruction to be loaded last on the page.

Thanks for the feedback. It's OK for that code to run before GA has finished loading, as it's just detecting the global variable. But there's no reason it couldn't be moved into Riveted's init function.

Regarding wrapping the init call in a ga function, I'd like to avoid users having to modify their GA implementation.

I can also add a note to the docs mentioning that the script should be loaded after GA.

Then you probably should.

I'd do it visually, by adding the GA code to the usage example.

On Thu, Jun 5, 2014 at 10:31 PM, Rob Flaherty notifications@github.com
wrote:

Thanks for the feedback. It's OK for that code to run before GA has
finished loading, as it's just detecting the global variable. But there's
no reason it couldn't be moved into Riveted's init function.

Regarding wrapping the init call in a ga function, I'd like to avoid
users having to modify their GA implementation.

I can also add a note to the docs mentioning that the script should be
loaded after GA.


Reply to this email directly or view it on GitHub
#7 (comment).

Moved GA type detection into Riveted init function 4b25774