Ngx 谷歌分析
在 Angular apps 中追踪 GA4 事件的简单方法.
@hakimio/ngx-google-analytics 是一个叉子 麦斯·安德里亚尼的 ngx-google-analytics。 。 。 .
指数
设置
安装软件包
npm install @hakimio/ngx-google-analytics
独立应用程序组件
如果您的应用程序组件正在使用独立的 API, 请遵循这些步骤来设置库 :
- 添加
provideGoogleAnalytics('ga4-tag-id')给你的应用程序的供应商。 如果您找不到您的 GA4 标记代号, 请看 这个 Google帮助页面.
main.ts
import {AppComponent} from './app/app.component';
import {bootstrapApplication} from '@angular/platform-browser';
import {ROUTES} from './app/app.routes';
import {provideGoogleAnalytics} from '@hakimio/ngx-google-analytics';
bootstrapApplication(AppComponent, {
providers: [
provideRouter(ROUTES),
provideAnimations(),
provideGoogleAnalytics('ga4-tag-id') // ⬅️ Google Analytics provider
]
}).catch(err => console.error(err));
您还可以使用第二个可选参数指定额外的设置 : provideGoogleAnalytics('ga4-tag-id', settings)。 。 。 .
见 IGoogle分析
界面,以获取更多关于可用设置的信息.
- 添加
NgxGoogleAnalyticsModule到您的应用组件的导入 :
app.component.ts
import {NgxGoogleAnalyticsModule} from '@hakimio/ngx-google-analytics';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css'],
standalone: true,
imports: [
NgxGoogleAnalyticsModule // ⬅️ Google Analytics module
]
})
export class AppComponent {}
NG 模块
如果你的申请是 NgModule 根据这些步骤建立图书馆:
- 添加
NgxGoogleAnalyticsModule到您的根应用程序模块( e)AppModule(中文(简体) )imports。 。 。 . - 添加
provideGoogleAnalytics('ga4-tag-id')在您的应用程序模块的提供者。 如果您找不到您的 GA4 标记代号, 请看 这个 Google帮助页面.
app.module.ts
import {NgxGoogleAnalyticsModule, provideGoogleAnalytics} from '@hakimio/ngx-google-analytics';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
NgxGoogleAnalyticsModule // ⬅️ Google Analytics module
],
providers: [
provideGoogleAnalytics('ga4-tag-id') // ⬅️ Google Analytics provider
],
bootstrap: [AppComponent]
})
export class AppModule {}
您还可以使用第二个可选参数指定额外的设置 : provideGoogleAnalytics('ga4-tag-id', settings)。 。 。 .
见 IGoogle分析
界面,以获取更多关于可用设置的信息.
设置路由器提供者
如果您正在使用 Angular 路由器并想要跟踪页面视图, 您可以包含 provideGoogleAnalyticsRouter() 在您的根应用程序提供者中.
关键是: provideGoogleAnalyticsRouter() 与 SSR 不相容,不应包含在服务器应用程序供应商中.
import {NgxGoogleAnalyticsModule, provideGoogleAnalytics, provideGoogleAnalyticsRouter} from '@hakimio/ngx-google-analytics';
@NgModule({
imports: [
// ...
NgxGoogleAnalyticsModule // ⬅️ Google Analytics module
],
providers: [
provideGoogleAnalytics('ga4-tag-id'),
provideGoogleAnalyticsRouter() // ⬅️ Google Analytics router provider
]
})
export class AppModule {}
高级路由器提供者设置
您可以通过路由选项将一些路径包含或排除到 provideGoogleAnalyticsRouter(options)。 。 。 .
支持以下路径匹配 :
- 简单路径匹配 :
{ include: [ '/full-uri-match' ] }· ; - 通关卡路径匹配 :
{ include: [ '*/public/*' ] }· ; - Regex 路径匹配 :
{ include: [ /^\/public\/.*/ ] }· ;
import {NgxGoogleAnalyticsModule, provideGoogleAnalytics, provideGoogleAnalyticsRouter} from '@hakimio/ngx-google-analytics';
@NgModule({
imports: [
// ...
NgxGoogleAnalyticsModule // ⬅️ Google Analytics module
],
providers: [
provideGoogleAnalytics('ga4-tag-id'),
provideGoogleAnalyticsRouter({ // ⬅️ Google Analytics router provider
include: ['/some-path'],
exclude: ['*/another/path/*']
})
]
})
export class AppModule {}
谷歌分析服务
该服务提供了强烈的打字方式 gtag() 命令。 除了类型检查以外,它没有
参数的任何其他验证或转换.
注册分析事件
@Component()
export class TestFormComponent {
private readonly gaService = inject(GoogleAnalyticsService);
onUserInputName() {
this.gaService.event('enter_name', {
category: 'user_register_form',
label: 'Name',
options: {
customDimension: 'foo_bar'
}
});
}
onUserInputEmail() {
this.gaService.event('enter_email', {
category: 'user_register_form',
label: 'Email'
});
}
onSubmit() {
this.gaService.event('submit', {
category: 'user_register_form',
label: 'Enviar'
});
}
}
人工注册页面浏览
@Component()
export class TestPageComponent implements OnInit {
private readonly gaService = inject(GoogleAnalyticsService);
ngOnInit() {
this.gaService.pageView('/test', {
title: 'Test the Title'
});
}
onUserLogin() {
this.gaService.pageView('/test', {
title: 'Test the Title',
options: {
user_id: 'my-user-id'
}
});
}
}
指令
指令提供了一种简单的登记分析事件的方法. 而不是手动使用 GoogleAnalyticsService, (中文(简体) ),
您可以简单地添加 ga* 属性到您的 html 元素.
简单的指令使用
默认情况下,指令调用 gtag() ,但您也可以通过提供 gaBind 属性.
关键是: 记住要导入 NgxGoogleAnalyticsModule 在您使用的所有独立组件和模块中 ga* 指令。 。 .
<div>
<button gaEvent="click_test" gaCategory="ga_directive_test">Click Test</button>
<button gaEvent="focus_test" gaCategory="ga_directive_test" gaBind="focus">Focus Test</button>
<button gaEvent="blur_test" gaCategory="ga_directive_test" gaBind="blur">Blur Test</button>
<button gaEvent="custom_test" gaCategory="ga_directive_test" gaBind="customEvent">Custom Event Test</button>
</div>
使用时间 input 要素
何时 gaEvent 指令用于形式元素, 默认 trigger 实值 focus 活动.
<div>
<input gaEvent="fill_blur" gaCategory="ga_directive_input_test" placeholder="Auto Blur Test"/>
</div>
指令组
这个 gaCategory 可针对更高层次的html元素组指定指令,为所有人指定相同的类别
儿童元素.
<div gaCategory="ga_test_category">
<button gaEvent gaAction="click_test">Click Test</button>
<button gaEvent gaAction="focus_test" gaBind="focus">Focus Test</button>
<button gaEvent gaAction="blur_test" gaBind="blur">Blur Test</button>
</div>