title: How do I get Google Analytics to track pages called by AJAX?
body: <p>I am calling a page on my website using AJAX but for some reason Google Analytics is not registering the page visit. Do I have to do something to force it to register AJAX triggered pages?</p>
  
  <p>I am using the latest Universal anaytics code as follows:</p>
  
  <pre><code>&lt;script&gt;
    (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-XXXXXXXX-1', 'mywebsite.ext');
    ga('send', 'pageview');
  
  &lt;/script&gt;
  </code></pre>
  
  <p>I am using a JQuery AJAX call to the page containing the above snippet as follows:</p>
  
  <pre><code>&lt;script&gt;
  //&lt;![CDATA[             
  $(document).ready(function(){
  
          $.ajax({
                  url : "http://www.mywebsite.ext",
                  type: "GET",
                  async: false,
                  data : { fromajax : "y" },
                  success: function(data, textStatus, jqXHR)
                  {
                          // alert("success - Data: " + data + "\ntextStatus: " + textStatus);
                  },
                  error: function (jqXHR, textStatus, errorThrown)
                  {
                          alert("error \ntextStatus: " + textStatus + "\nerrorThrown: " + errorThrown + "\njqXHR: " + jqXHR);
                  }
          });
  
  });
  //]]&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am certain the page is being called as I have some logging statements writing to a database. I also know the Analytics code is working as I am testing it using the real time overview.</p>
  
  <p>So to summarise, I have a page calling <a href="http://www.mywebsite.ext" rel="noreferrer">http://www.mywebsite.ext</a> using AJAX and the destination page (<a href="http://www.mywebsite.ext" rel="noreferrer">http://www.mywebsite.ext</a>) contains some Universal Analytics code which does not appear to track the page. If you visit the page normally from your browser the page is tracked fine.</p>
  
  <p>I have since discovered from the thread that I can call the "ga" function with a virtual folder. I have therefore done away with the AJAX call and instead modified the Universal Analytics as follows:</p>
  
  <pre><code>&lt;script&gt;
  (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-XXXXXXXX-1', 'mywebsite.ext');
  ga('send', 'pageview', '/localfolder/page');
  ga('send', 'pageview');
  
  &lt;/script&gt;
  </code></pre>
  
  <p>The problem I have now is that it calls ga('send', 'pageview', '/localfolder/page'); but not ga('send', 'pageview');</p>
  
  <p>But I understand there is a minimum interval between events:
  <a href="https://stackoverflow.com/questions/5537523/google-analytics-event-tracking-minimal-interval-between-the-events">Google Analytics Event Tracking - Minimal Interval between the events</a></p>
  
  <p>I therefore added a setTimeout(function(){},2000); between events and the last event is still not called. I even went up to 9 seconds.</p>