首页 > 解决方案 > Google Analytics - Angular 2 - 事件包含未定义的值

问题描述

我尝试将自定义事件从 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"

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

[编辑]

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

在此处输入图像描述

这是 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

谢谢

标签: angulargoogle-analyticsgoogle-tag-manager

解决方案


这是解决方案:

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 标签......


推荐阅读