GVKun编程网logo

将 gtag 脚本添加到外部 JavaScript 文件(gtr脚本执行流程)

4

本文将带您了解关于将gtag脚本添加到外部JavaScript文件的新内容,同时我们还将为您解释gtr脚本执行流程的相关知识,另外,我们还将为您提供关于@types/gtag.js错误:“config

本文将带您了解关于将 gtag 脚本添加到外部 JavaScript 文件的新内容,同时我们还将为您解释gtr脚本执行流程的相关知识,另外,我们还将为您提供关于@types/gtag.js 错误:“config”类型的参数不能分配给“consent”类型的参数、Adobe 发布:GTAG 活动view_item 等、Angular 无法识别 index.html 中的 Google gtag、Google Ads gtag 跟踪代码不起作用,但 analytics.js 在同一页面上运行的实用信息。

本文目录一览:

将 gtag 脚本添加到外部 JavaScript 文件(gtr脚本执行流程)

将 gtag 脚本添加到外部 JavaScript 文件(gtr脚本执行流程)

如何解决将 gtag 脚本添加到外部 JavaScript 文件

我已将以下代码添加到我的网页以将事件信息发送到 Google Analytics:

  1. <head>
  2. <!-- Global site tag (gtag.js) - Google Analytics -->
  3. <script async src="https://www.googletagmanager.com/gtag/js?id=UA-144340016-1"></script>
  4. <!-- Google Analytics -->
  5. <script>
  6. window.dataLayer = window.dataLayer || [];
  7. function gtag() { dataLayer.push(arguments); }
  8. gtag(''js'',new Date());
  9. gtag(''config'',''UA-144340016-1'');
  10. </script>
  11. <!-- End Google Analytics -->
  12. </head>
  13. <body>
  14. <a id=''lin1'' href="cool.com" onclick="gtag(''event'',''click'',{''event_category'': ''cat1'',''event_label'': ''lab1''});">
  15. </a>
  16. </body

现在我不想在 html 上使用内联 onclick 事件,我想把这个位移动到一个单独的 JavaScript 代码,所以像这样:

my.js

  1. $(''#lin1'').click(function() {
  2. gtag(''event'',''event_label'': ''lab1''});
  3. });

然后在我的网页底部添加 my.js。这种方法有什么问题吗?我看到的所有文档都将 gtag 事件直接添加到 HTML 中,这就是为什么我想确保我正在做的事情是否没有缺点

解决方法

具有将点击发送到 Analytics 的函数的函数库没有问题。最终,文档建议将代码段 (gtag.js) 直接插入 HTML 的 head 中(但这看起来您已经这样做了)。

@types/gtag.js 错误:“config”类型的参数不能分配给“consent”类型的参数

@types/gtag.js 错误:“config”类型的参数不能分配给“consent”类型的参数

如何解决@types/gtag.js 错误:“config”类型的参数不能分配给“consent”类型的参数

我正在尝试将 Google Analytics 添加到 Next.js 站点。我正在遵循 JavaScript 中的 this 指南,但我的应用程序是 TypeScript。安装@types/gtag.js 后,出现此错误:

No overload matches this call.
  The last overload gave the following error.
    Argument of type ''"config"'' is not assignable to parameter of type ''"consent"''.ts(2769)
index.d.ts(19,5): The last overload is declared here.

在我的这一行代码中:

window.gtag(''config'',process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS,{
    page_path: url,})

这里是类型定义:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/gtag.js/index.d.ts

我不确定类型是否不正确,但代码大概可以工作,因为它直接来自教程。

我的临时解决方法是声明 gtag: any

declare global {
  interface Window {
    gtag: any
  }
}

解决方法

我遇到了这个错误,我通过将 string 类型添加到 NEXT_PUBLIC_GOOGLE_ANALYTICS 来解决这个问题(在你的情况下)。就我而言,因为 process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS 的类型是 string | undefined

Adobe 发布:GTAG 活动view_item 等

Adobe 发布:GTAG 活动view_item 等

如何解决Adobe 发布:GTAG 活动view_item 等

我在 Adob​​e Launch(自定义代码标签类型)中为 Google Ads 设置 gtag ecom 事件。 基本上,我将事件数据添加到 dataLayer (gtag(''event''...)) 并且没有任何反应,至少在网络选项卡中,但是我确定必须有发送事件数据的请求。

在使用 Adob​​e Launch 之前,我从未这样做过。 在增强型经济事件的 GTM 中,您必须添加带有触发器的标签。所以我认为由于事件数据确实被添加到 dataLayer,我必须弄清楚如何“手动”发送这些数据(没有 gtm)。

gtag(''js'',new Date());
gtag(''config'',''AW-XXXXXXX'');
gtag(''event'',conf[''type''],{
   ''value'': conf[''price''],''items'': [
       {
           ''id'': conf[''products''],''google_business_vertical'': ''retail''
       }]
});

数据被添加到 dataLayer 就好了:

dataLayer screenshot

有什么想法吗?

Angular 无法识别 index.html 中的 Google gtag

Angular 无法识别 index.html 中的 Google gtag

