From 36e26110cf6c6f28fa55fa0552321668bf322e29 Mon Sep 17 00:00:00 2001 From: Dmitry Nehaychik <4dmitr@gmail.com> Date: Thu, 21 Jun 2018 16:17:19 +0300 Subject: [PATCH] fix(docs): update analytics tracking to exclude duplicated events (#502) --- docs/app/@theme/services/analytics.service.ts | 19 ++++++++++++++++--- .../tabbed-block/tabbed-block.component.ts | 2 +- docs/app/example/example.component.ts | 4 +++- docs/index.html | 2 -- 4 files changed, 20 insertions(+), 7 deletions(-) diff --git a/docs/app/@theme/services/analytics.service.ts b/docs/app/@theme/services/analytics.service.ts index 2d4e990c6a..c01b8e2578 100644 --- a/docs/app/@theme/services/analytics.service.ts +++ b/docs/app/@theme/services/analytics.service.ts @@ -2,7 +2,7 @@ import { Injectable, Inject } from '@angular/core'; import { NavigationEnd, Router } from '@angular/router'; import { Location } from '@angular/common'; -import { filter } from 'rxjs/operators'; +import { filter, delay, map } from 'rxjs/operators'; import { NB_WINDOW } from '@nebular/theme'; declare const ga: any; @@ -20,9 +20,12 @@ export class NgdAnalytics { if (this.enabled) { this.router.events.pipe( filter((event) => event instanceof NavigationEnd), + map(() => this.location.path()), + filter((location: string) => this.trackLocation(location)), + delay(50), ) - .subscribe(() => { - ga('send', {hitType: 'pageview', page: this.location.path()}); + .subscribe((location: string) => { + ga('send', { hitType: 'pageview', page: location }); }); } } @@ -32,4 +35,14 @@ export class NgdAnalytics { ga('send', 'event', eventName, eventVal); } } + + private trackLocation(path: string) { + if (path.match(/\/components\/[a-zA-Z-]+\/?$/) + || path.match(/\/docs\/?$/) + || path.match(/\/example\//)) { + + return !!path.match(/\/components\/components-overview\/?$/); + } + return true; + } } diff --git a/docs/app/blocks/components/tabbed-block/tabbed-block.component.ts b/docs/app/blocks/components/tabbed-block/tabbed-block.component.ts index 6e504a5582..4ce50a8552 100644 --- a/docs/app/blocks/components/tabbed-block/tabbed-block.component.ts +++ b/docs/app/blocks/components/tabbed-block/tabbed-block.component.ts @@ -48,7 +48,7 @@ export class NgdTabbedBlockComponent implements OnDestroy { this.tabs$.pipe(filter((tabs) => tabs && tabs.length)), ]) .subscribe(([params, tabs]) => { - this.router.navigate([tabs[0].tab], { relativeTo: activatedRoute }); + this.router.navigate([tabs[0].tab], { relativeTo: activatedRoute, replaceUrl: true }); }); combineLatest([ diff --git a/docs/app/example/example.component.ts b/docs/app/example/example.component.ts index 25895f1e5f..321cc4ee7d 100644 --- a/docs/app/example/example.component.ts +++ b/docs/app/example/example.component.ts @@ -2,7 +2,7 @@ import { AfterViewInit, Component, Inject, OnDestroy, OnInit } from '@angular/co import { Router } from '@angular/router'; import { takeWhile } from 'rxjs/operators'; import { NB_DOCUMENT, NbThemeService } from '@nebular/theme'; -import { NgdIframeCommunicatorService } from '../@theme/services'; +import { NgdAnalytics, NgdIframeCommunicatorService } from '../@theme/services'; @Component({ selector: 'ngd-example', @@ -16,12 +16,14 @@ export class NgdExampleComponent implements OnInit, AfterViewInit, OnDestroy { constructor(private communicator: NgdIframeCommunicatorService, private themeService: NbThemeService, private router: Router, + private analytics: NgdAnalytics, @Inject(NB_DOCUMENT) private document) { } ngOnInit() { this.setupId(); this.subscribeOnThemeSwitch(); + this.analytics.trackEvent('example-view', this.id); } ngAfterViewInit() { diff --git a/docs/index.html b/docs/index.html index 0eb970592b..70444035e6 100644 --- a/docs/index.html +++ b/docs/index.html @@ -20,8 +20,6 @@ })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-61945105-4', 'auto'); - ga('send', 'pageview'); -