@altack/ngx-onetrust

中文文档
1.5.0发布于 2022/12/23MITTypeScriptES Module近 30 天下载 1716

OneTrust loader & wrapper for Angular

原始使用文档

OneTrust for Angular

OneTrust loader & wrapper for Angular

How to use?

  1. Install the package
    npm i @altack/ngx-onetrust

    Notice that this library will not add any OneTrust related dependency, OneTrust scripts will be loaded at runtime.

  2. Import the OneTrustModule.forRoot() in your AppModule.

    A configuration object with your domainScriptID and cookiesGroups needs to be set as module configuration. CookiesGroups usually looks as follows but their IDs (C0001,C0002, etc. ) might be different.

        OneTrustModule.forRoot({
          cookiesGroups: {
            StrictlyNecessaryCookies: 'C0001',
            PerformanceCookies: 'C0002',
            FunctionalCookies: 'C0003',
            TargetingCookies: 'C0004',
            SocialMediaCookies: 'C0005'
          },
          domainScript: 'your-domain-script-id'
        })
    

    Visit your https://app.cookiepro.com/cookies/categorizations?tab=Categories to define the CookiesGroups object properly.

  3. Now you can inject the OneTrustService (usually during the APP_INITIALIZER) to load the OneTrust scripts, change the banner language in realtime and listen for consent changes. Let's have a look these 3 method in detail.

    • oneTrustService.loadOneTrust() accepts an optional parameter with your domainScriptID which will take over the one defined at the module level. Calling this method will init the download and initialization of the OneTrust script.

    • oneTrustService.translateBanner(lang) will try to translate the cookie banner using the lang parameter (e.g en, es, pl, etc.). It works by calling the OneTrust.changeLanguage() method from the OneTrust object of the browser. In order for this to work, you'll have to define the translation in the OneTrust administration panel (usually in the Templates section). Based on the country the user is, translateBanner() will attempt to create a valid locale (e.g en-US) by combining the language + the country. You can also pass a second argument true to the oneTrustService.translateBanner('en', true) in order to force a language change without trying to build the locale.

    • oneTrustService.consentChanged$() returns an Observable<Map<CookiesGroups, boolean>>. Basically a Map with the permission the user has consent. Useful for running certain logic based on the user selection. Heads up, by default (while the cookie banner is visible and pristine) this Map will contain only StrictlyNecessaryCookies permission.

    • oneTrustService.oneTrustInstance$() returns an Observable<OneTrust>. An instance of the OneTrust object after it has been loaded.

Side notes

A common use case for loading OneTrust is by calling oneTrustService.loadOneTrust() within the APP_INITIALIZER.

    {
      provide: APP_INITIALIZER,
      useFactory: appInitializer,
      deps: [MyInitService],
      multi: true
    }
export function appInitializer(myInitService: MyInitService): Function {
  return () => myInitService.load();
}
@Injectable({ providedIn: 'root' })
export class MyInitService {

  constructor(
    ...
    private oneTrust: OneTrustService,
    ...
  ) {}

  public load(): Promise<any> {
  this.oneTrust.loadOneTrust();
  ...
  }
}

Angular Compatibility

If you're using Angular 13 make sure to use 1.3.x versions

版本与发布记录

@altack/ngx-onetrust 共发布 8 个版本,首次发布于 2021/05/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.5.0latest2022/12/233 年前
1.3.12022/03/294 年前
1.3.02022/02/044 年前
0.0.52021/05/265 年前
0.0.42021/05/265 年前
0.0.32021/05/255 年前

关于 @altack/ngx-onetrust 的常见问题

@altack/ngx-onetrust 是什么?

OneTrust loader & wrapper for Angular

如何安装 @altack/ngx-onetrust?

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

@altack/ngx-onetrust 支持 TypeScript 吗?

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

@altack/ngx-onetrust 支持 ESM 还是 CommonJS?

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

@altack/ngx-onetrust 可以商用吗?

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

@altack/ngx-onetrust 的下载量有多少?

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

@altack/ngx-onetrust 还在维护吗?

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