title: Use Google Analytics with Meteor
body: <p>What is the best way to include Google Analytics tracking code on your Meteor app, conditionally only for production environment only?</p>
  
  <p>I am familiar with <a href="http://hackerpreneurialism.com/post/38115369392/google-analytics-with-meteor-js" rel="nofollow">this guide</a>, however it only works for a specific template. I would like to track across all pages and templates.</p>
  
  <p>EDIT: Currently, I am doing URL matching to check if I am on production level, and then include the tracking code from Google. Not sure if this is best practice though.</p>
  
  <pre><code>// main.js
  var production_url = /^my.production.url$/
  if (production_url.test(window.location.host)) {
      (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', 'TRACKING_CODE', 'production.url');
      ga('send', 'pageview');
  }
  </code></pre>
  
  <p>The code above is put directly in the <code>main.js</code> file and not under any Template instance.</p>