Google gtag_report_conversion: how can I use it without reloading my page?

Viewed 7489

I have the form in the popup. I need to use this script:

function gtag_report_conversion(url) {
  var callback = function () {
    if (typeof(url) != 'undefined') {
        window.location = url;
    }
  };
  gtag('event', 'conversion', {
    'send_to': 'id_of_analytics',
    'event_callback': callback
  });
  return false;
}

And on submit I use:

onclick="return gtag_report_conversion('my_page_url')"

But when I click submit-button, my page get reloaded and popup with form get closed. And no error- or success-messages are displayed.

How can I use this script without reloading my page?

2 Answers

Change this line

  window.location = url;
to
// window.location = url;

Basically the callback function will forward you to the url you call with the gtag_report_conversion function. So if you just don't pass the url, then this check

if (typeof(url) != 'undefined') {
        window.location = url;
    }

will evaluate to false and the forwarding won't be triggered.

Related