humster_spb
humster_spb

Reputation: 193

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

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?

Upvotes: 8

Views: 12948

Answers (2)

Gh05d
Gh05d

Reputation: 8962

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.

Upvotes: 4

user138821
user138821

Reputation: 444

Change this line

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

Upvotes: 10

Related Questions