Skip to content

Commit

Permalink
GH-6 added google analytics configuration, events service and example…
Browse files Browse the repository at this point in the history
… events
  • Loading branch information
dydome committed Apr 6, 2020
1 parent 2ff83be commit 7b4e275
Show file tree
Hide file tree
Showing 27 changed files with 381 additions and 25 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
38 changes: 34 additions & 4 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -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<any> = 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();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="bottom-navigation-container">
<div (click)="navigateTo('', $event)" class="navigation-item">
<div (click)="navigateToHomePage($event)" class="navigation-item">
<app-dvnt-icon
className="navigation-item-icon"
fill="#A3A5AD"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import { DvntModalSearchComponent } from 'src/app/features/dvnt-modal-search/dvn
import { DvntProfileLinkListComponent } from 'src/app/features/dvnt-profile-link-list/dvnt-profile-link-list.component';
import { DvntModalCategoriesComponent } from '../dvnt-modal-categories/dvnt-modal-categories.component';
import { DvntCartModalComponent } from '../dvnt-cart/dvnt-cart-modal/dvnt-cart-modal.component';
import { GoogleAnalyticsEventsService } from 'src/app/shared/google-analytics/google-analytics-event.service';
import {
GoogleEventCategory,
GoogleActionCategory,
GoogleLabelCategory,
} from 'src/app/shared/google-analytics/google-analytics.enum';

