mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
fix(docs-infra): add cookie consent gtag event default state (#54574)
This PR sets a default state for cookie consent of 'denied'. The other part of the PR exists in the angular/dev-infra repo which will grant permission when the user accepts the cookie banner. PR Close #54574
This commit is contained in:
parent
90389add7a
commit
0b53fdb3b4
2 changed files with 21 additions and 2 deletions
|
|
@ -7,8 +7,9 @@
|
|||
*/
|
||||
|
||||
import {Injector} from '@angular/core';
|
||||
import {ENVIRONMENT, WINDOW} from '@angular/docs';
|
||||
import {ENVIRONMENT, WINDOW, LOCAL_STORAGE} from '@angular/docs';
|
||||
import {AnalyticsService} from './analytics.service';
|
||||
import {MockLocalStorage} from '@angular/docs/testing';
|
||||
|
||||
describe('AnalyticsService', () => {
|
||||
let service: AnalyticsService;
|
||||
|
|
@ -18,6 +19,7 @@ describe('AnalyticsService', () => {
|
|||
let windowOnErrorHandler: (event: ErrorEvent) => void;
|
||||
|
||||
let mockWindow: any;
|
||||
let mockLocalStorage = new MockLocalStorage();
|
||||
|
||||
beforeEach(() => {
|
||||
gtagSpy = jasmine.createSpy('gtag');
|
||||
|
|
@ -39,6 +41,7 @@ describe('AnalyticsService', () => {
|
|||
{provide: ENVIRONMENT, useValue: {}},
|
||||
{provide: AnalyticsService, deps: [WINDOW]},
|
||||
{provide: WINDOW, useFactory: () => mockWindow, deps: []},
|
||||
{provide: LOCAL_STORAGE, useValue: mockLocalStorage},
|
||||
],
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
|
||||
import {WINDOW, ENVIRONMENT} from '@angular/docs';
|
||||
import {WINDOW, ENVIRONMENT, LOCAL_STORAGE, STORAGE_KEY, setCookieConsent} from '@angular/docs';
|
||||
|
||||
import {formatErrorEventForAnalytics} from './analytics-format-error';
|
||||
|
||||
|
|
@ -28,6 +28,7 @@ interface WindowWithAnalytics extends Window {
|
|||
export class AnalyticsService {
|
||||
private environment = inject(ENVIRONMENT);
|
||||
private window: WindowWithAnalytics = inject(WINDOW);
|
||||
private readonly localStorage = inject(LOCAL_STORAGE);
|
||||
|
||||
constructor() {
|
||||
this._installGlobalSiteTag();
|
||||
|
|
@ -64,6 +65,21 @@ export class AnalyticsService {
|
|||
window.gtag = function () {
|
||||
window.dataLayer?.push(arguments);
|
||||
};
|
||||
|
||||
// Cookie banner consent initial state
|
||||
// This code is modified in the @angular/docs package in the cookie-popup component.
|
||||
// Docs: https://developers.google.com/tag-platform/security/guides/consent
|
||||
if (this.localStorage) {
|
||||
if (this.localStorage.getItem(STORAGE_KEY) === 'true') {
|
||||
setCookieConsent('granted');
|
||||
} else {
|
||||
setCookieConsent('denied');
|
||||
}
|
||||
} else {
|
||||
// In case localStorage is not available, we default to denying cookies.
|
||||
setCookieConsent('denied');
|
||||
}
|
||||
|
||||
window.gtag('js', new Date());
|
||||
|
||||
// Configure properties before loading the script. This is necessary to avoid
|
||||
|
|
|
|||
Loading…
Reference in a new issue