【问题标题】:Google Analytics event values not recorded from angular 8 project未从 Angular 8 项目记录的 Google Analytics 事件值
【发布时间】:2020-11-03 14:02:56
【问题描述】:

我正在使用 Google Analytics 来跟踪用户活动。但是,即使我从服务中触发事件,事件的值也会记录为零。

按照以下链接中的步骤操作:How To Properly Add Google Analytics Tracking to Your Angular Web App

我的代码:

googleanalytics.service.ts

import {Injectable} from '@angular/core';
declare let gtag:Function;


@Injectable({
  providedIn: 'root'
})
export class GoogleanalyticsService {

  constructor() { }

  public eventEmitter( 
    Eventname: string, 
    Eventcategory: string, 
    Eventaction: string, 
    Eventlabel: string = null,  
    Eventvalue: number = null ){ 
         gtag('event', Eventname, { 
                 Eventcategory: Eventcategory, 
                 Eventlabel: Eventlabel, 
                 Eventaction: Eventaction, 
                 Eventvalue: Eventvalue
               })
    }
public event2(
  
    Eventcategory: string, 
    Eventaction: string, 
    Eventlabel: string = null,  
    Eventvalue: number 
){
  gtag('send', {
    hitType: 'event',
    eventCategory: Eventcategory,
    eventAction: Eventaction,
    eventLabel: Eventlabel,
    Eventvalue: Eventvalue
  });
}
    public setGAUser(userid){
      gtag('set',{'user_id': userid}); // Set the user ID using signed-in user_id.})

    }
}

samplecomponent.ts:

this.GAService.eventEmitter('user_completed_form','user','click','user',30);

请提出一些解决方案。

【问题讨论】:

    标签: google-analytics angular8 google-analytics-4


    【解决方案1】:

    gtag 中的事件具有以下语法:

    gtag('event', <action>, {
      'event_category': <category>,
      'event_label': <label>,
      'value': <value>
    });
    

    您调用的第二个参数Eventname 实际上是action。尝试按照上面的构造,你会发现它可以正常工作。

    【讨论】:

    • 嗨,谢谢你的回答,它就像一个魅力。但我想知道,我们可以将字符串作为事件值吗?另外,我们可以在谷歌分析中生成一个唯一的用户 ID(我的字符串)吗?
    • 不行,事件值只能是正整数,否则为0。是度量,所以不能是字符串。
    • 用户ID呢?例如:gtag('set',{'user_id': userid});。即使我将用户 ID 作为我想要的变量发送,但在谷歌分析中,它显示的是自动生成的。
    • 请打开另一个答案,因此我们可以在必要时添加一些代码,因为问题是不同的主题,谢谢
    猜你喜欢
    • 1970-01-01
    • 2011-06-14
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    相关资源
    最近更新 更多