PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Analytics code in Confirmation message

  1. rochow
    Member

    Hey,

    On the advice of others in the forum - I have added google analytics virtual page view code to the confirmation message for an AJAX based form.

    The code ive included is

    <script type="text/javascript">
    _gaq.push(['_trackPageview', '/disaster-help/complete']);
    </script>

    however when i submit the form and view source - i don't see the code in the source - its as though its being stripped out. (other HTML codes like strong are not stripped from the actual confirmation message)

    Does anybody know why?

    Posted 13 years ago on Wednesday March 16, 2011 | Permalink
  2. It shouldn't be getting stripped out, however Javascript may get disrupted in the Confirmation Text field because it automatically formats text entered. This can cause problems when entering HTML because it will add BR tags for line breaks, etc. automatically.

    The final release of Gravity Forms v1.5 will have an option to disable the auto-formatting on the confirmation text. Once that is released as a final release (tentatively scheduled for Monday) you can see if that resolves this issue by selecting that option.

    If you need to implement this now you would have to use a custom confirmation page that you create and then set your Confirmation settings to redirect to that page and embed that script on that page.

    Posted 13 years ago on Wednesday March 16, 2011 | Permalink
  3. rochow
    Member

    ok great will wait for monday!

    Yeah the thing is its not being formatted but stripped all together it seems.

    Whe i inspect the element with firebut or google chroms i see the confirmation text but no sign of the javascript at all.

    Any reason you can think of for this?

    Posted 13 years ago on Thursday March 17, 2011 | Permalink
  4. None, not unless another plugin is causing a conflict. We don't strip code out of the Confirmation Text field and we haven't had other users reporting this specific issue.

    Posted 13 years ago on Thursday March 17, 2011 | Permalink
  5. The confirmation message loads in an iframe so you have to include the entire code or you will get a javascript error _gaq is not defined.

    The solution is to paste everything into the confirmation text field. If you put it all on one line in a text editor you don't have to worry about the line breaks. Copy and paste this, then change 'UA-01234567-1' and '/my-form/sent'

    <script type="text/javascript">var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-01234567-1']); _gaq.push(['_trackPageview', '/my-form/sent']); (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>

    Posted 13 years ago on Monday April 25, 2011 | Permalink
  6. @frankanthony That is only the case if your form uses AJAX. AJAX does use an iframe, if you turn off AJAX it does not. So something to keep in mind.

    Posted 13 years ago on Monday April 25, 2011 | Permalink
  7. enrise
    Member

    Hi,

    I used FrankAnthony method with ajax.
    But for some reason the script is triggered twice, I confirmed this with a 'console.log()'.
    Any idea how this works?

    Posted 13 years ago on Friday May 6, 2011 | Permalink
  8. Hi Enrise,

    I would recommend putting Frank's code inside a function that is bound to the gform_confirmation_loaded event. This even is fired when the confirmation page is displayed when submitted from an AJAX enabled event.

    Posted 13 years ago on Friday May 6, 2011 | Permalink
  9. @david can you give a more specific sample

    Posted 12 years ago on Sunday July 17, 2011 | Permalink
  10. I've been looking at this as needed to get GA click tracking sorted and have a solution after finding the issues mentioned by Enrise above with FrankAnthony's method firing twice. Turns out using the jQuery method hooking off of the gform_confirmation_loaded function has problems too as whilst it'll fire jQuery code and execute it, it reports errors loading jQuery anyway, something it's already done. Use Firebug to see what I mean and wrap either of the suggested functions above with a simple bit of jQuery as follows:

    [js]
    jQuery('#gforms_confirmation_message').css('background-color','red');

    So anyway, the solution, albeit a minor hack is to use normal JS but to prevent it being called twice. This is done by wrapping the GA code in an IF statement to check for the existence of jQuery which has the desired effect of only running once and not erroring and causing the GA code to fail. Any thought on other work rounds would be appreciated, wish this sort of tracking could be implemented in the core code though :)

    Code for those that might want to get this working though is as follows:

    [js]
    <script>
    
    if (typeof jQuery != 'undefined') {
    
    var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-XXXXXXX-X']);
      _gaq.push(['_setDomainName', 'none']); // Not needed if cross domain tracking not required
      _gaq.push(['_setAllowLinker', 'none']); // Not needed if cross domain tracking not required
      _gaq.push(['_trackPageview', '/conversion']);
    
      (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>
    Posted 12 years ago on Thursday August 25, 2011 | Permalink