|
1 | | -import { Component, Inject, HostListener, OnDestroy, AfterViewInit } from '@angular/core'; |
2 | | -import { SeoService } from "@valor-software/common-docs"; |
| 1 | +import { AfterViewInit, Component, HostListener, OnDestroy } from '@angular/core'; |
| 2 | +import { SeoService } from '@valor-software/common-docs'; |
3 | 3 | import { NavigationEnd, Router, RoutesRecognized } from '@angular/router'; |
4 | | -import { DOCUMENT } from '@angular/common'; |
5 | | -import { filter, pairwise } from "rxjs/operators"; |
6 | | -import { Subscription } from "rxjs"; |
| 4 | +import { filter, pairwise } from 'rxjs/operators'; |
| 5 | +import { BehaviorSubject, Subscription } from 'rxjs'; |
| 6 | +import { CookieService } from 'ngx-cookie-service'; |
7 | 7 |
|
8 | 8 | const notFoundPageUrl = '/404'; |
9 | 9 | declare const gtag: any; |
| 10 | +const isCookieAllowed = 'isCookieAllowed'; |
10 | 11 |
|
11 | 12 | @Component({ |
12 | | - selector: 'valor-software-site-base-root', |
13 | | - templateUrl: './app.component.html', |
14 | | - styleUrls: ['./app.component.scss'], |
| 13 | + selector: 'valor-software-site-base-root', |
| 14 | + templateUrl: './app.component.html', |
| 15 | + styleUrls: ['./app.component.scss'], |
15 | 16 | }) |
16 | 17 | export class AppComponent implements OnDestroy, AfterViewInit { |
17 | | - title = 'valor-software-site'; |
18 | | - scrollPosition = 0; |
19 | | - $routeEvents?: Subscription; |
20 | | - $gaRouteEvents?: Subscription; |
21 | | - $routerEventNavigationEnd: Subscription; |
22 | | - showFooterAndHeader = true; |
| 18 | + title = 'valor-software-site'; |
| 19 | + scrollPosition = 0; |
| 20 | + $routeEvents?: Subscription; |
| 21 | + $gaRouteEvents?: Subscription; |
| 22 | + $routerEventNavigationEnd: Subscription; |
| 23 | + showFooterAndHeader = true; |
| 24 | + showCookieConsentBanner = new BehaviorSubject(!this.cookieService.get(isCookieAllowed)); |
23 | 25 |
|
24 | | - @HostListener('window:beforeunload') private onReload() { |
25 | | - window.sessionStorage.setItem('scrollPosition', window.scrollY.toString()); |
26 | | - } |
| 26 | + @HostListener('window:beforeunload') |
| 27 | + private onReload() { |
| 28 | + window.sessionStorage.setItem('scrollPosition', window.scrollY.toString()); |
| 29 | + } |
27 | 30 |
|
28 | | - constructor( |
29 | | - private seo: SeoService, |
30 | | - private router: Router, |
31 | | - @Inject(DOCUMENT) private document: any |
32 | | - ) { |
33 | | - const scrollItem = window.sessionStorage.getItem('scrollPosition'); |
34 | | - this.scrollPosition = scrollItem || scrollItem == '0' ? Number(scrollItem) : 0; |
35 | | - this.$routerEventNavigationEnd = router.events.pipe( |
36 | | - filter(evt => evt instanceof NavigationEnd) |
37 | | - ).subscribe(res => { |
38 | | - this.showFooterAndHeader = !!(this.router.url === notFoundPageUrl); |
39 | | - }); |
40 | | - } |
| 31 | + constructor( |
| 32 | + private seo: SeoService, |
| 33 | + private router: Router, |
| 34 | + private readonly cookieService: CookieService, |
| 35 | + ) { |
| 36 | + const scrollItem = window.sessionStorage.getItem('scrollPosition'); |
| 37 | + this.scrollPosition = scrollItem || scrollItem == '0' ? Number(scrollItem) : 0; |
| 38 | + this.$routerEventNavigationEnd = router.events.pipe( |
| 39 | + filter(evt => evt instanceof NavigationEnd) |
| 40 | + ).subscribe(res => { |
| 41 | + this.showFooterAndHeader = !!(this.router.url === notFoundPageUrl); |
| 42 | + }); |
| 43 | + } |
41 | 44 |
|
42 | | - scrollToPosition(value: number) { |
43 | | - setTimeout(() => { |
44 | | - window.scrollTo(0, value); |
45 | | - window.sessionStorage.removeItem('scrollPosition'); |
46 | | - }, 50); |
47 | | - } |
| 45 | + ngAfterViewInit() { |
| 46 | + this.cookieService.get(isCookieAllowed) |
| 47 | + ? this.handleGoogleAnalyticsTracking() |
| 48 | + : this.cookieService.deleteAll(); |
48 | 49 |
|
49 | | - ngAfterViewInit() { |
50 | | - this.handleGoogleAnalyticsTracking(); |
51 | | - setTimeout(() => { |
52 | | - if (this.scrollPosition) { |
53 | | - this.scrollToPosition(this.scrollPosition); |
54 | | - } |
55 | | - }, 300); |
| 50 | + setTimeout(() => { |
| 51 | + if (this.scrollPosition) { |
| 52 | + this.scrollToPosition(this.scrollPosition); |
| 53 | + } |
| 54 | + }, 300); |
56 | 55 |
|
57 | | - this.$routeEvents = this.router.events |
58 | | - .pipe(filter((evt: any) => evt instanceof RoutesRecognized), pairwise()) |
59 | | - .subscribe((events: RoutesRecognized[]) => { |
60 | | - const previousUrl = events[0].urlAfterRedirects; |
61 | | - const currentUrl = events[1].urlAfterRedirects; |
62 | | - if (previousUrl === currentUrl) { |
63 | | - return; |
64 | | - } |
| 56 | + this.$routeEvents = this.router.events |
| 57 | + .pipe(filter((evt: any) => evt instanceof RoutesRecognized), pairwise()) |
| 58 | + .subscribe((events: RoutesRecognized[]) => { |
| 59 | + const previousUrl = events[0].urlAfterRedirects; |
| 60 | + const currentUrl = events[1].urlAfterRedirects; |
| 61 | + if (previousUrl === currentUrl) { |
| 62 | + return; |
| 63 | + } |
65 | 64 |
|
66 | | - const previousFragment = this.router.parseUrl(events[0].urlAfterRedirects).fragment; |
67 | | - const currentFragment = this.router.parseUrl(events[1].urlAfterRedirects).fragment; |
68 | | - const previousRouteUrl = this.router.parseUrl(events[0].urlAfterRedirects).root.children["primary"]?.segments[0].path; |
69 | | - const currentRouteUrl = this.router.parseUrl(events[1].urlAfterRedirects).root.children["primary"]?.segments[0].path; |
70 | | - if (previousUrl !== currentUrl && previousRouteUrl === currentRouteUrl) { |
71 | | - if (previousFragment || currentFragment) { |
72 | | - return; |
73 | | - } |
74 | | - } |
| 65 | + const previousFragment = this.router.parseUrl(events[0].urlAfterRedirects).fragment; |
| 66 | + const currentFragment = this.router.parseUrl(events[1].urlAfterRedirects).fragment; |
| 67 | + const previousRouteUrl = this.router.parseUrl(events[0].urlAfterRedirects).root.children['primary']?.segments[0].path; |
| 68 | + const currentRouteUrl = this.router.parseUrl(events[1].urlAfterRedirects).root.children['primary']?.segments[0].path; |
| 69 | + if (previousUrl !== currentUrl && previousRouteUrl === currentRouteUrl) { |
| 70 | + if (previousFragment || currentFragment) { |
| 71 | + return; |
| 72 | + } |
| 73 | + } |
75 | 74 |
|
76 | | - this.scrollToPosition(0); |
77 | | - }); |
78 | | - } |
| 75 | + this.scrollToPosition(0); |
| 76 | + }); |
| 77 | + } |
79 | 78 |
|
80 | | - ngOnDestroy() { |
81 | | - this.$routeEvents?.unsubscribe(); |
82 | | - this.$routerEventNavigationEnd?.unsubscribe(); |
83 | | - this.$gaRouteEvents?.unsubscribe(); |
84 | | - } |
| 79 | + ngOnDestroy() { |
| 80 | + this.$routeEvents?.unsubscribe(); |
| 81 | + this.$routerEventNavigationEnd?.unsubscribe(); |
| 82 | + this.$gaRouteEvents?.unsubscribe(); |
| 83 | + } |
85 | 84 |
|
86 | | - private handleGoogleAnalyticsTracking(): void { |
87 | | - let configured = false; |
88 | | - this.$gaRouteEvents = this.router.events.subscribe((event) => { |
89 | | - if (event instanceof NavigationEnd) { |
90 | | - if(!configured){ |
91 | | - gtag('config', 'UA-73071494-2', { send_page_view: false }); |
92 | | - configured = true; |
93 | | - } |
94 | | - gtag('event', 'page_view', { |
95 | | - page_location: window.location.href, |
96 | | - page_path: event.urlAfterRedirects, |
97 | | - }); |
98 | | - } |
99 | | - }); |
100 | | - } |
| 85 | + onAcceptCookies(): void { |
| 86 | + this.cookieService.set(isCookieAllowed, 'true'); |
| 87 | + this.showCookieConsentBanner.next(false); |
| 88 | + /** make navigation to current url to trigger googleAnalytics on current route */ |
| 89 | + this.redirectTo(this.router.url); |
| 90 | + this.handleGoogleAnalyticsTracking(); |
| 91 | + } |
| 92 | + |
| 93 | + private scrollToPosition(value: number): void { |
| 94 | + setTimeout(() => { |
| 95 | + window.scrollTo(0, value); |
| 96 | + window.sessionStorage.removeItem('scrollPosition'); |
| 97 | + }, 50); |
| 98 | + } |
| 99 | + |
| 100 | + private handleGoogleAnalyticsTracking(): void { |
| 101 | + let configured = false; |
| 102 | + this.$gaRouteEvents = this.router.events.subscribe((event) => { |
| 103 | + if (event instanceof NavigationEnd) { |
| 104 | + if (!configured) { |
| 105 | + gtag('config', 'UA-73071494-2', { send_page_view: false }); |
| 106 | + configured = true; |
| 107 | + } |
| 108 | + gtag('event', 'page_view', { |
| 109 | + page_location: window.location.href, |
| 110 | + page_path: event.urlAfterRedirects, |
| 111 | + }); |
| 112 | + } |
| 113 | + }); |
| 114 | + } |
| 115 | + |
| 116 | + private redirectTo(uri: string): void { |
| 117 | + this.router.navigateByUrl('/', { skipLocationChange: true }) |
| 118 | + .then(() => this.router.navigate([uri])); |
| 119 | + } |
101 | 120 | } |
0 commit comments