@hakimio/ngx-google-analytics

中文文档
15.0.0发布于 2023/10/03MITTypeScriptES Module近 30 天下载 7260

Angular 应用程序的简单 Google 分析包

中文使用文档
文档目录13 节
  1. 设置
  2. 安装软件包
  3. 独立应用程序组件
  4. NG 模块
  5. 设置路由器提供者
  6. 高级路由器提供者设置
  7. 谷歌分析服务
  8. 注册分析事件
  9. 人工注册页面浏览
  10. 指令
  11. 简单的指令使用
  12. 使用时间 input 要素
  13. 指令组

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>

版本与发布记录

@hakimio/ngx-google-analytics 共发布 1 个版本,首次发布于 2023/10/03;过去 12 个月发布了 0 个版本。

版本发布日期距今
15.0.0latest2023/10/033 年前

关于 @hakimio/ngx-google-analytics 的常见问题

@hakimio/ngx-google-analytics 是什么?

Angular 应用程序的简单 Google 分析包

如何安装 @hakimio/ngx-google-analytics?

在项目目录运行 npm install @hakimio/ngx-google-analytics,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@angular/common(>=16.0.0)、@angular/core(>=16.0.0)、@angular/router(>=16.0.0)、rxjs(^7.4.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@hakimio/ngx-google-analytics 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@hakimio/ngx-google-analytics 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@hakimio/ngx-google-analytics 可以商用吗?

@hakimio/ngx-google-analytics 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@hakimio/ngx-google-analytics 的下载量有多少?

根据 npm 官方下载统计,@hakimio/ngx-google-analytics 在 2026/10/17 – 2026/11/15 这 30 天内被下载 7,260 次,日均约 242 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@hakimio/ngx-google-analytics 还在维护吗?

最新版本 15.0.0 发布于 2023/10/03(3 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/10/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。