Skip to content

Commit

Permalink
fix(app): track pageviews in Google Analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
philmerrell committed Apr 24, 2018
1 parent 3fb24c0 commit 3eb484a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 40 deletions.
107 changes: 68 additions & 39 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Title } from '@angular/platform-browser';
import { OverlayContainer } from '@angular/cdk/overlay';
import { Component, HostBinding, OnDestroy, OnInit } from '@angular/core';
import { ActivationEnd, Router } from '@angular/router';
import { ActivationEnd, Router, NavigationEnd } from '@angular/router';
import { Store } from '@ngrx/store';
import { Subject } from 'rxjs';
import { takeUntil, filter } from 'rxjs/operators';
Expand All @@ -14,7 +14,7 @@ import {
} from '@app/core';
import { environment as env } from '@env/environment';

import { NIGHT_MODE_THEME, selectorSettings } from './settings';
import { NIGHT_MODE_THEME, selectorSettings, SettingsState } from './settings';

@Component({
selector: 'anms-root',
Expand Down Expand Up @@ -51,43 +51,9 @@ export class AppComponent implements OnInit, OnDestroy {
) {}

ngOnInit(): void {
this.store
.select(selectorSettings)
.pipe(takeUntil(this.unsubscribe$))
.subscribe(settings => {
const { theme, autoNightMode } = settings;
const hours = new Date().getHours();
const effectiveTheme = (autoNightMode && (hours >= 20 || hours <= 6)
? NIGHT_MODE_THEME
: theme
).toLowerCase();
this.componentCssClass = effectiveTheme;
const classList = this.overlayContainer.getContainerElement().classList;
const toRemove = Array.from(classList).filter((item: string) =>
item.includes('-theme')
);
classList.remove(...toRemove);
classList.add(effectiveTheme);
});
this.store
.select(selectorAuth)
.pipe(takeUntil(this.unsubscribe$))
.subscribe(auth => (this.isAuthenticated = auth.isAuthenticated));
this.router.events
.pipe(
takeUntil(this.unsubscribe$),
filter(event => event instanceof ActivationEnd)
)
.subscribe((event: ActivationEnd) => {
let lastChild = event.snapshot;
while (lastChild.children.length) {
lastChild = lastChild.children[0];
}
const { title } = lastChild.data;
this.titleService.setTitle(
title ? `${title} - ${env.appName}` : env.appName
);
});
this.subscribeToSettings();
this.subscribeToIsAuthenticated();
this.subscribeToRouterEvents();
}

ngOnDestroy(): void {
Expand All @@ -102,4 +68,67 @@ export class AppComponent implements OnInit, OnDestroy {
onLogoutClick() {
this.store.dispatch(new ActionAuthLogout());
}

private subscribeToIsAuthenticated() {
this.store
.select(selectorAuth)
.pipe(takeUntil(this.unsubscribe$))
.subscribe(auth => (this.isAuthenticated = auth.isAuthenticated));
}

private subscribeToSettings() {
this.store
.select(selectorSettings)
.pipe(takeUntil(this.unsubscribe$))
.subscribe(settings => this.setTheme(settings));
}

private setTheme(settings: SettingsState) {
const { theme, autoNightMode } = settings;
const hours = new Date().getHours();
const effectiveTheme = (autoNightMode && (hours >= 20 || hours <= 6)
? NIGHT_MODE_THEME
: theme
).toLowerCase();
this.componentCssClass = effectiveTheme;
const classList = this.overlayContainer.getContainerElement().classList;
const toRemove = Array.from(classList).filter((item: string) =>
item.includes('-theme')
);
classList.remove(...toRemove);
classList.add(effectiveTheme);
}

private subscribeToRouterEvents() {
this.router.events
.pipe(
takeUntil(this.unsubscribe$),
filter(event => event instanceof ActivationEnd || event instanceof NavigationEnd)
)
.subscribe((event: ActivationEnd) => {
if (event instanceof ActivationEnd) {
this.setPageTitle(event);
}

if (event instanceof NavigationEnd) {
this.trackPageView(event);
}
});
}

private setPageTitle(event: ActivationEnd) {
let lastChild = event.snapshot;
while (lastChild.children.length) {
lastChild = lastChild.children[0];
}
const { title } = lastChild.data;
this.titleService.setTitle(
title ? `${title} - ${env.appName}` : env.appName
);
}

private trackPageView(event: NavigationEnd) {
(<any>window).ga('set', 'page', event.urlAfterRedirects);
(<any>window).ga('send', 'pageview');
}
}
1 change: 0 additions & 1 deletion src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-53234284-5', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>

0 comments on commit 3eb484a

Please sign in to comment.