[英]How do I launch an onclick function and make it launch another function?
I need to launch an onclick function when someone clicks a button on my site (has the id of "rum_sst_tab") and then launch another function that counts a google conversion ( gtag_report_conversion()
)
我已經為 onclick 嘗試了一些 jQuery 選項,但它們對我不起作用。 這是一個 wordpress 站點,我正在嘗試打開 onclick 的 ID,位於 WordPress 插件中 - 不確定這是否會使事情復雜化。
document.getElementById("rum_sst_tab").onclick = gtag_report_conversion();
這是連接到 gtag function 的代碼的另一部分:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-58161224-5"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-72452xxxx');
</script>
我也在控制台中收到此錯誤:
(索引):1251 未捕獲類型錯誤:無法設置 null 的屬性“onclick”
如果您嘗試在解析之前訪問 DOM, getElementById()
將返回 null,因此請確保在此之后調用您的代碼。 否則添加點擊事件很簡單......
<!DOCTYPE html>
<html>
<head>
<title>so test</title>
</head>
<body>
<button type="button" id="test-div">click me!</button>
<script>
function testFunc() {
console.log('doing the test!');
}
document.getElementById('test-div').onclick = testFunc;
</script>
</body>
</html>
調用 function 的正確方法是:
document.getElementById('rum_sst_tab').onclick =
function(){
gtag_report_conversion();
}
(看: 使用 HTML 中的提交按鈕調用 function )
筆記:
建議您等待 9-10 小時后再檢查您的轉化率。 這是 Google Analytics 和 Google Ads 開始合作可能需要多長時間。
(在https://wpforms.com/how-to-track-form-submissions-as-google-adwords-conversions/的最終想法中)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.