Skip to content

Instantly share code, notes, and snippets.

@getify
Created November 24, 2010 15:09
Show Gist options
  • Save getify/713779 to your computer and use it in GitHub Desktop.
Save getify/713779 to your computer and use it in GitHub Desktop.
loading google analytics using LABjs
<!DOCTYPE html>
<html>
<head>
<title>LABjs Demo</title>
</head>
<body>
<!-- some stuff -->
<script src="/js/LAB.js"></script>
<script>
$LAB
.script(('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js')
.wait(function(){
var pageTracker = _gat._getTracker("UA-XXXXX-X");
pageTracker._trackPageview();
});
</script>
</body>
</html>
@jimmykhlam
Copy link

@getify thanks for the explanation.

Where would one typically chain in the google analytics loading? My initial thought was at the end, but my confusion now is whether any .wait() calls above it would cause it to become 'dependant' on the execution of other code. In which case should it be included before any .wait() calls instead so that it can execute async to everything else?

<script>
   $LAB
   .script("framework.js").wait() //should google analytics loading come before this line?
   .script("plugin.framework.js")
   .script("myplugin.framework.js").wait()
   .script("init.js")
   .script(('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js') //or here?
       .wait(function(){
            var pageTracker = _gat._getTracker("UA-XXXXX-X");
            pageTracker._trackPageview();
    });
</script>

@joedevon
Copy link

@jimmykhlam I believe it's up to you. Is site functionality less important to you than the analytics? Then put ga first. Otherwise put it last.

@getify
Copy link
Author

getify commented Nov 23, 2011

i recommend loading google analytics with an entirely separate chain. It's neither dependent on, nor a depdency of, any other code in your page. so load the rest of the code in your page in one chain, and use a separate chain for google analytics.

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