From 7b4e2757e218ced9c395500bb1917af451979fca Mon Sep 17 00:00:00 2001 From: Michal Dydo Date: Mon, 6 Apr 2020 10:43:29 +0200 Subject: [PATCH 1/2] GH-6 added google analytics configuration, events service and example events --- package.json | 1 + src/app/app.component.ts | 38 +++++++++++++-- .../dvnt-bottom-navigation.component.html | 2 +- .../dvnt-bottom-navigation.component.ts | 23 +++++++++- .../dvnt-footer/dvnt-footer.component.html | 6 ++- .../dvnt-footer/dvnt-footer.component.ts | 38 ++++++++++++++- .../dvnt-header-logo.component.html | 2 +- .../dvnt-header-logo.component.ts | 20 +++++++- .../dvnt-login/dvnt-login.component.html | 2 +- .../dvnt-login/dvnt-login.component.ts | 24 ++++++++++ .../dvnt-mini-cart.component.ts | 13 ++++++ .../dvnt-wish-list-icon.component.html | 2 +- .../dvnt-wish-list-icon.component.ts | 17 ++++++- .../dvnt-bestselling-products.component.html | 1 + .../dvnt-bestselling-products.component.ts | 23 +++++++++- .../dvnt-new-products.component.html | 1 + .../dvnt-new-products.component.ts | 23 +++++++++- .../dvnt-add-product-to-cart.component.ts | 20 +++++++- .../dvnt-carousel-products.component.html | 1 + .../dvnt-carousel-products.component.ts | 8 ++++ .../dvnt-wish-list-item.component.ts | 16 ++++++- .../dvnt-wish-list.component.ts | 28 ++++++++++- .../google-analytics-event.service.ts | 46 +++++++++++++++++++ .../google-analytics/google-analytics.enum.ts | 26 +++++++++++ src/app/shared/shared.module.ts | 3 ++ src/index.html | 17 +++++-- yarn.lock | 5 ++ 27 files changed, 381 insertions(+), 25 deletions(-) create mode 100644 src/app/shared/google-analytics/google-analytics-event.service.ts create mode 100644 src/app/shared/google-analytics/google-analytics.enum.ts diff --git a/package.json b/package.json index 56a8a04..3686b33 100644 --- a/package.json +++ b/package.json @@ -53,6 +53,7 @@ "@angular-devkit/build-angular": "~0.803.24", "@angular/cli": "~8.3.24", "@angular/compiler-cli": "~8.2.14", + "@types/google.analytics": "0.0.40", "@angular/language-service": "~8.2.14", "@types/jasmine": "~3.3.8", "@types/jasminewd2": "~2.0.3", diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 1898d86..3331d16 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,17 +1,47 @@ -import { Component } from '@angular/core'; +import { Component, OnDestroy } from '@angular/core'; +import { Router, NavigationEnd } from '@angular/router'; +import { + StorefrontComponent, + HamburgerMenuService, +} from '@spartacus/storefront'; +import { ContentSlotComponentData, RoutingService } from '@spartacus/core'; +import { Subject } from 'rxjs'; +import { takeUntil } from 'rxjs/operators'; -import { StorefrontComponent } from '@spartacus/storefront'; -import { ContentSlotComponentData } from '@spartacus/core'; +import { GoogleAnalyticsEventsService } from './shared/google-analytics/google-analytics-event.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'], }) -export class AppComponent extends StorefrontComponent { +export class AppComponent extends StorefrontComponent implements OnDestroy { public footerComponent: ContentSlotComponentData = { uid: 'FooterNavigationComponent', typeCode: 'FooterNavigationComponent', flexType: 'FooterNavigationComponent', }; + + private unsubscribe: Subject = new Subject(); + + constructor( + hamburgerMenuService: HamburgerMenuService, + routingService: RoutingService, + private googleAnalyticsEventsService: GoogleAnalyticsEventsService, + private router: Router + ) { + super(hamburgerMenuService, routingService); + + this.router.events.pipe(takeUntil(this.unsubscribe)).subscribe((event) => { + if (event instanceof NavigationEnd) { + this.googleAnalyticsEventsService.emitSetPage(event.urlAfterRedirects); + console.log('SET PAGE', event.urlAfterRedirects); + } + }); + } + + public ngOnDestroy(): void { + this.unsubscribe.next(); + this.unsubscribe.complete(); + } } diff --git a/src/app/features/dvnt-bottom-navigation/dvnt-bottom-navigation.component.html b/src/app/features/dvnt-bottom-navigation/dvnt-bottom-navigation.component.html index be6fd65..a898667 100644 --- a/src/app/features/dvnt-bottom-navigation/dvnt-bottom-navigation.component.html +++ b/src/app/features/dvnt-bottom-navigation/dvnt-bottom-navigation.component.html @@ -1,5 +1,5 @@
-