From a605bd9390b68bb27337de53dddd59eea55c1763 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 18 Feb 2018 17:16:37 -0600 Subject: [PATCH] alerts-ui: Clean url before reporting to google analytics - Query parameters are not tracked. - The token is removed from urls containing an unique token. --- alerts-ui/src/app/app.component.ts | 33 +++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/alerts-ui/src/app/app.component.ts b/alerts-ui/src/app/app.component.ts index d953865..daa1a60 100644 --- a/alerts-ui/src/app/app.component.ts +++ b/alerts-ui/src/app/app.component.ts @@ -40,10 +40,41 @@ export class AppComponent implements OnInit { return true; }).subscribe((x: any) => { - (window).gtag('config', environment.gtag.id, { 'page_path': x.url }); + const dirtyUrl: string = x.url || ''; + const url = this.cleanUrlForAnalytics(dirtyUrl); + (window).gtag('config', environment.gtag.id, { 'page_path': url }); }); } + private cleanUrlForAnalytics(dirtyUrl: string): string { + const urlWithoutParams = this.removeQueryParams(dirtyUrl); + const urlWithoutToken = this.removeTokens(urlWithoutParams); + + return urlWithoutToken; + } + + private removeTokens(dirtyUrl: string): string { + const urlWithToken = ['/verify-email']; + const result = urlWithToken + .filter(prefix => dirtyUrl.indexOf(prefix) >= 0) + .map(prefix => dirtyUrl.substring(0, dirtyUrl.indexOf(prefix) + prefix.length) ); + + if (result.length === 0) { + return dirtyUrl; + } else { + return result[0]; + } + } + + private removeQueryParams(url: string): string { + const index = url.indexOf('?'); + if (index >= 0) { + return url.substring(0, index); + } else { + return url; + } + } + englishLang(): Object { return { 'home.examples': 'Examples',