title: JavaScript const and var and Google Analytics
body: <p>I have some code that is basically a class to encapsulate tracking with Google Analytics. It looks like</p>
  <pre><code>const cecTrack = window['cecTrack'] || ( () =&gt; {
      let _initialized;
      let _toTrack = [];
      let _gaUser;
      let _lastView;
      let _appName;
      let _campaignSource;
      let _gaKey;
  
      var _ga = function(){
          if(window['ga'] &amp;&amp; arguments &amp;&amp; arguments.length){
              const a = arguments;
  
              setTimeout(function(){
                  window['ga'].apply(null, a);
              }, 500);
          }
      }
  
      var _gan = function(){
          if(window['ga'] &amp;&amp; arguments &amp;&amp; arguments.length){
              const a = arguments;
  
              window['ga'].apply(null, a);
          }
      }
  </code></pre>
  <p>. . . .</p>
  <p>I have omitted the details specific to the application. I have seen the recommendation that let or const is preferred to var. Yet when I change either of these functions to something like</p>
  <pre><code>const _ga = () =&gt; {
  </code></pre>
  <p>Google Analytics doesn't seem to work. Something breaks the initialization. The only thing common to these functions is reference to a global variable. Could that be the problem? Anyone with direct experience with GA, do you have any advice that I might use so that I can replace var with const or?</p>