@Component({
selector: 'app-dvnt-bottom-navigation',
Expand All @@ -18,6 +24,7 @@ export class DvntBottomNavigationComponent implements OnDestroy {

constructor(
private authService: AuthService,
private googleAnalyticsEventsService: GoogleAnalyticsEventsService,
private routingService: RoutingService,
private modalService: ModalService
) {}
Expand All @@ -26,10 +33,15 @@ export class DvntBottomNavigationComponent implements OnDestroy {
this.clearModalRef();
}

public navigateTo(path: string, event: Event): void {
public navigateToHomePage(event: Event): void {
this.preventOtherActions(event);

this.routingService.goByUrl(path);
this.routingService.goByUrl('');

this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.Navigation,
GoogleActionCategory.HomePageRedirect
);
}

public setSearchAction(event: Event): void {
Expand Down Expand Up @@ -81,6 +93,13 @@ export class DvntBottomNavigationComponent implements OnDestroy {
private createModal(component: any, componentType: string): void {
this.clearModalRef();
this.createModelInstance(component, componentType);

this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.Modal,
GoogleActionCategory.OpenModal,
GoogleLabelCategory.BottomNavigationModal,
componentType
);
}

private createModelInstance(component: any, componentType: string): void {
Expand Down
6 changes: 5 additions & 1 deletion src/app/features/dvnt-footer/dvnt-footer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
{{ column?.title }}
</li>
<li *ngFor="let item of column.children" class="footer-menu-item">
<a [href]="item.url" [target]="item.target">
<a
(click)="footerSectionSelected(column.title, item.title)"
[href]="item.url"
[target]="item.target"
>
{{ item.title }}
</a>
</li>
Expand Down
38 changes: 36 additions & 2 deletions src/app/features/dvnt-footer/dvnt-footer.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
import { Component } from '@angular/core';
import { FooterNavigationComponent } from '@spartacus/storefront';
import {
FooterNavigationComponent,
CmsComponentData,
NavigationService,
} from '@spartacus/storefront';
import {
Product,
CmsNavigationComponent,
AnonymousConsentsConfig,
} from '@spartacus/core';

import { GoogleAnalyticsEventsService } from 'src/app/shared/google-analytics/google-analytics-event.service';
import {
GoogleEventCategory,
GoogleActionCategory,
GoogleLabelCategory,
} from 'src/app/shared/google-analytics/google-analytics.enum';

@Component({
selector: 'app-dvnt-footer',
templateUrl: './dvnt-footer.component.html',
styleUrls: ['./dvnt-footer.component.scss'],
})
export class DvntFooterComponent extends FooterNavigationComponent {}
export class DvntFooterComponent extends FooterNavigationComponent {
constructor(
anonymousConsentsConfig: AnonymousConsentsConfig,
componentData: CmsComponentData<CmsNavigationComponent>,
service: NavigationService,
private googleAnalyticsEventsService: GoogleAnalyticsEventsService
) {
super(componentData, service, anonymousConsentsConfig);
}

public footerSectionSelected(column: string, title: string): void {
this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.SelectFooterSection,
GoogleActionCategory.Footer,
GoogleLabelCategory.FooterSection,
{ column, title }
);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<a role="link" routerLink="">
<a (click)="goToHomePage()" role="link" routerLink="">
<img src="../../../../assets/images/homepage-logo.svg" alt="Logo" />
</a>
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
import { Component } from '@angular/core';
import { GoogleAnalyticsEventsService } from 'src/app/shared/google-analytics/google-analytics-event.service';

import {
GoogleEventCategory,
GoogleActionCategory,
GoogleLabelCategory,
} from 'src/app/shared/google-analytics/google-analytics.enum';

@Component({
selector: 'app-dvnt-header-logo',
templateUrl: './dvnt-header-logo.component.html',
})
export class DvntHeaderLogoComponent {}
export class DvntHeaderLogoComponent {
constructor(
private googleAnalyticsEventsService: GoogleAnalyticsEventsService
) {}

public goToHomePage(): void {
this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.Navigation,
GoogleActionCategory.HomePageRedirect
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<ng-template #login>
<a
(click)="blockPropagation($event)"
(click)="loginEvent($event)"
[routerLink]="{ cxRoute: 'login' } | cxUrl"
class="header-navigation-icon"
role="link"
Expand Down
24 changes: 24 additions & 0 deletions src/app/features/dvnt-header/dvnt-login/dvnt-login.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import { Component } from '@angular/core';
import { LoginComponent } from '@spartacus/storefront';
import { AuthService, UserService } from '@spartacus/core';

import { GoogleAnalyticsEventsService } from 'src/app/shared/google-analytics/google-analytics-event.service';
import {
GoogleEventCategory,
GoogleActionCategory,
} from 'src/app/shared/google-analytics/google-analytics.enum';

@Component({
selector: 'app-dvnt-login',
templateUrl: './dvnt-login.component.html',
})
export class DvntLoginComponent extends LoginComponent {
constructor(
auth: AuthService,
userService: UserService,
private googleAnalyticsEventsService: GoogleAnalyticsEventsService
) {
super(auth, userService);
}

public loginEvent(event: Event): void {
this.blockPropagation(event);

this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.Navigation,
GoogleActionCategory.Login
);
}

public blockPropagation(event: Event): void {
event.stopPropagation();
event.preventDefault();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { MiniCartComponent, ModalService } from '@spartacus/storefront';
import { CartService } from '@spartacus/core';

import { DvntCartModalComponent } from '../../dvnt-cart/dvnt-cart-modal/dvnt-cart-modal.component';
import { GoogleAnalyticsEventsService } from 'src/app/shared/google-analytics/google-analytics-event.service';
import {
GoogleEventCategory,
GoogleActionCategory,
GoogleLabelCategory,
} from 'src/app/shared/google-analytics/google-analytics.enum';

@Component({
selector: 'app-dvnt-mini-cart',
Expand All @@ -13,6 +19,7 @@ export class DvntMiniCartComponent extends MiniCartComponent {
constructor(
// tslint:disable-next-line: deprecation
protected cartService: CartService,
private googleAnalyticsEventsService: GoogleAnalyticsEventsService,
private modalService: ModalService
) {
super(cartService);
Expand All @@ -26,5 +33,11 @@ export class DvntMiniCartComponent extends MiniCartComponent {
windowClass: 'side-modal slide-from-right',
backdropClass: 'side-modal-backdrop',
});

this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.Modal,
GoogleActionCategory.OpenModal,
GoogleLabelCategory.DesktopCart
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<ng-container *ngIf="user$ | async as user">
<a
(click)="blockPropagation($event)"
(click)="goToWishList($event)"
[routerLink]="'my-account/wishlist'"
class="header-navigation-icon navigation-link"
role="link"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import {
User,
WishListService,
} from '@spartacus/core';
import { GoogleAnalyticsEventsService } from 'src/app/shared/google-analytics/google-analytics-event.service';
import {
GoogleEventCategory,
GoogleActionCategory,
} from 'src/app/shared/google-analytics/google-analytics.enum';

@Component({
selector: 'app-dvnt-wish-list-icon',
Expand All @@ -25,6 +30,7 @@ export class DvntWishListIconComponent implements OnInit {

constructor(
private auth: AuthService,
private googleAnalyticsEventsService: GoogleAnalyticsEventsService,
private userService: UserService,
private wishListService: WishListService
) {}
Expand All @@ -41,7 +47,16 @@ export class DvntWishListIconComponent implements OnInit {
);
}

public blockPropagation(event: Event): void {
public goToWishList(event: Event): void {
this.blockPropagation(event);

this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.Navigation,
GoogleActionCategory.WishListRedirect
);
}

private blockPropagation(event: Event): void {
event.stopPropagation();
event.preventDefault();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<app-dvnt-carousel-products
(emitClickEvent)="productSelected($event)"
[componentData]="componentData$"
></app-dvnt-carousel-products>
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { Component } from '@angular/core';
import { CmsService } from '@spartacus/core';
import { CmsService, Product } from '@spartacus/core';
import { filter } from 'rxjs/operators';
import { Observable } from 'rxjs';

import { GoogleAnalyticsEventsService } from 'src/app/shared/google-analytics/google-analytics-event.service';
import {
GoogleEventCategory,
GoogleActionCategory,
GoogleLabelCategory,
} from 'src/app/shared/google-analytics/google-analytics.enum';

@Component({
selector: 'app-dvnt-bestselling-products',
templateUrl: './dvnt-bestselling-products.component.html',
Expand All @@ -13,5 +20,17 @@ export class DvntBestsellingProductsComponent {
.getComponentData('ElectronicsHomepageProductCarouselComponent')
.pipe(filter(Boolean));

constructor(private cmsService: CmsService) {}
constructor(
private cmsService: CmsService,
private googleAnalyticsEventsService: GoogleAnalyticsEventsService
) {}

public productSelected(product: Product): void {
this.googleAnalyticsEventsService.emitEvent(
GoogleEventCategory.SelectProduct,
GoogleActionCategory.Bestseller,
GoogleLabelCategory.ProductCode,
product.code
);
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<app-dvnt-carousel-products
(emitClickEvent)="productSelected($event)"
[componentData]="componentData$"
></app-dvnt-carousel-products>
Loading

0 comments on commit 7b4e275

Please sign in to comment.