Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the usage of the angular nonce token #194

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ npm i --save angular-google-tag-manager

### Custom configuration and GTM environments

You can pass _gtm_preview_ and _gtm_auth_ optional variables to your GTM by providing them in app.module.ts
You can provide the nonce through the Angular [`CSP_NONCE`](https://angular.io/guide/security#content-security-policy) token or by providing the `googleTagManagerCSPNonce` token.
You can pass _gtm_preview_ and _gtm_auth_ optional variables to your GTM by providing them in app.module.ts.

```
providers: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Inject, Injectable, Optional } from '@angular/core';
import { CSP_NONCE, Inject, Injectable, Optional, inject } from '@angular/core';
import { GoogleTagManagerConfiguration } from './angular-google-tag-manager-config.service';
import { GoogleTagManagerConfig } from './google-tag-manager-config';

Expand Down Expand Up @@ -37,6 +37,10 @@ export class GoogleTagManagerService {
@Inject('googleTagManagerCSPNonce')
public googleTagManagerCSPNonce: string
) {
const ngCspNonce = inject(CSP_NONCE);
if (!googleTagManagerCSPNonce && ngCspNonce){
this.googleTagManagerCSPNonce = ngCspNonce;
}
this.config = this.googleTagManagerConfiguration?.get();
if (this.config == null) {
this.config = { id: null };
Expand Down
3 changes: 2 additions & 1 deletion projects/demo-application/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const routes: Routes = [
})
],
providers: [
{ provide: 'googleTagManagerCSPNonce', useValue: 'CSP-NONCE' },
// You can provide the nonce automically by providing the value through Angular. See: https://angular.io/guide/security#content-security-policy
// { provide: 'googleTagManagerCSPNonce', useValue: 'CSP-NONCE' },
// OLD PROVIDER VERSION
// { provide: 'googleTagManagerId', useValue: 'GTM-PV8586C' }
],
Expand Down
2 changes: 1 addition & 1 deletion projects/demo-application/src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
<app-root ngCspNonce="randomNonceGoesHere"></app-root>
</body>
</html>