Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

  ga('create', 'UA-00000000-1', 'auto');
  ga('send', 'pageview'); 


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 – Simon Hayter Aug 4 '14 at 19:24
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 '14 at 23:37
@Binarysurf Maybe you could add that as an answer? – dan Aug 5 '14 at 22:11
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.


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


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.