From 877c4b4d8953dc7f8a614bdb64802b8e1fb84350 Mon Sep 17 00:00:00 2001 From: Patrick Michalina Date: Mon, 19 Mar 2018 15:45:33 -0500 Subject: [PATCH] feat: flag to remove hashes (#) from url path (#247) --- README.md | 12 ++++++++++++ src/lib/core/angulartics2-config.ts | 3 +++ src/lib/core/angulartics2.spec.ts | 12 ++++++++++++ src/lib/core/angulartics2.ts | 4 +++- 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index db7df0c5..f7985c85 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ Vendor-agnostic Analytics for Angular Applications. [angulartics.github.io/angul - [Exclude routes from automatic pageview tracking](#exclude-routes-from-automatic-pageview-tracking) - [Remove IDs from url paths](#remove-ids-from-url-paths) - [Remove Query Params from url paths](#remove-query-params-from-url-paths) + - [Remove Hash from url paths](#remove-hash-from-url-paths) - [Using Without A Router](#using-without-a-router) - [Using With UI-Router](#using-with-ui-router) - [SystemJS](#systemjs) @@ -202,6 +203,17 @@ Angulartics2Module.forRoot([providers], { }), ```` +#### Remove Hash from url paths + +`/callback#authcode=123&idToken=456` becomes `/callback` +````ts +Angulartics2Module.forRoot([providers], { + pageTracking: { + clearHash: true, + } +}), +```` + ### Using Without A Router __Warning:__ this support is still experiemental diff --git a/src/lib/core/angulartics2-config.ts b/src/lib/core/angulartics2-config.ts index 9bf5bf6b..c7cccc43 100644 --- a/src/lib/core/angulartics2-config.ts +++ b/src/lib/core/angulartics2-config.ts @@ -20,6 +20,8 @@ export interface PageTrackingSettings { excludedRoutes: (string | RegExp)[]; /** drop ids from url `/sections/123/pages/456` -> `/sections/pages` */ clearIds: boolean; + /** drop contents of url after hash marker `/callback#authcode=1234` -> `/callback` */ + clearHash: boolean; /** drop query params from url `/sections/123/pages?param=456¶m2=789` -> `/sections/123/pages` */ clearQueryParams: boolean; /** used with clearIds, define the matcher to clear url parts */ @@ -41,6 +43,7 @@ export class DefaultConfig implements Angulartics2Settings { basePath: '', excludedRoutes: [], clearIds: false, + clearHash: false, clearQueryParams: false, idsRegExp: /^\d+$|^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/, }; diff --git a/src/lib/core/angulartics2.spec.ts b/src/lib/core/angulartics2.spec.ts index 26cb3bdc..2ac302c6 100644 --- a/src/lib/core/angulartics2.spec.ts +++ b/src/lib/core/angulartics2.spec.ts @@ -376,6 +376,18 @@ describe('angulartics2', () => { })), ); + it('should remove hash if clearHash is set', + fakeAsync(inject([Router, Location, Angulartics2], + (router: Router, location: Location, angulartics2: Angulartics2) => { + fixture = createRootWithRouter(router, RootCmp); + angulartics2.pageTrack.subscribe((x: any) => EventSpy(x)); + angulartics2.settings.pageTracking.clearHash = true; + (location).simulateUrlPop('/0sections0/a01/pages/page/2/summary#authCode=123'); + advance(fixture); + expect(EventSpy).toHaveBeenCalledWith({ path: '/0sections0/a01/pages/page/2/summary' }); + })), + ); + it('should remove ids and query params if clearQueryParams and clearIds are set', fakeAsync(inject([Router, Location, Angulartics2], (router: Router, location: Location, angulartics2: Angulartics2) => { diff --git a/src/lib/core/angulartics2.ts b/src/lib/core/angulartics2.ts index e292883d..d6c81d37 100644 --- a/src/lib/core/angulartics2.ts +++ b/src/lib/core/angulartics2.ts @@ -103,10 +103,12 @@ export class Angulartics2 { * @param url current page path */ protected clearUrl(url: string): string { - if (this.settings.pageTracking.clearIds || this.settings.pageTracking.clearQueryParams) { + if (this.settings.pageTracking.clearIds || this.settings.pageTracking.clearQueryParams || + this.settings.pageTracking.clearHash) { return url .split('/') .map(part => this.settings.pageTracking.clearQueryParams ? part.split('?')[0] : part) + .map(part => this.settings.pageTracking.clearHash ? part.split('#')[0] : part) .filter(part => !this.settings.pageTracking.clearIds || !part.match(this.settings.pageTracking.idsRegExp)) .join('/'); }