【问题标题】:Google Analytics - Angular 2 - events contain undefined valuesGoogle Analytics - Angular 2 - 事件包含未定义的值
【发布时间】:2018-09-10 13:39:36
【问题描述】:

我尝试将自定义事件从 Angular 2 应用程序发送到谷歌分析

这里是代码

ga('send', 'event', {
    eventCategory: eventCategory,
    eventLabel: eventLabel,
    eventAction: eventAction,
    eventValue: eventValue
});

我是这样称呼它的:

this.googleAnalyticsService.sendEvent("testCategory", "testAction", "testLabel", 10);

查看实时视图,在“事件”选项卡中,我看到所有事件“事件类别”和“事件操作”都是“未定义”

我尝试在跟踪代码管理器中编辑“Google Analytics”标签并设置

"category" field to "eventCategory"
"label" field to "eventLabel"
"action" field to "eventAction"
"value" field to "eventValue"

但是没有运气,有人可以帮助我吗?

[编辑]

除非我在标签本身中设置值,否则事件实际上根本不会触发 但是如何获取从我的代码发送的实际值???

enter image description here

这里是 GA 调试器日志: 显然 GA 发送了一个带有“未定义”字段的事件

我自己的ga.send也发送了,所以不知道为什么这个没有触发

Initializing Google Analytics.
analytics_debug.js:10 Running command: ga("create", "UA-********-3", {name: "gtm4"})
analytics_debug.js:10 Creating new tracker: gtm4
analytics_debug.js:10 Running command: ga("gtm4.set", "&gtm", "G9452W68FS")
analytics_debug.js:10 Running command: ga("gtm4.set", "nonInteraction", false)
analytics_debug.js:10 Running command: ga("gtm4.set", "hitCallback", [function])
analytics_debug.js:10 Running command: ga("gtm4.send", {hitType: "event", eventCategory: "undefined", eventAction: "undefined", eventLabel: undefined, eventValue: undefined})
analytics_debug.js:10 Setting throttling cookie: "_gat_UA-********-3"
analytics_debug.js:10 
Sent beacon:
v=1&_v=j68d&a=708558753&t=event&ni=0&_s=1&dl=https%3A%2F%2F*******************%2Fa%2Fgifts%3Fs%3D**************************************%26pid%3D14%26m%3D0%26categories%3D%26p%3D0&ul=fr-fr&de=UTF-8&sd=24-bit&sr=1920x1080&vp=1903x943&je=0&ec=undefined&ea=undefined&_u=QAGAAAAB~&jid=1225520003&gjid=48267034&cid=804438810.1535016930&tid=UA-********-3&_gid=121630742.1536576429&_r=1&gtm=G9452W68FS&z=461818314

analytics_debug.js:10 <unknown>        (&gtm)  G9452W68FS
analytics_debug.js:10 _j1              (&jid)  1225520003
analytics_debug.js:10 _j2              (&gjid) 48267034
analytics_debug.js:10 adSenseId        (&a)    708558753
analytics_debug.js:10 apiVersion       (&v)    1
analytics_debug.js:10 clientId         (&cid)  804438810.1535016930
analytics_debug.js:10 encoding         (&de)   UTF-8
analytics_debug.js:10 eventAction      (&ea)   undefined
analytics_debug.js:10 eventCategory    (&ec)   undefined
analytics_debug.js:10 hitType          (&t)    event
analytics_debug.js:10 javaEnabled      (&je)   0
analytics_debug.js:10 language         (&ul)   fr-fr
analytics_debug.js:10 location         (&dl)   https://*******************/a/gifts?s=**************************************&pid=14&m=0&categories=&p=0
analytics_debug.js:10 nonInteraction   (&ni)   0
analytics_debug.js:10 screenColors     (&sd)   24-bit
analytics_debug.js:10 screenResolution (&sr)   1920x1080
analytics_debug.js:10 trackingId       (&tid)  UA-********-3
analytics_debug.js:10 viewportSize     (&vp)   1903x943
analytics_debug.js:10 Running command: ga("set", "page", "/a/gifts?s=**************************************&pid=14&m=0&categories=&p=0")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("send", "pageview")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("set", "page", "/a/gifts?s=**************************************&pid=14&m=0&categories=&p=0")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("send", "pageview")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("set", "page", "/a/gifts?s=**************************************&pid=14&m=0&categories=&p=0")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("send", "pageview")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("set", "page", "/a/gifts?s=**************************************&pid=14&m=0&categories=&p=0")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("send", "pageview")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("set", "page", "/a/gifts?s=**************************************&pid=14&m=0&categories=&p=0")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("send", "pageview")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("set", "page", "/a/gifts?s=**************************************&pid=14&m=0&categories=&p=0")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Running command: ga("send", "pageview")
analytics_debug.js:10 Command ignored. Unknown target: undefined
analytics_debug.js:10 Registered new plugin: ga(provide, "render", Function)
main.79214c40a0c056e8e45c.106.32125.bundle.js:1 sending to ga Product Details Page Page Displayed Sports & Loisirs 14
analytics_debug.js:10 Executing Google Analytics commands.
analytics_debug.js:10 Running command: ga("send", "event", {eventCategory: "Product Details Page", eventLabel: "Sports & Loisirs", eventAction: "Page Displayed", eventValue: 14})
analytics_debug.js:10 Command ignored. Unknown target: undefined

谢谢

【问题讨论】:

  • 看起来您引用的变量在您的 ga 事件的上下文中未定义。您需要让它们可用才能被取走。
  • 我需要得到“gtm4”。跟踪器名称,​​但我使用时未定义 getAll:let tracker = ga.getAll()[0];

标签: angular google-analytics google-tag-manager


【解决方案1】:

解决办法如下:

import {Injectable} from "@angular/core";
declare let ga: any;

@Injectable()
export class GoogleAnalyticsService {

    public sendEvent(eventCategory: string,eventAction: string,eventLabel: string = null,eventValue: number = null) {
        if(ga && ga.getAll)
        {
            console.log('sending to ga '+eventCategory+' '+eventAction+' '+eventLabel+' '+eventValue);
            let tracker = ga.getAll()[0];
            tracker.send('event', {
                eventCategory: eventCategory,
                eventLabel: eventLabel,
                eventAction: eventAction,
                eventValue: eventValue,
                /*hitCallback : function () {
                  alert("Event received");
                }*/         
            });
        }
        else
            console.warn("GA not ready yet");
    }

    public sendPageView(url:string)
    {
        if(ga && ga.getAll)
        {
            let tracker = ga.getAll()[0];
            tracker.set('page', url);
            tracker.send('pageview');
        }
    }
}

只有在你的 DOM 准备好时才应该注意调用这些函数 (ngAfterViewInit)

对于“未定义”的值,它们实际上来自 GA 标签本身,显然它正在自动触发一个事件

所以我还是不明白为什么同一个网站会有多个 GA 标签...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多