Richard Parnaby-King

Web Developer – PHP, Zend Framework and Actionscript 3

Posted on | | No Comments

Using Google Analytics it is possible to track pages as though a user had visited it, even if the page does not exist. This is useful for, for example, ajax form submissions – instead of redirecting to a thank-you page, you can leave the user on the same page and track a custom page (that does not exist) in Google Analytics.

Below is the minimum configuration needed to track a page asynchronously.

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

To submit a custom page, have the call-back of the ajax form request echo the following code:

<script type=”text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(["_trackPageview", "your-custom-page-here"]);
</script>

If you are not using an ajax request to submit a form, e.g. you are submitting the form to the same page, then simply add the above code after the Google Analytics code.

Posted By:

Comments

  • ABOUT

    Having three years of programming experience in PHP, Zend Framework and ActionScript3, I have a very strong working knowledge of object orientated programming.

    I am a PC Gamer! Playing FPS, RTS, RPG and the occasional MMO since 1996 I have a huge number of a variety of fast-paced games.

  • Recent Posts

  • Categories

  • RSS SUBSCRIBE TO OUR FEED

  •