英文:
Google Ads Conversion Tracking With React
问题
以下是您要翻译的内容:
"I'm attempting to add Google Ads conversion tracking within a React App. In this situation I need to set it up to trigger on click as opposed to page URL. Within the index.html I added the Google Tag and the Conversion Event Snippet without issues.
My Question: How do I now call the function on a React Component to trigger a conversion?
Event Snippet:
"
英文:
I'm attempting to add Google Ads conversion tracking within a React App. In this situation I need to set it up to trigger on click as opposed to page URL. Within the index.html I added the Google Tag and the Conversion Event Snippet without issues.
My Question: How do I now call the function on a React Component to trigger a conversion?
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-______"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'AW-______');
</script>
Event Snippet:
<!-- Event snippet for Payment conversion page
In your html page, add the snippet and call gtag_report_conversion when someone clicks on the chosen link or button. -->
<script>
function gtag_report_conversion(url) {
var callback = function () {
if (typeof(url) != 'undefined') {
window.location = url;
}
};
gtag('event', 'conversion', {
'send_to': 'AW-_____/_____',
'transaction_id': '',
'event_callback': callback
});
return false;
}
</script>
答案1
得分: 0
在React组件中,您可以通过以下方式在单击时使用window.gtag
触发Google Ads转化。
第一个AW-________号码来自您的Google标签。
第二个AW-_______/________号码来自您的事件片段。
const clickedButton = () => {
// 这将触发转化
window.gtag('config', 'AW-_______');
window.gtag('event', 'conversion', {'send_to': 'AW-______/____________'});
}
英文:
Within a React component you can trigger a Google Ads conversion on click by using window.gtag
as shown below.
The first AW-________ number is from your Google Tag.
The second AW-_______/________ number is from your Event Snippet.
const clickedButton = () => {
//This will trigger the conversion
window.gtag('config', 'AW-_______');
window.gtag('event', 'conversion', {'send_to': 'AW-______/____________'});
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论