最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google gtag_report_conversion: how can I use it without reloading my page? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Oct 31, 2018 at 11:51 humster_spbhumster_spb 1932 gold badges2 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

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.

发布评论

评论列表(0)

  1. 暂无评论