Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a form which submits using AJAX. My client requested analytics to be updated with a new URL when submit is successful, so they can get statistics on completion vs abandonment of that form.

The analytics code they sent me looks like this

<script>
  (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-00000000-1', 'auto');
  ga('send', 'pageview'); 

</script>

I'm thinking I should use an iframe with this code inside the page to trigger the analytics under a new URL. The iframe page url would have the word success in it so it can be useful in their reporting.

I like the iframe approach because it allows me to call the analytics without refreshing the entire page. I could refresh when the 'success' event occurs, but in a multi-step process, with go-forward and go-back + animation, I am not able to refresh conveniently. I like the iframe solution because it accommodates that flexibility should I need it.

Is that a good solution, or should I be doing it differently?

share|improve this question
    
Shouldn't be any need to add an iframe and just add that code to the bottom of the page. Then you setup events for the ajax i.e onclick="" or _gaq.push as well as many other solutions. Take a look at Google's Event Tracking –  bybe Aug 4 at 19:24
1  
As bybe said you could track using events or tracking "virtual pageviews", without the need of refreshing the page, you need to call google analytics functions using javascript or when receiving success from ajax requests. –  Binarysurf Aug 4 at 23:37
1  
@Binarysurf Maybe you could add that as an answer? –  dan Aug 5 at 22:11

1 Answer 1

up vote 0 down vote accepted

You don't need to use an iframe. This tracking code is for universal analytics. There are a few ways you can push this into Google Analytics:

1) Trigger an Event on submit

You can use Javascript for this or jQuery

$('#button').on('click', function() {ga('send', 'event', 'button', 'click','Form Completed');});

2) Trigger a Virtual Pageview on submit

ga('send', 'pageview', {'page': '/form-completed','title': 'Page Name - Form Completed'});

Both have pros and cons. With Events you cant really set up goal funnels. With Virtual Pageviews you will generate extra pageviews which will mess with your bounce rate.

P.S.

I would also look into using Google Tag Manager. It will help you set these things up without having to write too much code. Its not as easy as Google wants us to think it is but once you get used to it is great.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.