如何解决Angular 无法识别 index.html 中的 Google gtag

按照 google 的指示,在 head 部分的 index.html 中添加了 gtag。但是,我遇到了错误。

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <base href="/">
  5. <Meta charset="utf-8">
  6. <Meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  7. <title>Hello</title>
  8. <!-- Favicon -->
  9. <link href="./assets/img/favicon.ico" rel="icon" type="image/png">
  10. <!-- Fonts -->
  11. <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
  12. <!-- Google Tag Manager -->
  13. <script>(function (w,d,s,l,i) {
  14. w[l] = w[l] || []; w[l].push({
  15. ''gtm.start'':
  16. new Date().getTime(),event: ''gtm.js''
  17. }); var f = d.getElementsByTagName(s)[0],j = d.createElement(s),dl = l != ''dataLayer'' ? ''&l='' + l : ''''; j.async = true; j.src =
  18. ''https://www.googletagmanager.com/gtm.js?id='' + i + dl; f.parentNode.insertBefore(j,f);
  19. })(window,document,''script'',''dataLayer'',''GTM-01jd9k'');</script>
  20. <!-- End Google Tag Manager -->
  21. </head>
  22. <body>
  23. <app-root></app-root>
  24. </body>
  25. </html>

看控制台,

(索引):30 GET https://www.googletagmanager.com/gtm.js?id=GT....。净::ERR_ABORTED 404

似乎无法识别 insertBefore()

Failed to load resource: the server responded with a status of 404 ()

enter image description here

angular 如何识别 index.html 中的 gtag?任何帮助表示赞赏!

解决方法

使用 angular-google-tag-manager 谷歌标签管理器的 npm 包。我已经用过了。这是一个很棒的包。

Google Ads gtag 跟踪代码不起作用,但 analytics.js 在同一页面上运行

Google Ads gtag 跟踪代码不起作用,但 analytics.js 在同一页面上运行

如何解决Google Ads gtag 跟踪代码不起作用,但 analytics.js 在同一页面上运行

请参阅此最小示例

我有一个像这样的静态 HTML 页面

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <Meta charset="UTF-8" />
  5. <Meta name="viewport" content="width=device-width,initial-scale=1.0" />
  6. <Meta http-equiv="X-UA-Compatible" content="ie=edge" />
  7. <title>New User</title>
  8. </head>
  9. <body>
  10. <!-- Google Analytics Code -->
  11. <script>
  12. (function (i,s,o,g,r,a,m) {
  13. i["GoogleAnalyticsObject"] = r;
  14. (i[r] =
  15. i[r] ||
  16. function () {
  17. (i[r].q = i[r].q || []).push(arguments);
  18. }),(i[r].l = 1 * new Date());
  19. (a = s.createElement(o)),(m = s.getElementsByTagName(o)[0]);
  20. a.async = 1;
  21. a.src = g;
  22. m.parentNode.insertBefore(a,m);
  23. })(
  24. window,document,"script","https://www.google-analytics.com/analytics.js","ga"
  25. );
  26. ga("create","MY_UA","auto");
  27. ga("send","pageview");
  28. ga("send",{
  29. hitType: "event",eventCategory: "livechatNewUser",eventAction: "redirect",});
  30. </script>
  31. <!-- Global site tag (gtag.js) -->
  32. <script src="https://www.googletagmanager.com/gtag/js?id=MY_AdWords"></script>
  33. <script>
  34. window.dataLayer = window.dataLayer || [];
  35. function gtag() {
  36. dataLayer.push(arguments);
  37. }
  38. gtag("js",new Date());
  39. gtag("config","MY_AdWords");
  40. gtag("event","conversion",{
  41. send_to: "MY_AdWords/MY_Label",});
  42. </script>
  43. <script language="javascript">
  44. setTimeout(function () {
  45. document.location.href = "/dashboard";
  46. },1000);
  47. </script>
  48. </body>
  49. </html>

3 天后,我的 ga 后端正在接收一些数据

enter image description here

但是,我的广告词后端没有收到任何数据

enter image description here

日期范围相同,为什么会这样?

解决方法

您使用的是 Google Analytics analytics.js 实现,而不是 gtag 实现。不过,这很好,您使用 gtag 方法调用 AdWords 转化事件。但是没有 gtag。

请看这里:https://support.google.com/google-ads/answer/6331314?hl=en#zippy=%2Cset-up-conversion-tracking-using-javascript%2Cset-up-conversion-tracking-using-the-global-site-tag

我建议将您的分析实现切换为 GA 的 gtag 实现,以便您可以同时使用两者。

今天关于将 gtag 脚本添加到外部 JavaScript 文件gtr脚本执行流程的讲解已经结束,谢谢您的阅读,如果想了解更多关于@types/gtag.js 错误:“config”类型的参数不能分配给“consent”类型的参数、Adobe 发布:GTAG 活动view_item 等、Angular 无法识别 index.html 中的 Google gtag、Google Ads gtag 跟踪代码不起作用,但 analytics.js 在同一页面上运行的相关知识,请在本站搜索。

本文标签: