gpt4 book ai didi

angular - 谷歌 AdWords 与 Angular 5

转载 作者:行者123 更新时间:2023-12-01 22:26:20 24 4
gpt4 key购买 nike

我正在尝试在使用 Angular 5 构建的结帐 SPA 中使用 AdWords。我需要设置“google_conversion_value”等变量,然后运行位于//www.googleadservices.com/pagead/conversion.js 的脚本

但是有 3 个问题:

  1. 我无法使用文档中的内容,因为这是 SPA。
  2. 设置变量然后注入(inject)脚本会导致注入(inject)脚本时值为空
  3. 或者,首先注入(inject)脚本,然后使用 Promise 设置值,这样会在设置之前触发它。

2:

let googleAdWords = this.marketingService.googleAdWords();
if (googleAdWords != undefined) {
window.google_conversion_language = googleAdWords.google_conversion_language;
window.google_conversion_format = googleAdWords.google_conversion_format;
window.google_conversion_label = googleAdWords.google_conversion_label;
window.google_conversion_value = googleAdWords.google_conversion_value;
window.google_conversion_currency = googleAdWords.google_conversion_currency;
}
this.marketingService.loadScript('//www.googleadservices.com/pagead /conversion.js')

3:

this.marketingService.loadScript('//www.googleadservices.com/pagead/conversion.js').then(() => {
this.zone.run(() => {
let googleAdWords = this.marketingService.googleAdWords();
if (googleAdWords != undefined) {
window.google_conversion_language = googleAdWords.google_conversion_language;
window.google_conversion_format = googleAdWords.google_conversion_format;
window.google_conversion_label = googleAdWords.google_conversion_label;
window.google_conversion_value = googleAdWords.google_conversion_value;
window.google_conversion_currency = googleAdWords.google_conversion_currency;
}
});
});

--服务中的加载脚本--

loadScript(scriptUrl: string) {
return new Promise((resolve, reject) => {
const scriptElement = document.createElement('script');
scriptElement.src = scriptUrl;
scriptElement.onload = resolve;
document.body.appendChild(scriptElement);
});
}

最佳答案

我确信您已经解决了这个问题,但如果有人需要它,这对我有用。

第 1 步

将其放入您的 index.html 中,将 XXXXXXXXXXX 替换为您自己的实际转化 ID。

<!-- Global site tag (gtag.js) - Google Ads: XXXXXXXXXXX -->
script async src="https://www.googletagmanager.com/gtag/js?id=AW-XXXXXXXXXX">
</script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('js', new Date());
gtag('config', 'AW-XXXXXXXXXXX');
</script>

第 2 步

然后在您的结帐流程中,您要在其中注册交易:

const gtag = window['gtag'];
gtag('event', 'conversion', {
'send_to': 'AW-XXXXXXXXXX/CONVERSION_CODE',
'value': GRAND_TOTAL,
'currency': CURRENCY,
'transaction_id': TRANSACTION_ID
});

请记住填写 AW-XXXXXXXXXX/CONVERSION_CODEGRAND_TOTALCURRENCYTRANSACTION_ID 以及适当的值。上面的代码是typescript

关于angular - 谷歌 AdWords 与 Angular 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50356716/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com