Skip to content

Commit

Permalink
fix(docs-infra): add cookie consent gtag event default state
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bencodezen committed Mar 8, 2024
1 parent b322079 commit 7ffc56b
Showing 1 changed file with 17 additions and 1 deletion.
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

0 comments on commit 7ffc56b

Please sign in to comment.