0

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

遵循以下链接中的步骤:如何正确地将 Google Analytics 跟踪添加到您的 Angular Web 应用程序

来自 Google Analytics 的事件图像

我的代码:

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.})

    }
}

示例组件.ts:

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

请提出一些解决方案。

4

1 回答 1

1

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

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

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

于 2020-11-03T20:13:59.973 回答