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 27, 2024
1 parent 5bd188a commit ffb3422
Show file tree
Hide file tree
Showing 3 changed files with 783 additions and 751 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -153,7 +153,7 @@
"@angular-devkit/architect-cli": "^0.1703.0-rc",
"@angular/animations": "^17.3.0-rc",
"@angular/build-tooling": "https://github.com/angular/dev-infra-private-build-tooling-builds.git#65d002a534a74daa3c9bd26bdea5a092cbd519df",
"@angular/docs": "https://github.com/angular/dev-infra-private-docs-builds.git#82c4573f5c9d4fb864271a1c74259fc251457e2f",
"@angular/docs": "https://github.com/angular/dev-infra-private-docs-builds.git#ea7f2bc177445d0420103f3e7611d5d40793e8c7",
"@angular/ng-dev": "https://github.com/angular/dev-infra-private-ng-dev-builds.git#7dea535110c0215b221908e37067ee6b605db373",
"@babel/helper-remap-async-to-generator": "^7.18.9",
"@babel/plugin-proposal-async-generator-functions": "^7.20.7",
Expand Down

0 comments on commit ffb3422

Please sign in to comment.