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

fix(docs-infra): add cookie consent gtag event default state #54574

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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;
Expand All @@ -18,6 +19,7 @@ describe('AnalyticsService', () => {
let windowOnErrorHandler: (event: ErrorEvent) => void;

let mockWindow: any;
let mockLocalStorage = new MockLocalStorage();

beforeEach(() => {
gtagSpy = jasmine.createSpy('gtag');
Expand All @@ -39,6 +41,7 @@ describe('AnalyticsService', () => {
{provide: ENVIRONMENT, useValue: {}},
{provide: AnalyticsService, deps: [WINDOW]},
{provide: WINDOW, useFactory: () => mockWindow, deps: []},
{provide: LOCAL_STORAGE, useValue: mockLocalStorage},
],
});

Expand Down
18 changes: 17 additions & 1 deletion adev/src/app/core/services/analytics/analytics.service.ts
Expand Up @@ -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';

Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down