0

Using TrackForm and Identify together

I have a form on my website that should trigger both a track AND an identify call:

analytics.trackForm($('.viewing-form'), 'viewingRequested', {
        category: '{{ location.type }}',
        label: '{{ entry.title }}',
        location: '{{ location.title }}',
        reference: '{{ entry.reference }}'
    });

    $('.viewing-form').submit(function(){
      analytics.identify(analytics.user().anonymousId(), {
          name: $('#frmViewing .name').val(),
          email: $('#frmViewing .email').val(),
          phone: $('#frmViewing .phone').val()
      });
    });

Locally this works fine, but in production the identify call is occasionally dropped. I'm assuming this due to the form submitting before the identify call has finished.

Is there a technique to ensuring both calls complete without ending up in callback soup?

1reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
  • Hey Jon, and thanks for posting. It looks as if it may be simpler just to combine these events while tooling a manual timeout before page redirect. We typically suggest about 300 ms. The code would look something like this:

    <script>
          $('form').submit(function (e) {
            var form = this;
            e.preventDefault();
            analytics.track('viewingRequested');
            analytics.identify('userId');
            setTimeout(function () {
                form.submit();
            }, 300);
          });
    </script>

    Let us know if this helps!

    Reply Upvote